Question

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 ?

Was it helpful?

Solution

Try this code :

 $unformattedData = $this->GeneralNews->findById($id);
     $unformattedData['GeneralNews']['views'] = $unformattedData['GeneralNews']['views'] + 1;
     $this->GeneralNews->save($unformattedData);

OTHER TIPS

Perhaps even easier:

$this->GeneralNews->id = $id;
$views = $this->GeneralNews->field('views');
$this->GeneralNews->saveField('views', $views  + 1);

Hope this helps.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top