Re: A better way to save to HABTM join table meta data?

2009-03-05 Thread Omi

Thanks for the reply mscdex

I think if you do it the way I posted (put the field in Tag) you don't
need bindModel at all. You can also use save() and not saveAll().

On Mar 5, 2:28 am, mscdex  wrote:
> On Mar 4, 6:52 pm, Omi  wrote:
>
> > The correct way is to set
> > $this->data['Tag'][0] = array(
> >                                            'tag_id' => $foo,
> >                                            'tagger_id' => $bar
> >                                          );
>
> This is kinda on the right track. Here's an example of what I used for
> a Movies sample project:
>
> views/movies/add.ctp:
>  echo $form->create('Movie');
> echo $form->input('Movie.name');
> echo $form->input('MoviesUser.0.user_id');
> echo $form->input('MoviesUser.0.ranking');
> echo $form->end('Submit');
> ?>
>
> controllers/movies_controller.ctp:
>         function add() {
>                 if (!empty($this->data)) {
>                         $this->Movie->create();
>                         
> $this->Movie->bindModel(array('hasMany'=>array('MoviesUser')),
> false);
>                         if ($this->Movie->saveAll($this->data)) {
>                                 $this->Session->setFlash(__('The Movie has 
> been saved', true));
>                                 $this->redirect(array('action'=>'index'));
>                         } else {
>                                 $this->Session->setFlash(__('The Movie could 
> not be saved. Please,
> try again.', true));
>                         }
>                 }
>                 $users = $this->Movie->User->find('list');
>                 $this->set(compact('users'));
>         }
>
> IIRC the key was to do a bindModel with the name of the join relation
> name as a hasMany association before saving.
>
> Hope that helps some.

--~--~-~--~~~---~--~~
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+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/cake-php?hl=en
-~--~~~~--~~--~--~---



Re: A better way to save to HABTM join table meta data?

2009-03-04 Thread mscdex

On Mar 4, 6:52 pm, Omi  wrote:
> The correct way is to set
> $this->data['Tag'][0] = array(
>                                            'tag_id' => $foo,
>                                            'tagger_id' => $bar
>                                          );

This is kinda on the right track. Here's an example of what I used for
a Movies sample project:

views/movies/add.ctp:
create('Movie');
echo $form->input('Movie.name');
echo $form->input('MoviesUser.0.user_id');
echo $form->input('MoviesUser.0.ranking');
echo $form->end('Submit');
?>

controllers/movies_controller.ctp:
function add() {
if (!empty($this->data)) {
$this->Movie->create();

$this->Movie->bindModel(array('hasMany'=>array('MoviesUser')),
false);
if ($this->Movie->saveAll($this->data)) {
$this->Session->setFlash(__('The Movie has been 
saved', true));
$this->redirect(array('action'=>'index'));
} else {
$this->Session->setFlash(__('The Movie could 
not be saved. Please,
try again.', true));
}
}
$users = $this->Movie->User->find('list');
$this->set(compact('users'));
}

IIRC the key was to do a bindModel with the name of the join relation
name as a hasMany association before saving.

Hope that helps some.
--~--~-~--~~~---~--~~
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+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/cake-php?hl=en
-~--~~~~--~~--~--~---



Re: A better way to save to HABTM join table meta data?

2009-03-04 Thread Omi
'RecipiesTag.user_id' => $this->data['User']
['id']

should be
'RecipiesTag.recipe_id' => $this->data['Recipe']
['id']

On Mar 4, 10:30 pm, Omi  wrote:
> I've searched through the groups and web, but I can't find any mention
> of SAVING to the model, only reading from it.
>
> Some example Models:
>
> Recipes HABTM Tags
> The "recipies_tags" table has an extra column which is "tagger_id"
>
> I have tried setting the value in $this->data before the save but this
> doesn't propagate back to the DB - the HABTM saves with the third
> column empty
> (I tried:
>  $this->data['RecipiesTag']['tagger_id']
>  $this->data['Tag']['RecipiesTag']['tagger_id']
>  $this->data['Recipie']['RecipiesTag']['tagger_id']
>  - are there any more locations I should try?)
>
> I did find a way of saving to the third column, but it seems pretty
> hacky:
>
> $recipiestag = $this->Tag->RecipiesTag->find('first', array
> ('conditions' => array(
>                                                                               
>                                                                               
>                                             'RecipiesTag.tag_id' => 
> $this->data['Tag']
> ['tag_id'],
>                                                                               
>                                                                               
>                                             'RecipiesTag.user_id' => 
> $this->data['User']
> ['id']
>                                                                               
>                                                                               
>                                     )
>                                                                               
>                                                                           )
>                                                                               
>                                           );
>
> $this->Tag->RecipiesTag->id = $recipiestag ['RecipiesTag']['id'];
> $this->Tag->RecipiesTag->saveField('tagger_id', $user_id);
>
> Is there a better way of doing this?
--~--~-~--~~~---~--~~
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+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/cake-php?hl=en
-~--~~~~--~~--~--~---



Re: A better way to save to HABTM join table meta data?

2009-03-04 Thread Omi
There is!

The correct way is to set
$this->data['Tag'][0] = array(
   'tag_id' => $foo,
   'tagger_id' => $bar
 );

Does this seem unintuitive to anyone else?
I think updating the CakePHP Book with saving and retrieving join
table data would be very helpful, as this information is hard to find

On Mar 4, 10:30 pm, Omi  wrote:
> I've searched through the groups and web, but I can't find any mention
> of SAVING to the model, only reading from it.
>
> Some example Models:
>
> Recipes HABTM Tags
> The "recipies_tags" table has an extra column which is "tagger_id"
>
> I have tried setting the value in $this->data before the save but this
> doesn't propagate back to the DB - the HABTM saves with the third
> column empty
> (I tried:
>  $this->data['RecipiesTag']['tagger_id']
>  $this->data['Tag']['RecipiesTag']['tagger_id']
>  $this->data['Recipie']['RecipiesTag']['tagger_id']
>  - are there any more locations I should try?)
>
> I did find a way of saving to the third column, but it seems pretty
> hacky:
>
> $recipiestag = $this->Tag->RecipiesTag->find('first', array
> ('conditions' => array(
>                                                                               
>                                                                               
>                                             'RecipiesTag.tag_id' => 
> $this->data['Tag']
> ['tag_id'],
>                                                                               
>                                                                               
>                                             'RecipiesTag.user_id' => 
> $this->data['User']
> ['id']
>                                                                               
>                                                                               
>                                     )
>                                                                               
>                                                                           )
>                                                                               
>                                           );
>
> $this->Tag->RecipiesTag->id = $recipiestag ['RecipiesTag']['id'];
> $this->Tag->RecipiesTag->saveField('tagger_id', $user_id);
>
> Is there a better way of doing this?
--~--~-~--~~~---~--~~
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+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/cake-php?hl=en
-~--~~~~--~~--~--~---



A better way to save to HABTM join table meta data?

2009-03-04 Thread Omi

I've searched through the groups and web, but I can't find any mention
of SAVING to the model, only reading from it.

Some example Models:

Recipes HABTM Tags
The "recipies_tags" table has an extra column which is "tagger_id"

I have tried setting the value in $this->data before the save but this
doesn't propagate back to the DB - the HABTM saves with the third
column empty
(I tried:
 $this->data['RecipiesTag']['tagger_id']
 $this->data['Tag']['RecipiesTag']['tagger_id']
 $this->data['Recipie']['RecipiesTag']['tagger_id']
 - are there any more locations I should try?)

I did find a way of saving to the third column, but it seems pretty
hacky:

$recipiestag = $this->Tag->RecipiesTag->find('first', array
('conditions' => array(


'RecipiesTag.tag_id' => 
$this->data['Tag']
['tag_id'],


'RecipiesTag.user_id' => 
$this->data['User']
['id']


)

)

);

$this->Tag->RecipiesTag->id = $recipiestag ['RecipiesTag']['id'];
$this->Tag->RecipiesTag->saveField('tagger_id', $user_id);

Is there a better way of doing this?

--~--~-~--~~~---~--~~
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+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/cake-php?hl=en
-~--~~~~--~~--~--~---