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值的可能性!?
7 个解决方案
#1
26
Here is what I use:
这是我使用的:
$data = array()// Your data to be saved;
$this->tableGateway->insert($data);
$id = $this->tableGateway->lastInsertValue;
#2
6
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");
#3
4
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();应该管用。
#4
4
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数据库很有用
#5
1
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;
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');
}
#6
0
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
#7
0
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中有一个错误。当您使用公共架构时,一切都很好。当你必须使用其他模式时,你应该使用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')
In this case sql query should be
在这种情况下应该是sql查询
SELECT NEXTVAL('someSchema'.'user_id_seq')
#1
26
Here is what I use:
这是我使用的:
$data = array()// Your data to be saved;
$this->tableGateway->insert($data);
$id = $this->tableGateway->lastInsertValue;
#2
6
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");
#3
4
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();应该管用。
#4
4
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数据库很有用
#5
1
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;
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');
}
#6
0
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
#7
0
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中有一个错误。当您使用公共架构时,一切都很好。当你必须使用其他模式时,你应该使用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')
In this case sql query should be
在这种情况下应该是sql查询
SELECT NEXTVAL('someSchema'.'user_id_seq')