Friday, January 2, 2009

Re: saving to multiple models in a habtm relationship

Brian's answer is spot on for a generic HABTM join table, but I am
confused by your original post in places.

> And the measure stuff saves fine, but the join table saves the new
> measure_id, but the article_id is always 0, and nothing is saved in
> the article table.

Are you adding a Measure record and instantly relating it to existing
Article records by selecting it from a multiple drop-down or checkbox
list in the form?

If so, why do you expect something to be written to the Article
model?

What should happen, by using HABTM in it's most basic guise (i.e.
without any extra trickery like you're trying), is a record should be
saved to the Measure model and the join table (articles_measures) will
automatically be updated with the new measure_id and the selected
article_id for each related article selected in the form.

For the data to be saved correctly it needs to be in the following
format ...

$this->data = Array
(
[Measure] => Array
(
[id] => 460
[field1] => value1
[field2] => value2
[field3] => value3
...
)

[Article] => Array
(
[Article] => Array
(
[0] => %article_id1%
[1] => %article_id2%
[2] => %article_id3%
)
)
)

Hope this helps.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "CakePHP" group.
To post to this group, send email to cake-php@googlegroups.com
To unsubscribe from this group, send email to cake-php+unsubscribe@googlegroups.com
For more options, visit this group at http://groups.google.com/group/cake-php?hl=en
-~----------~----~----~----~------~----~------~--~---

No comments: