我希望表單域在表單頁面打開時包含數據庫中包含的先前數據。我在這裏經歷了大量的查詢,並且知道使用populate()或bind()方法是實現它的方法。但是當我嘗試使用它時,我得到一個未定義的方法錯誤。 有沒有其他方法可以做到這一點? 我也無法使用bind()。我在提交後獲得了一個帶有默認值的新表單。 對不起,這是一個愚蠢的問題。自從我開始學習Zend框架以來,這隻剩下4-5天。另外,我上網的大多數方法都是針對較老的框架。我正在使用Zend Framework2。無法讓bind()工作
這是控制器代碼
<?php
class ChatController extends AbstractActionController
{
protected $chatTable;
public function indexAction()
{
$form = new ChatForm();
$model= new Chat();
$form->bind($model);
$form->get('submit')->setValue('Save');
$request = $this->getRequest();
if ($request->isPost()) {
$gen_set = new Chat();
$form->setInputFilter($gen_set->getInputFilter());
$form->setData($request->getPost());
if ($form->isValid()) {
$gen_set->exchangeArray($form->getData());
$this->getChatTable()->saveChat($gen_set);
// Redirect to list of albums
return $this->redirect()->toRoute('chat');
}
}
return array('form' => $form);
}
public function getChatTable()
{
if (!$this->chatTable) {
$sm = $this->getServiceLocator();
$this->chatTable = $sm->get('Chat\Model\ChatTable');
}
return $this->chatTable;
}
}
我的實體類,這裏API_KEY和anon_prefix是列「設置」行,還有一個柱值。
<?php
class Chat implements InputFilterAwareInterface
{
protected $inputFilter;
public function exchangeArray($data)
{
$this->api_key=(isset($data['api_key'])) ? $data['api_key'] : null;
$this->anon_prefix = (isset($data['anon_prefix'])) ? $data['anon_prefix'] : null;
}
// Add content to these methods:
public function setInputFilter(InputFilterInterface $inputFilter)
{
throw new \Exception("Not used");
}
public function getInputFilter()
{
if (!$this->inputFilter) {
$inputFilter = new InputFilter();
$inputFilter->add(array(
'name' => 'iflychat_external_api_key',
'required' => true,
'filters' => array(
array('name' => 'StripTags'),
array('name' => 'StringTrim'),
),
));
$inputFilter->add(array(
'name' => 'iflychat_show_admin_list',
'required' => true,
'validators' => array(
array(
'name' => 'InArray',
'options' => array(
'haystack' => array(1,2),
),
),
),
));
$this->inputFilter = $inputFilter;
}
return $this->inputFilter;
}
public function getArrayCopy()
{
return get_object_vars($this);
}
}
這是用來值輸入到數據庫
<?php
class ChatTable
{
protected $tableGateway;
public function __construct(TableGateway $tableGateway)
{
$this->tableGateway = $tableGateway;
}
public function fetchAll()
{
$resultSet = $this->tableGateway->select();
return $resultSet;
}
public function saveChat(Chat $gen_set)
{
$data = array(
'value' => $gen_set->api_key,
);
$id='iflychat_external_api_key';
$this->tableGateway->update($data,array('settings' => $id));
$data = array(
'value' => $gen_set->anon_prefix,
);
$id='anon_prefix';
$this->tableGateway->update($data,array('settings' => $id));
}
}
我收到此錯誤,「不能使用類型爲聊天\型號\聊天作爲陣列的對象
檢查ZF2中的$ form-> bind($ model);從數據庫加載數據(通常用於編輯操作)。爲了使用form-submit之後發佈的數據來填充表單,執行此操作 - '$ request = $ this-> getRequest(); if($ request-> isPost()){$ form-> setData($ request-> getPost()); }' –
''model'有很多屬性時'bind()'非常有用。如果你想手動設置數據,那麼試試這個 - $ form-> get('id') - > setValue($ model-> id);''其中'id'是表單中的一個字段。 –
顯示您的代碼。 –