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!
locke2005Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.