Sunday, February 28, 2010

Re: ClassRegistry('init') more than once returns same Model id

The create() method appears to do exactly what I want to do.

The book says it perfectly:
"When calling save in a loop, don't forget to call create()."

Previously, I have not had to call create because I have only been
saving a single instance of a Model in each of my controller methods.

Thank you for this tip!
-Greg

On Feb 28, 2:30 pm, John Andersen <j.andersen...@gmail.com> wrote:
> In my opinion, there should only be one instance of the model! You
> just have to use it to create many new records!
>
> Use the models create method to ensure that each record is saved as a
> new record. See the CakePHP book at:http://book.cakephp.org/view/75/Saving-Your-Data
>
> So, still in my opinion, you should change your code as follows:
> [code]
> $UserBadge = ClassRegistry::init('UserBadge');
> foreach ($new_badges as $new_badge){
>    if ($new_badge){
>       $UserBadge->create();
>       $UserBadge->set('user_id', $check_in["User"]["id"]);
>       $UserBadge->set('check_in_id', $check_in["CheckIn"]["id"]);
>       $UserBadge->set('badge_id', $new_badge);
>       $UserBadge->save();
>       $awarded_badges[]=$UserBadge;
>       unset($UserBadge);
>    }}
>
> [/code]
>
> But I may be wrong, that you are not just saving the records (badges)
> but that you want to store the entire model in the $awarded_badges
> array for every badge?? :)
>
> Please enlighten me, enjoy,
>    John
>
> On Feb 27, 6:12 pm, Greg Marra <greg.ma...@gmail.com> wrote:
>
>
>
> > Hello,
>
> > I have a site where I am using ClassRegistry('init') to create
> > multiple new objects inside of a component. I want to save each one
> > into the database. Unfortunately, it seems that each time I call
> > ClassRegistry('init') I appear to get the same instance of the Model.
> > The net result is that instead of having two or three objects in my
> > database, it gets overwritten and overwritten and I end up with just
> > the most-recently-saved one.
>
> > Here is my code:
> > foreach ($new_badges as $new_badge){
> >                         if ($new_badge){
> >                                 $UserBadge = ClassRegistry::init('UserBadge');
> >                                 $UserBadge->set('user_id', $check_in["User"]["id"]);
> >                                 $UserBadge->set('check_in_id', $check_in["CheckIn"]["id"]);
> >                                 $UserBadge->set('badge_id', $new_badge);
> >                                 $UserBadge->save();
> >                                 $awarded_badges[]=$UserBadge;
> >                                 unset($UserBadge);
> >                         }
> >                 }
>
> > Any idea why this isn't working as expected?

Check out the new CakePHP Questions site http://cakeqs.org and help others with their CakePHP related questions.

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: