On Feb 14, 11:40 am, "admin_AT_loveyourdress.com" <[EMAIL PROTECTED]>
wrote:
> Before cake, whenever i save data into a row without specifying the
> id(auto incremental primary key). I cant get the id number back from
> mysql withouth doing a search. However, using cake's model->save, i
> get the model->id magically. Anyone actually knows how cake retrived
> the id key?
It doesn't as far as I know. Here's an example from the manual where
the id is retrieved after saving to save associated data
This uses the $this->Post->getLastInsertId(); function.
/app/controllers/posts_controller.php (partial)
function add()
{
if (!empty($this->data))
{
//We can save the Post data:
//it should be in $this->data['Post']
$this->Post->save($this->data);
//Now, we'll need to save the Comment data
//But first, we need to know the ID for the
//Post we just saved...
$post_id = $this->Post->getLastInsertId();
//Now we add this information to the save data
//and save the comment.
$this->data['Comment']['post_id'] = $post_id;
//Because our Post hasMany Comments, we can access
//the Comment model through the Post model:
$this->Post->Comment->save($this->data);
}
}
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Cake
PHP" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/cake-php?hl=en
-~----------~----~----~----~------~----~------~--~---