Thanks Martin!

So i can put the method in the model class, but how would I call it
within a view?

This is the link I have that calls the ingredients function to remove
it from the group. If I put the method in the model, I can't call it
directly like I'm doing below can I?

Right now this works, but I'm open to a better way:

<?php echo $html->link(__('Remove', true),
                            array('controller'=>'ingredients',
                                     'action'=>'removeFromGroup',
                                     $ingredient['id'],
                                     $ingredientGroup
['IngredientGroup']['id']),
                           null, sprintf(__('Are you sure you want to
remove \'%s\' from this group?', true), $ingredient['name'])); ?>

Can I just put

$this->IngredientGroup->Ingredient->removeFromGroup( $ingredient
['id'], $ingredientGroup['IngredientGroup']['id'])

directly in the view somehow? That seems to go away from the MVC
methodology.

Thanks again!

Will
On Oct 17, 10:27 am, Martin Westin <martin.westin...@gmail.com> wrote:
> First the quickfix... hopefully.
>
> This will try to save a PHP-null value.
> $this->data['ingredient_group_id'] = null;
>
> Instead try to save an SQL-null value.
> $this->data['ingredient_group_id'] = 'null';
>
> Or even just a zero if the above doesn't work.
> $this->data['ingredient_group_id'] = '0';
>
> Now for a suggestion. For saving a single field Cake models have a
> special function... called saveField no less :)
>
> $this->Ingredient->id = $id; // this line if very very important
> $successful = $this->Ingredient->saveField
> ('ingredient_group_id','null'); // or 0
>
> And finally another suggestion. You don't have to call a method in one
> controller from another for this... Better to put the whole
> "removeFromGroup()" method into the Model and call it from wherever.
> You know you can call a model like: $this->IngredientGroup->Ingredient
> right?
>
> ... and by answering I am not implying I am a God of any kind ;)
>
> /Martin
>
> On Oct 17, 7:56 am, Will Poillion <lorew...@gmail.com> wrote:
>
> > Just trying to update a single field within a function called from its
> > associated model:
>
> > I'm calling 'removeFromGroup' in Ingredients controller from my
> > IngredientGroups controller.
>
> > I basically want to remove the ingredient from the group, so set the
> > ingredient_group_id to null.
>
> >         function removeFromGroup($id = null, $group_id = null) {
> >                 if (!$id) {
> >                         $this->Session->setFlash(__('Invalid id for 
> > Ingredient', true));
> >                         $this->redirect(array('controller'=> 
> > 'IngredientGroups',
> > 'action'=>'view', $group_id));
> >                 }
>
> >                 $this->data = $this->Ingredient->read(null, $id);
>
> >                 $this->data['ingredient_group_id'] = null;
> >                 $this->Ingredient->save($this->data);
> >                 $this->redirect(array('controller'=> 'IngredientGroups',
> > 'action'=>'view', $group_id));
>
> >         }
>
> > There's my function. It just won't save the ingredient_group_id to
> > null. Not sure what I'm doing wrong. Thanks!
--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---

Reply via email to