This should work
function view($id = null) {
if (!$id) {
$this->Session->setFlash(__('Invalid Item.', true));
$this->redirect(array('action'=>'index'));
}
$this->Item->recursive = 2;
$item = $this->Item->read(null, $id)
$this->Item->saveField('views', $item['Item']['views']
+ 1, false);
$this->set(compact('item'));
}
On Wed, Feb 18, 2009 at 8:34 PM, NineBlindEyes <[email protected]> wrote:
>
> Ok so here is my problem. I have to have a record of how many times a
> user views a page. I simply want to add 1 to the view every time the
> view page for the item is loaded. It seems to be updating the views
> correctly, but it also adds to new rows in the database.
>
> Here is my code:
>
> function view($id = null) {
> if (!$id) {
> $this->Session->setFlash(__('Invalid Item.', true));
> $this->redirect(array('action'=>'index'));
> }
>
> //Add 1 to view
> $this->Item->recursive = -1;
> $item = $this->Item->read(null, $id);
> $viewnum = $item['Item']['views']+1;
> $this->data['Item'] = array('id'=>$id,'views'=>$viewnum);
>
> //Save new view #s
> $this->Item->save($this->data,false,array('views'));
>
> $this->Item->recursive = 2;
> $this->set('item', $this->Item->read(null, $id));
> }
>
> >
>
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"CakePHP" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/cake-php?hl=en
-~----------~----~----~----~------~----~------~--~---