简体   繁体   English

ZF2如何获取最后一次插入id值?

[英]ZF2 How to get last insert id value?

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. 值是插入表,但每行(除了第一个,给出int“1”)返回null。 Please don't tell me, that such a big framework does not give possibility to get last insert id value!? 请不要告诉我,这样一个大框架没有给出获得最后一个插入id值的可能性!?

Here is what I use: 这是我使用的:

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

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. 这适用于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.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");

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(); 现在$this->tableGateway->getLastInsertValue(); should work. 应该管用。

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数据库

In App\\Model\\AlbumTable which extends AbstractTableGateway I get last id by $inserted_id = $this->lastInsertValue; 在扩展AbstractTableGateway App\\Model\\AlbumTable ,我通过$inserted_id = $this->lastInsertValue;得到最后一个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');
        }

This does not work with oracle oci8 because it is not implemented yet. 这不适用于oracle oci8,因为它尚未实现。 See here: https://github.com/zendframework/zf2/blob/master/library/Zend/Db/Adapter/Driver/Oci8/Connection.php#L343 请看: https//github.com/zendframework/zf2/blob/master/library/Zend/Db/Adapter/Driver/Oci8/Connection.php#L343

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. 但是SequenceFeature中存在一个错误。 Everything is ok, when you use public schema. 当您使用公共架构时,一切都很好。 When you have to use other schema you should use Zend\\Db\\Sql\\TableIdentifier; 当你必须使用其他模式时,你应该使用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')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM