>
> I have two models, Measure and Article, with an habtm relationship. I
> want to add a measure and a related article at the same time. I've
> tried using this algorithm from another post (http://groups.google.com/
> group/cake-php/browse_thread/thread/863c403df2a2de6/3137f612e3ffdf68?
> lnk=gst&q=saving+data+to+multiple+models+habtm#3137f612e3ffdf68)
>
> Step 1 - Unbind the HABTM with the Article model
> Step 2 - Save Measure model data
> Step 3 - bind HABTM with Article model
> Step 4 - save data['Measure']['id'] = getLastInsertID <- for the
> automagic to happen!!!
> Step 5 - Save Article data
>
> 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.
>
> Here's my code from my measures_controller:
>
> function add() {
> if (!empty($this->data)) {
>
> $this->Measure->unbindModel(
> array('hasAndBelongsToMany'=>array('ArticlesMeasure')), true);
>
> $this->Measure->create();
> $this->Measure->save($this->data);
>
> $this->Measure->bindModel(
> array('hasMany'=>array('ArticlesMeasure')), true);
>
> $this->data['Measure']['id'] = $this->Measure->getLastInsertId
> ();
>
> $this->Measure->Article->create();
> $this->Measure->Article->save($this->data);
>
> $this->Session->setFlash(__('The Measure has been saved' . pr
> ($this->data), true));
> $this->redirect(array('action'=>'index', ));
> } else {
> $this->Session->setFlash(__('The Measure could not be saved.
> Please, try again.', true));
> }
> }
>
> What am I doing wrong?
>
You're associating Measure to ArticlesMeasure. It should be to
Article. Also, your join model should be named ArticlesMeasures,
actually. But you likely needn't have one, unless you're doing
something complex. Just ensure the table is named articles_measures.
Check your model associations:
Article
var $hasAndBelongsToMany = array('Measure');
Measure
var $hasAndBelongsToMany = array('Article');
If it's still not working do:
if (!empty($this->data)) {
debug($this->data); exit;
... to find out if the data is even correct.
--~--~---------~--~----~------------~-------~--~----~
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:
Post a Comment