last insert id with zend db table abstract
Asked Answered
V

5

26

variable $tablemodel in an instance of a model which extends Zend_Db_Table_Abstract, if i do

$tablemodel->insert($data)

to insert data. Is there any method or property to get last insert id?

regards

Varix answered 30/1, 2011 at 9:7 Comment(0)
T
33

try

$id = $tablemodel->insert($data);  
echo $id;
Tonguetied answered 30/1, 2011 at 9:11 Comment(2)
In my version of Zend insert returns an int, but it's the number of affected rows not the last inserted row!Viridity
Zend 2 does not return the id, it returns the affected amount of rows.Intracranial
S
32
$last_id = $tablemodel->getAdapter()->lastInsertId();
Sokol answered 29/8, 2011 at 4:14 Comment(0)
F
15

you can use lastInsertId Method

echo 'last inserted id: ' . $db->lastInsertId();
Flux answered 30/7, 2011 at 13:28 Comment(1)
Where does $db come from?Intracranial
T
2

use after insert query

$this->dbAdapter->getDriver()->getLastGeneratedValue();
Thermoluminescence answered 27/10, 2016 at 11:30 Comment(1)
Now THIS one works for a change. I use an auto incremented primary key 'id'. So if you have your adapter, get the driver and it should work. Also Zend-Db v2.9Neuron
I
1

$insert_id = $this->db->getLastId() worked for me

Inhospitality answered 14/5, 2013 at 19:21 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.