如何检查控制器中的特定字段值

时间:2011-12-19 04:31:07

标签: cakephp controller cakephp-1.3

我有评论功能,我在评论表中有“状态”字段 ..i想要检查此特定字段值是否等于0 或者不在控制器中..有人帮我请更正此代码

   function admin_publish ($id = null){
                  if (!$id) {
                        // set flash message
                        $this->Session->setFlash('Invalid id for Location','default',
array('class' => 'flash_bad'));
                        // redirect
                        $this->redirect(array('action'=>'admin_index'));
                }else{
               // if comment status field 0
        if($comments['Comment']['status']== null){
                // change status from 0 to 1
             $this->Comment->saveField('status',1);
                        // set flash message
                        $this->Session->setFlash('The Comment was successfully
Published.');
                } else {
                     $this->Comment->saveField('status', 0);
                        // set flash message
                        $this->Session->setFlash('The Comment could not be NotPublished.');
                }

                // redirect
                $this->redirect(array('action'=>'admin_index'));
        }
        } 

1 个答案:

答案 0 :(得分:0)

尝试:

$commentData = $this->Comment->findById($commentId, "Comment.status");
$commentStatus = $commentData["Comment"]["Status"];
if(empty($commentStatus)) {
   //its zero
}
else {
  //its not zero
}

希望有所帮助