As a convention, on RDBMS brands that support sequences
(e.g. Oracle, PostgreSQL, DB2), this method forms the name of a sequence
from the arguments and returns the last id generated by that sequence.
On RDBMS brands that support IDENTITY/AUTOINCREMENT columns, this method
returns the last value generated for such a column, and the table name
argument is disregarded.
On RDBMS brands that don't support sequences, $tableName and $primaryKey
are ignored.
function testInsert() { $row = array('title' => 'News Item 3', 'subTitle' => 'Sub title 3', 'body' => 'This is body 1', 'date_created' => '2006-05-03 13:13:13'); $rows_affected = $this->_db->insert(self::TableName, $row); $last_insert_id = $this->_db->lastInsertId(); $this->assertEquals('3', (string) $last_insert_id); // correct id has been set }
protected function insert() { $bind = array(); foreach ($this->databaseControls as $control) { if (!$control instanceof Volcano_Component_Control_Database_ReadOnly) { $bind[$control->getDbField()] = $control->getDbValue(); } } try { $result = $this->db->insert($this->tableName, $bind); $this->primaryKeyValue = $this->db->lastInsertId(); } catch (Zend_Db_Exception $e) { $this->errors[] = $this->localizer->translate("Database Error: %1\$s", $e->getMessage()); return false; } foreach ($this->databaseControls as $control) { if ($control instanceof Volcano_Component_Control_Database_File || $control instanceof Volcano_Component_Control_File) { if (!$control->move($this->primaryKeyValue)) { $this->delete(); return false; } } } return "insert"; }