lastInsertId() 공개 메소드

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.
public lastInsertId ( string $tableName = null, string $primaryKey = null ) : string
$tableName string OPTIONAL Name of table.
$primaryKey string OPTIONAL Name of primary key column.
리턴 string
예제 #1
0
 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
 }
예제 #2
0
 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";
 }