Saving related models with a newly created parent ID in CakePHP
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.
精彩评论