2013-12-15 03:36
浏览 24

Cakephp博客教程 - 编辑例程$ this-> Post-> id = $ id;

In the cakephp blog tutorial in the Edit Post edit() action section


I can't understand what this code does $this->Post->id = $id;

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • doude4924 2013-12-15 04:42

    Your url would be something like /posts/edit/1, where $id = 1 in this case. What that line does is set the id for the current record that the Post->save() call will affect.

    打赏 评论
  • duan6161 2013-12-15 04:48

    What this code does is set the value of the Post id to the $id parameter, passed in the URL. The Post instance is then saved with the data entered through the form. This code is executed when the page is called on form submission.

    It might be easier to understand with an example. Let's say you get the page to edit post number 3, /posts/edit/3. You fill out the form and submit. The same URL is accessed, but by POST this time. The controller now has all the data entered through the form. All it needs is the post id, which it gets from the parameter, $id. With all that, we can now save the post to the DB.

    打赏 评论

相关推荐 更多相似问题