3

I am trying to switch from mysql to Zend / TableGateway class.

I wonder if there is a method, similar to mysql_insert_id, to get the autoincremented id of the last inserted row.

Googling around, I found some answers, pointing to the lastInsertId() of the DB-Adapter, however, this method seems no longer available in ZF2. Also: the return value of the insert method is returning a boolean value, not the last id as in ZF1.

Currently I am using a ugly workaround. Please see code below.

Is there a better / recommended way of getting the Id?

table_1 {
    id: integer, primary key, autoincremented
    content: text
}

table_2 {
    table_1_id: integer
    other_content: text
}

// using mysql
$sql = "INSERT INTO table_1 (content) VALUES ('some text')";
$result = mysql_query($sql);
// check omitted

$id = mysql_insert_id();
$sql = "INSERT INTO table_2 (table_1_id, other_content) VALUES ($id, 'other text')";
$result = mysql_query($sql);


// using Zend - this is the code, I am currently using
//*************************************************************
// get_last_insert_id emulation; works only if content is unique
private function getLastInsertId($tableGateway, $content) {
    $entries = $tableGateway->select(array('content' => $content));
    foreach ($entries as $entry) {
        return $entry->id;
    }

    return null;
}
// another option: get highest ID, must be the last inserted
private function getLastInsertId($tableGateway) {
    // needs a method like 'getRowWithHighestId'
}
//*************************************************************

// ...
table_1_entry = new Table1Entry('some text');
$tableGateway->insert($hydrator->extract($table_1_entry));

//*************************************************************
// using the workaround:
$id = getLastInsertId($tableGateway, $table_1_entry->content);
// 
// there MUST be some Zend method to get this last id.
//*************************************************************

table_1_entry = new Table1Entry('other text', $id);
$tableGateway->insert($hydrator->extract($table_2_entry));
GingerHead
  • 8,130
  • 15
  • 59
  • 93
Gisela
  • 1,194
  • 2
  • 16
  • 30

2 Answers2

9

Use magic property $tableGateway->lastInsertValue to get the Id

$id = $tableGateway->lastInsertValue;

This property is set when executing insert data

source

$this->lastInsertValue = $this->adapter->getDriver()->getConnection()->getLastGeneratedValue();
Elvan
  • 621
  • 3
  • 4
  • 2
    It's worth noting there's also an accessor "getLastInsertValue()" which returns the same value. Worth using so you can mock the response and make your class more testable. – Dan Apr 26 '13 at 10:36
0
`enter code here  `$data_add=array(
                'line_1'        =>  $Data1->line_1,
                'line_2'        =>  $Data1->line_2,
                'line_3'        =>  $Data1->line_3,
                'city_id'       =>  $Data1->city_id,
                'state_id'      =>  $Data1->state_id,
                'country_id'    =>  1,
                'is_active'     =>  '1',
        );
            $adapter = $this->tableGateway->getAdapter();
            $otherTable = new TableGateway('address', $adapter);
            $otherTable->insert($data_add);
            $lastInsertValue= $adapter->getDriver()->getConnection()->getLastGeneratedValue();
            print_r('lastInsertId1 :'.$lastInsertValue);
            error_log($lastInsertValue);
        die();