> Ok, i think i found the origin of the problem, but not the reason or
> the solution.
>
> As i said this is an online car selling site, so every time the
> someone is viewing a car i was updating an database field calld 'view'
> to increment the number of views for this car. To do this i was using,
> in the view method of the controller :
>
> $this->Post->saveField('view', $view+1);
>
> I tried to remove this line and im not getting anymore those empty
> posts, but as i said i still have to understand why this was
> happening.
You hadn't set the model's id, so a new record is being inserted.
$this->Post->id = $id;
$this->saveField(...);
Another (I think better) way:
$this->Post->updateAll(
array('Post.view' => 'Post.view + 1'),
array('Post.id' => $id)
);
Better still:
$this->Post->increment();
function increment($id = null)
{
if (!$id) $id = $this->id;
$this->updateAll(
array('Post.view' => 'Post.view + 1'),
array('Post.id' => $id)
);
}
Even better again would be to put the updateAll() in afterFind(), so
it's done automatically. But I've yet to figure out a decent way to
differentiate between a find('first') from find('all'), as $results
are unfortunately formatted the same in either case.
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:
Post a Comment