Saturday, February 27, 2010

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

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: