2016-04-23 22 views
0

隨着Zend\Db\Adapter\Driver\ResultInterface#getGeneratedValue()Zend\Db提供了一種簡單的方式來獲得最後的INSERT編輯條目的ID,例如:如何獲取ZF2中自定義PRIMARY KEY列的最後生成值?

$action = new Insert('my_table'); 
$action->values($data); 
$sql = new Sql($this->dbAdapter); 
$statement = $sql->prepareStatementForSqlObject($action); 
$result = $statement->execute(); 
$newId = $result->getGeneratedValue(); 

但似乎只是工作,如果PRIMARY KEY柱所說的「身份證」。如何檢索另一列上定義的PRIMARY KEYgeneratedValue

+0

的SQL您正在使用? – tasmaniski

+0

MySQL。但是由於數據庫抽象層,在這種情況下它並不重要。 – automatix

+0

Than getLastInsertValue();應該正常工作。 PostgreSQL在某些情況下有問題... – tasmaniski

回答

1

優選地使用Zend\Db\TableGateway\TableGateway提供方法getLastInsertValue()

use Zend\Db\TableGateway\TableGateway; 
$myTable = new TableGateway('my_table', $this->dbAdapter); 
$action = new Insert('my_table'); 
$action->values($data); 
$myTable->insertWith($action); 
$newId = $myTable->getLastInsertValue(); 
0
$this->dbAdapter->getDriver()->getLastGeneratedValue(); 
相關問題