1

我正在尝试使用 cakephp 计算帖子的浏览量,但使用此代码无法节省

         $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);

print_r 向我展示了已经在表中添加的视图 +1 .. 但它从未保存在数据库中 .. 有什么问题?

4

2 回答 2

4

试试这个代码:

 $unformattedData = $this->GeneralNews->findById($id);
     $unformattedData['GeneralNews']['views'] = $unformattedData['GeneralNews']['views'] + 1;
     $this->GeneralNews->save($unformattedData);
于 2013-04-04T08:15:18.827 回答
1

也许更容易:

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

希望这可以帮助。

于 2013-04-04T08:38:52.003 回答