Zend_Db_Table Autoincrement ID

February 4, 2009

The otherwise excellent Zend Framework manual unfortunately doesn’t mention how to get the auto-incremented ID of a Zend_Db_Table. Luckily this is pretty simple when we know that Zend_Db_Table uses the same Zend_Db adapter mechanism.

In your Zend_Db_Table_Abstract subclass, just override the insert method:

public function create(array $data)
{
  parent::insert($data);
  return parent::getDefaultAdapter()->lastInsertId();
}

Not sure if that’s the best way of doing it, but it works and makes sense.