开发者

How should data be formatted in CakePHP to save three levels of associations with saveAll()?

开发者 https://www.devze.com 2023-03-05 08:32 出处:网络
I\'m trying to build an \"Add an Event\" page.You can add multiple \"Schedules\" - each of which will have calculations run on the date(s) / repeat options you set to create \"Dates\".

I'm trying to build an "Add an Event" page. You can add multiple "Schedules" - each of which will have calculations run on the date(s) / repeat options you set to create "Dates".

-Event hasMany Schedule
-Schedule hasMany Date
-Schedule belongsTo Event
-Date belongsTo Sched开发者_高级运维ule

The question is - how does the data need to be formatted and/or how to the field names need to read in order to be able to saveAll() the data so Date(s) are also saved. I have it currently working so it saves the Event and the Schedule... but I can't get it to also save the Date(s).


UPDATE: See answer marked as 'answer'. This answer is out of date.


It appears it's not possible according to this stackoverflow answer.

ie - have to do it manually, not in the same "saveAll()".


Actually, since CakePHP 2.1, you can do it with saveAll(). Doesn't matter the deep level you have within your models. Here you can see the new features.

http://book.cakephp.org/2.0/en/appendices/new-features-in-cakephp-2-1.html#model-saveall-model-saveassociated-model-validateassociated


saveAll only works on models that are directly associated. From the CakePHP doc:

Saving related data with saveAll() will only work for directly associated models. If successful, last_insert_id()'s will be stored in the related models id field, i.e. $this->RelatedModel->id.

http://book.cakephp.org/view/1031/Saving-Your-Data

0

精彩评论

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