0

I have two models: Action HABTM Image

I don't have a form 'cause I'm generating the Post data with code outside of cakephp. In the Post I'm sending this data to the ActionController to be saved:

data[Action][id] = 5e0ece37-bc8a-4bb9-a95d-1572dcfb4a96
data[Action][action_type_id] = 69c34b29-c247-11e1-b880-f46d04737d8a
data[Image][0][id] = 9d8c1ade-2b1b-48be-a4be-f9d3176601fc
data[Image][0][url] = file%3A%2F%2F%2Fmnt%2Fsdcard%2FDCIM%2FCamera%220120702_140539.jpg
data[Image][0][action_id] = 5e0ece37-bc8a-4bb9-a95d-1572dcfb4a96
data[Image][1][id] = 7cc3f6e4-7143-41ef-b98d-a766b8b1fffe
data[Image][1][url] = file%3A%2F%2F%2Fmnt%2Fsdcard%2FDCIM%2FCamera%220130702_140539.jpg
data[Image][1][action_id] = 5e0ece37-bc8a-4bb9-a95d-1572dcfb4a96

By now only the Action is saved when I call saveAll($this->data). I haven't been able to find an example where the two related models are saved the same time, but only one model and the record in the join table (actions_images). ¿Is there a way to do this with saveAll? ¿Or do I need to save the models separately?

eleonzx
  • 677
  • 10
  • 16

1 Answers1

0

It is possible its just the formatting of the array needs to be different.

This chapter of the cook book should help: Saving Related Model Data (HABTM)

Rob Forrest
  • 7,329
  • 7
  • 52
  • 69
  • thank's for answering, but on those examples only one side of the relation is inserted, together with a record on the join table to link with an already inserted record on the other related table. What I was trying to do was inserting both sides of the HABTM relation at the same time(of course including also the join table records that relates them) something like the examples for has many: http://book.cakephp.org/2.0/en/models/saving-your-data.html#saving-related-model-data-hasone-hasmany-belongsto (on those they save the master and the details records all at once) – eleonzx Jul 03 '12 at 23:28
  • Well, after all it works, but you must use a join model, not HABTM, and call saveAll from the actual join model (in this case "ActionsImage"), and it only works for saving just one relation at a time. At least it save all three related rows to the database in a single transaction. – eleonzx Oct 23 '12 at 22:56