tags:

views:

4576

answers:

7

Hi, In my controller

     function designpage() 
    {
       //to create a form Untitled
 $this->Form->saveField('name','Untitled Form');
     echo $this->Form->getLastInsertId();//here it works

    }

        function insertformname()
      { 

          echo $this->Form->getLastInsertId(); //this doesnt echo at all



      }

Please suggest me..

+1  A: 

You'll need to do an insert (or update, I believe) in order for getLastInsertId() to return a value. Could you paste more code?

If you're calling that function from another controller function, you might also be able to use $this->Form->id to get the value that you want.

inkedmn
In my homepage if i click designpage it by default will create a form entry ..After tat in my designpage i want to change my formname there i m using the function insertformname to update the name ...For this i need the id of it....
Aruna
Ack. Supposing that designpage() and insertformname() are two different Actions in a Controller, there will be no lastInsertId in the insertformname() Action since you haven't done any database inserts in this action. Each Action (read: page request) is self-containing, you can't get insertIds from a previous page request.
deceze
A: 

In Cake, the last insert id is automatically saved in the id property of the model. So if you just inserted a user via the User model, the last insert id could be accessed via $User->id

id - Value of the primary key ID of the record that this model is currently pointing to. Automatically set after database insertions.

Read more about model properties in the CakePHP API Docs: http://api.cakephp.org/class/model

Edit: I just realized that Model::getLastInsertID() is essentially the same thing as Model->id

After looking at your code more closely, it's hard to tell exactly what you're doing with the different functions and where they exist in the grand scheme of things. This may actually be more of a scope issue. Are you trying to access the last insert id in two different requests?

Can you explain the flow of your application and how it relates to your problem?

Mike B
inside my insertformname ,i need to update the formname with the last inserted form Id.....
Aruna
A: 

thanks for this tag "getLastInsertId();" i was looking for it :)

use it in this way:

$this->Form->save($this->data);

echo $this->Form->getLastInsertId(); //this will echo saved row id man

A: 

Is there any way to get the last insert id without doing a save/update? Model->id is null...

Hooman Ahmadi
+1  A: 
$this->Model->field('id', null, 'id DESC')
Robert
A: 

use just,

$this->id;

s razu
A: 

Try using this code in your model class (perhaps in AppModel):

function get_sql_insert_id() {
   $db =& ConnectionManager::getDataSource($this->useDbConfig);
   return $db->lastInsertId();
}

Caveat emptor: MySql's LAST_INSERT_ID() function only works on tables with an AUTO_INCREMENT field (otherwise it only returns 0). If your primary key does not have the AUTO_INCREMENT attribute, that might be the cause of your problems.

Rick