我想获取zf2中插入记录的id。我在 php.ini 中使用scope_identity找到了解决方案。但如何在 zend 中使用它呢?
我在indexcontroller中的代码是
<?php
public function addAction()
{
$form = new UserForm();
$request = $this->getRequest();
if ($request->isPost())
{
$form->setData($request->getPost());
if($form->isValid())
{
$data=$form->getData();
$this->getUserTable()->insert($data);
}
}
}
public function getUserTable()
{
if(!$this->userTable)
{
$this->userTable = new TableGateway('eo_user',$this->getServiceLocator()->get('Zend\Db\Adapter\Adapter')
);
}
return $this->userTable;
}
eo_user 表的架构为
eo_user
user_id | username | user_password | user_email | user_status
这里的user_id是具有自增约束的主键。
我需要做哪些更改才能找到插入记录的 user_id?
最佳答案
您可以使用$this->getUserTable()->getLastInsertValue();
来获取插入记录的最后插入ID。
更新
$this->getUserTable()->insert($data);
$insertedId = $this->getUserTable()->getLastInsertValue();
echo $insertedId; // will get the latest id
关于php - 如何在zend框架中获取插入记录的id,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25029565/