2013-04-04 30 views
1

我试图使用CakePHP计算后的意见,但使用此代码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的告诉我这是已经在桌子上的意见,节省不happinging添加到他们+1 ..但它从来没有保存在数据库中..有什么问题?

回答

4

试试这个代码:

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

也许更容易:

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

希望这有助于。