Zend framework2 Can';我不能让bind()工作
我希望我的表单字段在表单页面打开时包含数据库中以前包含的数据。我在这里经历了很多查询,并了解到使用populate()或bind()方法是实现这一点的方法。但是当我尝试使用它时,我得到了一个未定义的方法错误。 还有别的办法吗? 我也不能使用bind()。提交后,我将获得一个带有默认值的新表单。 对不起,如果这是一个愚蠢的问题。从我开始学习Zend框架到现在只有4-5天了。此外,我在线获得的大多数方法都是针对旧框架的。我正在使用Zend Framework2 这是控制器代码Zend framework2 Can';我不能让bind()工作,zend-framework2,Zend Framework2,我希望我的表单字段在表单页面打开时包含数据库中以前包含的数据。我在这里经历了很多查询,并了解到使用populate()或bind()方法是实现这一点的方法。但是当我尝试使用它时,我得到了一个未定义的方法错误。 还有别的办法吗? 我也不能使用bind()。提交后,我将获得一个带有默认值的新表单。 对不起,如果这是一个愚蠢的问题。从我开始学习Zend框架到现在只有4-5天了。此外,我在线获得的大多数方法都是针对旧框架的。我正在使用Zend Framework2 这是控制器代码 <?php cl
<?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;
}
您的操作没有多大意义,您将一个聊天
实例实例化为$model
,然后将另一个实例实例化为$gen\u set
。您应该做的是绑定第一个实例,然后使用form classgetData
方法稍后返回绑定到它的实例,以及在setData
方法中给定的值。不需要从对象到数组再到数组进行任何转换
下面是它的外观
public function indexAction()
{
$form = new ChatForm();
// bind the model
$model= new Chat();
$form->bind($model);
$form->get('submit')->setValue('Save');
$request = $this->getRequest();
if ($request->isPost()) {
$form->setInputFilter($gen_set->getInputFilter());
// set data from POST as properties of the bound model ...
$form->setData($request->getPost());
if ($form->isValid()) {
// get the bound model instance with the POSTed values
// ($gen_set is now the original $model object instance bound above)
$gen_set = $form->getData();
// and save it
$this->getChatTable()->saveChat($gen_set);
// Redirect to list of albums
return $this->redirect()->toRoute('chat');
}
}
return array('form' => $form);
}
控制器代码-
<?php
class ChatController extends AbstractActionController {
protected $chatTable;
public function indexAction() {
$model = $this->getChatTable()->fetchLastChat();
if($model === null || $model->count() == 0)
$model = new Chat();
//Now if no record exists in the database then $model will be empty
//Else $model will contain data of last record.
$form = new ChatForm();
$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);
}
}
return array('form' => $form);
}
public function getChatTable() {
if (!$this->chatTable) {
$sm = $this->getServiceLocator();
$this->chatTable = $sm->get('Chat\Model\ChatTable');
}
return $this->chatTable;
}
}
检查$form->bind($model)在ZF2中编写>以从数据库加载数据(通常用于编辑操作)。要使用表单提交后发布的数据填充表单,请执行以下操作-$request=$this->getRequest();如果($request->isPost()){$form->setData($request->getPost());}
当$model
有很多属性时,bind()
非常有用。如果要手动设置数据,请尝试以下操作-$form->get('id')->setValue($model->id)
其中id
是表单中的一个字段。@kunaldesh当我使用bind时,我得到一个错误“不能使用Chat\Model\Chat as array类型的对象”@kuldeep.kamboj code added。在您的控制器中添加这个use语句-use Zend\form\form interface代码>。然后代替$gen_set->exchangearlay($form->getData())代码>写入$gen\u set->exchangearlay($form->getData(FormInterface::VALUES\u AS\u ARRAY))代码>并尝试。我确实意识到我在发布后创建了两个聊天实例。但这仍然没有帮助。由于我的表单id及其值存储在2列的行中,我们在这里所做的是提取列值,我想:/
public function indexAction()
{
$form = new ChatForm();
// bind the model
$model= new Chat();
$form->bind($model);
$form->get('submit')->setValue('Save');
$request = $this->getRequest();
if ($request->isPost()) {
$form->setInputFilter($gen_set->getInputFilter());
// set data from POST as properties of the bound model ...
$form->setData($request->getPost());
if ($form->isValid()) {
// get the bound model instance with the POSTed values
// ($gen_set is now the original $model object instance bound above)
$gen_set = $form->getData();
// and save it
$this->getChatTable()->saveChat($gen_set);
// Redirect to list of albums
return $this->redirect()->toRoute('chat');
}
}
return array('form' => $form);
}
<?php
class ChatController extends AbstractActionController {
protected $chatTable;
public function indexAction() {
$model = $this->getChatTable()->fetchLastChat();
if($model === null || $model->count() == 0)
$model = new Chat();
//Now if no record exists in the database then $model will be empty
//Else $model will contain data of last record.
$form = new ChatForm();
$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);
}
}
return array('form' => $form);
}
public function getChatTable() {
if (!$this->chatTable) {
$sm = $this->getServiceLocator();
$this->chatTable = $sm->get('Chat\Model\ChatTable');
}
return $this->chatTable;
}
}
<?php
//Other use statements
use Zend\Db\Sql\Select;
class ChatTable {
protected $tableGateway;
public function __construct(TableGateway $tableGateway) {
$this->tableGateway = $tableGateway;
}
public function fetchAll() {
$resultSet = $this->tableGateway->select();
return $resultSet;
}
public function fetchLastChat() {
$select = new Select('TABLE_NAME'); //Change the tablename accordingly
$select->order('PRIMARY_KEY DESC'); //Set the Primary Key of the table
$select->limit(1);
$resultSet = $this->tableGateway->selectWith($select);
return $resultSet->current();
}
//Rest of the Code ....