Ninjakreborn Posted January 11, 2010 Share Posted January 11, 2010 I have been working on a site in Zend Framework. Basically there was another developer long ago who set up the basic inner workings of this code. I have just been building onto it. I was almost done and just getting ready to do some file handling and I ran into what appears to be a simple issue..I can't get the last mysql ID. Generally I would just call the mysql function to get that. Or I would run a Mysql Query to get it directly. Neither one of those methods work here. It seems it's not connected to the database unless actually using the class. So after failing to do that, I started doing some more research into the way you can handle databases in Zend Framework..obviously I found it in the documentation. Here is my code. <?php public function getTable() { if(null === $this->_table){ require_once APPLICATION_PATH . '/models/DbTable/Projects.php'; $this->_table = new Model_DbTable_Projects; } return $this->_table; } // Save a new record public function save(array $data) { $table = $this->getTable(); $fields = $table->info(Zend_Db_Table_Abstract::COLS); foreach($data as $field => $value){ if(!in_array($field, $fields)){ unset($data[$field]); } } $table->insert($data); $table->lastInsertId(); return $last_id; } ?> Here is the link were I found the documentation: http://framework.zend.com/manual/en/zend.db.html Here is what it says: 15.1.4.2. Retrieving a Generated Value Some RDBMS brands support auto-incrementing primary keys. A table defined this way generates a primary key value automatically during an INSERT of a new row. The return value of the insert() method is not the last inserted ID, because the table might not have an auto-incremented column. Instead, the return value is the number of rows affected (usually 1). If your table is defined with an auto-incrementing primary key, you can call the lastInsertId() method after the insert. This method returns the last value generated in the scope of the current database connection. Example 15.19. Using lastInsertId() for an Auto-Increment Key $db->insert('bugs', $data); // return the last value generated by an auto-increment column $id = $db->lastInsertId(); I followed that. Obviously I am not using DB class because he set it into Table. When I do what this recommends I get the following error via Ajax: <b>Fatal error</b>: Call to undefined method Model_DbTable_Projects::lastInsertId() in <b>D:\Inetpub\litmusbox\tiogahostportal\application\models\Projects.php</b> on line <b>27</b><br /> I have tried using the original Mysql method, I have tried writing a query for it, and I tried it like the documentation say's. That is all the documentation says about it. Anyone have any advice? Quote Link to comment https://forums.phpfreaks.com/topic/188084-get-last-mysql-id-using-zend-frameworks/ Share on other sites More sharing options...
Ninjakreborn Posted January 11, 2010 Author Share Posted January 11, 2010 The return value of insert and update return the last used Mysql ID. Problem solved. Quote Link to comment https://forums.phpfreaks.com/topic/188084-get-last-mysql-id-using-zend-frameworks/#findComment-993004 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.