当我成功入伍时(1)!数据库将数字保存为1,并且当我成功获得响应ENLISTED(1)时!数据库将数字保存为0。我很困惑,对我的问题感到困惑。 CakePHP有我不了解的特别之处吗?
public function enlist($user_id_main, $user_id_from) {
$this->render(false);
$this->loadModel('Enlist');
$enlist = $this->Enlist->getEnlist($user_id_main, $user_id_from);
if (!empty($enlist)){
dump($enlist);
dump($this->Enlist->isActiveSub($user_id_main, $user_id_from));
if ($enlist['active']) {
$enlist -> active = 0;
if($this->Enlist->save($enlist)) {
dump($enlist);
$this->Flash->success(__('Successfully UNENLISTED(1)!'));
}
} else {
$enlist -> active = 1;
if($this->Enlist->save($enlist)) {
dump($enlist);
$this->Flash->success(__('Successfully ENLISTED(1)!'));
}
}
} else {
$en = $this->Enlist->newEntity(['validate'=>'validationForEnlist']);
$en['user_id_main'] = $user_id_main;
$en['user_id_for'] = $user_id_from;
$en['permited'] = 1;
$en['active'] = 1;
if ($this->Enlist->save($en)){
$this->Flash->success(__('Successfully ENLISTED!'));
}
}
}
答案 0 :(得分:0)
我将在有条件后立即取消$ enlist上的活动索引:
if ($enlist['active']) {
unset($enlist['active']);
$enlist -> active = 0;
...
} else {
unset($enlist['active']);
$enlist -> active = 1;
...
}
我猜Cake很困惑,因为您同时拥有$enlist['active']
和$enlist->active
。如果这样不起作用,您可以尝试将active设置为数组,例如
$enlist['active'] = 0;
代替
$enlist -> active = 0;