Search code examples
zend-frameworkzend-db

ZF last inserted id


does this code gives me the last inserted id of the record, even on a heavy load page?

  db = Zend_Db_Table::getDefaultAdapter();
  $db->insert($this->_name, $fields_values);
  $idAddress = $db->lastInsertId($this->_name);

Regards Andrea


Solution

  • public function saveData(array $data) {
        $this->_setName('user');
    
        // this will return all field name for user table in $field variable
        $fields = $this->info($this->getConstCol());
    
        // By this foreach loop we will set data in $data array
        foreach ($data as $field => $value) 
        {
            if (!in_array($field, $fields)) 
            {
                unset($data[$field]);
            }
        }
        // It will call insert function of (Zend_Db_Table_Abstract Parent of Zend_Db_Table)
        // It will return $pkData which is primary key for user table
    
        return $this->insert($data); // It will return last insert ID
     }