Marko Čepo
Marko Čepo

Reputation: 315

postgresql RETURNING in zf2 table gateway

How would one add a RETURNING clause in insert via table gateway ?

INSERT INTO users (name, age) VALUES ('Liszt', 10) RETURNING id;

Upvotes: 1

Views: 223

Answers (2)

venca
venca

Reputation: 1222

if you want to get last insert id in postgresql when inserting into tablegateway you have to use SequenceFeature.

$myTable = new TableGateway('table_name', $dbAdapter, new Feature\SequenceFeature('primary_key', 'sequence_name'));
$id = $myTable->insert(array(/*your data*/));

Upvotes: 0

Nandakumar V
Nandakumar V

Reputation: 4635

$dataArray = array('name'=> 'Liszt','age' => 10);
$this->tableGateway->insert($dataArray);
$userId = $this->tableGateway->lastInsertValue; 

Another method is :

$userId = $this->tableGateway->getLastInsertValue();

Upvotes: 2

Related Questions