ZF2如何获得最后插入id值


ZF2 How to get last insert id value?

我坚持最后插入id与Zend框架2,我放弃了这个…

有尝试过的组合:

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被插入到表中,但是每一行(除了第一行,它给出int "1")返回null。请不要告诉我,这么大的框架不提供获得最后插入id值的可能性!

我是这样用的:

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

我知道这是一段时间了,但在这个特殊的问题上花费了大量的时间后,我想发布解决方案,为未来遇到同样问题的谷歌人。

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

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

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

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

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

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

在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();
}

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

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

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

mysql数据库

在扩展AbstractTableGatewayApp'Model'AlbumTable中,我通过$inserted_id = $this->lastInsertValue;获得最后一个id

为例:

 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');
        }

这不能在oracle oci8上工作,因为它还没有实现。详见:https://github.com/zendframework/zf2/blob/master/library/Zend/Db/Adapter/Driver/Oci8/Connection.php#L343

还可以将FeatureSet添加到tableggateway中。

在Module.php中你可以定义(例如table 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);

但是在SequenceFeature中有一个bug。当您使用公共模式时,一切都没问题。当你必须使用其他模式时,你应该使用Zend'Db'Sql'TableIdentifier;

在这个例子中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);

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

选择NEXTVAL (user_id_seq)

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

选择NEXTVAL (someSchema。user_id_seq)