Jump to content


Photo

Get last Mysql ID using Zend Frameworks


  • Please log in to reply
1 reply to this topic

#1 Ninjakreborn

Ninjakreborn

    Information Technology Specialist

  • Members
  • PipPipPip
  • 3,917 posts
  • Age:29

Posted 11 January 2010 - 01:37 PM

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.zen...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?

------

Business Website: http://www.infotechnologist.biz

Personal Website: http://www.joyelpuryear.com

Blog Site: http://ninjakreborn.blogspot.com/
Services: Web development, application development, mobile development, and custom development. All services listed on my website.


#2 Ninjakreborn

Ninjakreborn

    Information Technology Specialist

  • Members
  • PipPipPip
  • 3,917 posts
  • Age:29

Posted 11 January 2010 - 02:28 PM

The return value of insert and update return the last used Mysql ID.
Problem solved.

------

Business Website: http://www.infotechnologist.biz

Personal Website: http://www.joyelpuryear.com

Blog Site: http://ninjakreborn.blogspot.com/
Services: Web development, application development, mobile development, and custom development. All services listed on my website.





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

Cheap Linux VPS from $5
SSD Storage, 30 day Guarantee
1 TB of BW, 100% Network Uptime

AlphaBit.com