Jump to content

Get last Mysql ID using Zend Frameworks


Ninjakreborn

Recommended Posts

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?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.