我遇到问题,下面的代码根本无法处理。它不会保存或删除。我得到了一个成功的消息但没有任何东西会保存,也不会删除我认为该方法是一个保留关键字,但后来我尝试重命名它仍然无法正常工作。任何见解都会受到极大的赞赏,因为这让我很难过。
function approve() {
$this->set('title', 'Approve Article');
if ($this->RequestHandler->isPost() || $this->RequestHandler->isPut()) {
if (empty($this->data['Article']['id'])) {
$this->Session->setFlash('No article was passed.', 'message_error');
} else {
$this->Article->set($this->data);
if ($this->Article->validates()) {
if ($this->data['Article']['approved']) {
$this->data['Article']['content'] = $this->Safe->filter($this->data['Article']['content']);
$role = $this->Auth->user('role');
if ($role == 'Admin')
$this->Article->set('updated', strtotime($this->data['Article']['updated']));
else
$this->Article->set('updated', time());
$this->Article->set('updated', time());
$this->Article->save();
// Status
$status['Status']['type'] = 'Gain';
$status['Status']['amount'] = 20;
$status['Status']['created'] = time();
$this->Status->add($this->data['Article']['account_id'], $status);
$this->Session->setFlash('The article was approved and status was added.', 'message_success');
} else {
$this->Article->delete($this->data['Article']['id']);
$this->Session->setFlash('The article was unapproved and deleted.', 'message_error');
}
} else {
$this->Session->setFlash('Form Errors', 'message_error');
}
}
}
$unapproved_articles = $this->Article->find('count', array('conditions' => array('Article.approved =' => 0)));
if ($unapproved_articles == 0) {
$this->Session->setFlash('There are no unapproved articles.', 'message_success');
} else {
$article = $this->Article->find('first', array('order' => array('Article.created DESC')));
$article['Article']['updated'] = date('d M Y', $article['Article']['updated']);
$this->set('article', $article);
$this->set('categories', $this->Category->dropdown());
$this->set('accounts', $this->Account->dropdown());
}
}
答案 0 :(得分:0)
我认为你需要
$this->Article->save($this->data)
您还应检查是否存在无法在模型代码中保存的回调。确保在app / config / core.php文件中打开debug到level 2,并查看在重定向之前获得输出的sql查询。
答案 1 :(得分:0)
除非您提供$this->Article->save($this->data,array('validate' => false))
您可以试试这些tips
试试这个
if ($this->data['Article']['approved']) {
$this->data['Article']['content'] = $this->Safe->filter($this->data['Article']['content']);
$role = $this->Auth->user('role');
if ($role == 'Admin')
$this->data['Article']['updated']=strtotime($this->data['Article']['updated']);
else
$this->data['Article']['updated']=time();
if($this->Article->save($this->data)){
// Status
$status['Status']['type'] = 'Gain';
$status['Status']['amount'] = 20;
$status['Status']['created'] = time();
$this->Status->add($this->data['Article']['account_id'], $status);
$this->Session->setFlash('The article was approved and status was added.', 'message_success');
}
else{
$this->Session->setFlash('Form Errors', 'message_error');
}
} else {
$this->Article->delete($this->data['Article']['id']);
$this->Session->setFlash('The article was unapproved and deleted.', 'message_error');
}