php - 如何在zend框架中获取插入记录的id

标签 php mysql zend-framework2 tablegateway

我想获取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/

相关文章:

php - 如何获取当前年份并在 MySQL 中使用它作为过滤器?

php - ZF2 - 基于完整路径在 View 中显示图像

php - 具有子路由的子域的 ZF2 路由器配置

php - 自定义表单中的 Drupal Imagfield/Filefield

mysql - CSV 日期格式转换为 MySQL 日期格式

php - 从 Youtube channel 获取视频链接,从 Urls 中提取视频 ID 并将它们存储在数组中

javascript - for循环中的node.js mysql查询

session - ZF2中如何实现鉴权

php - 将输入的日期与当前日期进行比较并得到错误的结果,因为服务器时区与输入的时区不同

php - Ajax 实时文本框按下所有 Html 元素