Monday, March 1, 2010

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

On Feb 27, 5: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?

To me your code is working as expected - becuase ClassRegistry::init
is supposed to do exactly what you've found.

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: