We help IT Professionals succeed at work.

In the CakePHP Manual, I do not understand a code!

locke2005
locke2005 asked
on
function add()
{
    if (!empty($this->data))
    {
        $this->Post->save($this->data);

        $post_id = $this->Post->getLastInsertId();

        $this->data['Comment']['post_id'] = $post_id;

        $this->Post->Comment->save($this->data);
    }
}

// Please, explain me this code!
Comment
Watch Question

Top Expert 2007

Commented:
In the manual is a commented version http://manual.cakephp.org/chapter/models
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);
 
    }
}

Open in new window

Commented:
Hi locke2005,

well here is the way i read this without knowing too much about what else is going on.

You are creating a function called add()

line 3: if(!empty($this->data)) says
if $this->data is not empty (meaning something is there) then

line 4: $this->Post->save($this->data);
this is where is saves $this->data with the $this->Post->save function

line 5: $post_id = $this->Post->getLastInsertId();
this is calling a function within $this->Post to get the ID of the get inserted post you just did on line 4

line6: $this->data['Comment']['post_id'] = $post_id;
this is setting this line to $post_id where it got the value was from line 5 with the getLastInsertId();

line7: $this->Post->Comment->save($this->data);
saving the comment (line6) with the $post_id;

hopefully this helps.
CERTIFIED EXPERT
Commented:
I'm no CAKE expert, but from what I do know this seems to be taking some data from a form, saving it to a database, noting the record id for the inserted recird and then saving this in an object called "comment" which is then saved to the database itself
function add()
{
    // If there is data to be worked on
    //
    if (!empty($this->data))
    {
 
        // Save the data in to the model (writes to database)
        //
        $this->Post->save($this->data);
 
 
        // Get the id code for the record that was inserted
        //
        $post_id = $this->Post->getLastInsertId();
 
 
        // Store this code against the "comment" object's id field thus identifying the
        // record that was just inserted as a comment
        //
        $this->data['Comment']['post_id'] = $post_id;
 
 
        // Write this data to the database table that deals with comments
        //
        $this->Post->Comment->save($this->data);
    }
}

Open in new window

Explore More ContentExplore courses, solutions, and other research materials related to this topic.