lastInsertId() public method

Get the last auto-incremented insert ID from the database.
public lastInsertId ( string $table = null, string $col = null ) : integer
$table string The table name on which the auto-increment occurred.
$col string The name of the auto-increment column.
return integer The last auto-increment ID value inserted to the database.
コード例 #1
0
ファイル: Model.php プロジェクト: kalkin/solarphp
 /**
  * 
  * Inserts one row to the model table and deletes cache entries.
  * 
  * @param array $data The row data to insert.
  * 
  * @return int|bool On success, the last inserted ID if there is an
  * auto-increment column on the model (otherwise boolean true). On failure
  * an exception from PDO bubbles up.
  * 
  * @throws Solar_Sql_Exception on failure of any sort.
  * 
  * @see Solar_Sql_Model_Cache::deleteAll()
  * 
  */
 public function insert($data)
 {
     if (!is_array($data)) {
         throw $this->_exception('ERR_DATA_NOT_ARRAY', array('method' => 'insert'));
     }
     // reset affected rows
     $this->_affected_rows;
     // remove non-existent table columns from the data
     foreach ($data as $key => $val) {
         if (empty($this->_table_cols[$key])) {
             unset($data[$key]);
             // not in the table, so no need to check for autoinc
             continue;
         }
         // remove empty autoinc columns to soothe postgres, which won't
         // take explicit NULLs in SERIAL cols.
         if ($this->_table_cols[$key]['autoinc'] && empty($val)) {
             unset($data[$key]);
         }
     }
     // perform the insert and track affected rows
     $this->_affected_rows = $this->_sql->insert($this->_table_name, $data);
     // does the table have an autoincrement column?
     $autoinc = null;
     foreach ($this->_table_cols as $name => $info) {
         if ($info['autoinc']) {
             $autoinc = $name;
             break;
         }
     }
     // return the last insert id, or just "true" ?
     if ($autoinc) {
         $id = $this->_sql->lastInsertId($this->_table_name, $autoinc);
     }
     // clear the cache for this model and related models
     $this->_cache->deleteAll();
     if ($autoinc) {
         return $id;
     } else {
         return true;
     }
 }