Mohamed Emad Hegab
Mohamed Emad Hegab

Reputation: 2675

cakePHP: saving data in one column

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

Answers (2)

toby1kenobi
toby1kenobi

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

Krishna
Krishna

Reputation: 1540

Try this code :

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

Upvotes: 4

Related Questions