Reputation: 2675
i'm trying to count the views of a post using cakephp but the saving is not happinging using this code
$this->autoRender=false;
$unformattedData = $this->GeneralNews->findById($id);
$data = $unformattedData['GeneralNews'];
$total = $data['views'];
$total = $total+1;
$this->GeneralNews->views =$total;
print_r($this->GeneralNews->views);
$this->GeneralNews->save($this->request->data);
the print_r show me the views that's already on the table adding to them +1 .. but it's never saved in the DB .. what is the problem ?
Upvotes: 1
Views: 1034
Reputation: 1701
Perhaps even easier:
$this->GeneralNews->id = $id;
$views = $this->GeneralNews->field('views');
$this->GeneralNews->saveField('views', $views + 1);
Hope this helps.
Upvotes: 1
Reputation: 1540
Try this code :
$unformattedData = $this->GeneralNews->findById($id);
$unformattedData['GeneralNews']['views'] = $unformattedData['GeneralNews']['views'] + 1;
$this->GeneralNews->save($unformattedData);
Upvotes: 4