php ZF2 如何获取最后插入的 id 值?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15908065/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 10:10:40  来源:igfitidea点击:

ZF2 How to get last insert id value?

phppostgresqlinsertframeworkszend-framework2

提问by Piotr

I am stuck with getting last insert id with Zend framework 2 and I gave up on this...

我坚持使用 Zend 框架 2 获取最后一个插入 ID,我放弃了这个......

There are tried combinations:

有尝试的组合:

var_dump($this->tableGateway->insert($insert));
var_dump($this->tableGateway->lastInsertValue);
var_dump($this->tableGateway->getLastInsertValue());
var_dump($this->tableGateway->getAdapter()->getDriver()->getConnection()->getLastGeneratedValue());

Value is inserting to table, but every line (except first, which gives int "1") returns null. Please don't tell me, that such a big framework does not give possibility to get last insert id value!?

值正在插入到表中,但每一行(除了第一行,它给出 int "1")返回 null。请不要告诉我,这么大的框架不可能获得最后插入的 id 值!?

回答by Jean Paul

Here is what I use:

这是我使用的:

$data = array()// Your data to be saved;
$this->tableGateway->insert($data);
$id = $this->tableGateway->lastInsertValue;

回答by skymander

I know this was a while back, but after spending a ton of time on this particular problem I wanted to post the solution for future Googlers experiencing the same problem.

我知道这是很久以前的事了,但是在这个特定问题上花费了大量时间之后,我想为未来遇到相同问题的 Google 员工发布解决方案。

The issue is the Pgsql driver requires a name of the sequence to return the last id like this:

问题是 Pgsql 驱动程序需要一个序列名称来返回最后一个 id,如下所示:

$adapter->getDriver()->getLastGeneratedValue("sequence_name");

This works in 2.3.1. There was a bug in 2.2.2 (and possibly later) where name was not a param of the getLastGeneratedValue in the driver, so you'd have to call the connection directly.

这适用于 2.3.1。在 2.2.2(可能以后)中存在一个错误,其中 name 不是驱动程序中 getLastGeneratedValue 的参数,因此您必须直接调用连接。

$adapter->getDriver()->getConnection()->getLastGeneratedValue

So that's what I found by looking through the code. Best solution is to make sure you have ZF2 updated, and use

这就是我通过查看代码发现的。最佳解决方案是确保您已更新 ZF2,并使用

$adapter->getDriver()->getLastGeneratedValue("sequence_name");

回答by Subey

In postgres you need setup SequenceFeature:

在 postgres 中你需要设置 SequenceFeature:

...
use Zend\Db\TableGateway\Feature;
...
public function setDbAdapter( Adapter $adapter ) {
   ...
   $this->featureSet = new Feature\FeatureSet();
   $this->featureSet->addFeature(new Feature\SequenceFeature('YOUR_FIELD_NAME','YOUR_SEQUENCE_NAME'));
   $this->initialize();
}

Now $this->tableGateway->getLastInsertValue();should work.

现在$this->tableGateway->getLastInsertValue();应该工作。

回答by Raj

Ok, I know the question is few month old, though it may be useful for those who are searching for a solution to this problem to get the last insert values the best way is to get the value from the adapter interface

好的,我知道这个问题已经有几个月了,尽管对于那些正在寻找解决此问题的方法以获取最后一个插入值的人来说可能有用,但最好的方法是从适配器接口获取值

do your insert //then
$id = $adapter->getDriver()->getLastGeneratedValue();

This worked well for me for mysql database

这对我的 mysql 数据库很有效

回答by Aurimas

In App\Model\AlbumTablewhich extends AbstractTableGatewayI get last id by $inserted_id = $this->lastInsertValue;

App\Model\AlbumTable哪个扩展中AbstractTableGateway我得到最后一个 id$inserted_id = $this->lastInsertValue;

as an example:

举个例子:

 if ($id == 0) {
            $this->insert($data);
            $inserted_id = $this->lastInsertValue;    
        } elseif ($this->getAlbum($id)) {
            $this->update(
                $data, array(
                'id' => $id,
                )
            );
        } else {
            throw new \Exception('Form id does not exist');
        }

回答by Tomasz Konopiński

There is another possibility to add FeatureSet to TableGateway.

还有另一种可能将 FeatureSet 添加到 TableGateway。

In Module.php you can define (table User for example)

在 Module.php 中,您可以定义(例如表 User)

            'UserTableGateway' => function ($sm) {
                $dbAdapter = $sm->get('Zend\Db\Adapter\Adapter');
                $resultSetPrototype = new ResultSet();
                $resultSetPrototype->setArrayObjectPrototype(new UserEntity());
                return new TableGateway('user', $dbAdapter, new Feature\SequenceFeature('user','user_id_seq'), $resultSetPrototype);

But there is a bug in SequenceFeature. Everything is ok, when you use public schema. When you have to use other schema you should use Zend\Db\Sql\TableIdentifier;

但是SequenceFeature有一个bug。当您使用公共架构时,一切正常。当你必须使用其他模式时,你应该使用 Zend\Db\Sql\TableIdentifier;

In this case Module.php should be:

在这种情况下 Module.php 应该是:

            'UserTableGateway' => function ($sm) {
                $dbAdapter = $sm->get('Zend\Db\Adapter\Adapter');
                $resultSetPrototype = new ResultSet();
                $resultSetPrototype->setArrayObjectPrototype(new UserEntity());
                return new TableGateway(new TableIdentifier('user','someSchema'), $dbAdapter, new Feature\SequenceFeature(new TableIdentifier('user','someSchema'),'user_id_seq'), $resultSetPrototype);

In this case will be an error with Postgresql query SELECT NEXTVAL, because Feature\SequenceFeature will use public schema instead of defined in first argument to prepare query

在这种情况下,Postgresql 查询 SELECT NEXTVAL 会出错,因为 Feature\SequenceFeature 将使用公共模式而不是在第一个参数中定义来准备查询

SELECT NEXTVAL('user_id_seq')

SELECT NEXTVAL('user_id_seq')

In this case sql query should be

在这种情况下 sql 查询应该是

SELECT NEXTVAL('someSchema'.'user_id_seq')

SELECT NEXTVAL('someSchema'.'user_id_seq')

回答by Name

This does not work with oracle oci8 because it is not implemented yet. See here: https://github.com/zendframework/zf2/blob/master/library/Zend/Db/Adapter/Driver/Oci8/Connection.php#L343

这不适用于 oracle oci8,因为它还没有实现。见这里:https: //github.com/zendframework/zf2/blob/master/library/Zend/Db/Adapter/Driver/Oci8/Connection.php#L343