开发者

Saving related models with a newly created parent ID in CakePHP

开发者 https://www.devze.com 2023-03-26 20:58 出处:网络
I am sure there must be a simple way to do this, but I have been combing the manual and can\'t find it.

I am sure there must be a simple way to do this, but I have been combing the manual and can't find it.

A user creates a new "course" model and in the same form adds a dynamic number of "student" models. Each student belongs to one course. So I receive the data开发者_StackOverflow中文版 to the controller roughly like this:

Array
(
  [Course] => Array(name, etc. etc.),

  [Student] => Array(
    [0] => Array(student details, etc.),
    [1] => Array(student 2 details, etc.))
)

Once the controller receives this, I save the Course data to create the new course. I now have the course unique ID from the database...how do I easily save the student data, adding the course_id automatically to each Student, without having to loop through all of them and adding the course_id manually?

Thanks!


If your model relationships are setup correctly, you can use saveAll().

Check the book on Saving your Data.

0

精彩评论

暂无评论...
验证码 换一张
取 消