[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 473
  • Last Modified:

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

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!
0
locke2005
Asked:
locke2005
2 Solutions
 
hernst42Commented:
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

0
 
jcullinsCommented:
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.
0
 
Beverley PortlockCommented:
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

0

Featured Post

[Webinar] Improve your customer journey

A positive customer journey is important in attracting and retaining business. To improve this experience, you can use Google Maps APIs to increase checkout conversions, boost user engagement, and optimize order fulfillment. Learn how in this webinar presented by Dito.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now