我使用updateAll(),而不是什么教程建议CakePHP的updateAll()不工作
$this->Post->id = $id;
我没有ID列在我的表。当我运行这段代码 -
public function edit($user_id = null) {
if (!$user_id) {
throw new NotFoundException(__('Invalid post'));
}
$user_info = array('conditions' => array('User.user_ID' => $user_id));
$user = $this->User->find('all', $user_info);
if (!$user) {
throw new NotFoundException(__('Invalid User ID'));
}
if ($this->request->is('put')) {
$data = $this->request->input('json_decode');
//$this->set('new_user', $data);
$this->User->updateAll(
array('User.status' => 'ooactive'), //fields to update
array('User.user_id' => $user_id));
}
}
我得到这个错误 -
Error: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'ooactive' in 'field list' SQL Query: UPDATE
Smooch
.users
ASUser
SETUser
.status
= ooactive WHEREUser
.user_id
= 3
我不知道为什么我收到此错误的任何帮助将是真棒,谢谢
编辑:
当前代码,我决定按照CakePHP.org博客教程中显示的方式执行此操作 -
public function edit($user_id = null) {
if (!$user_id) {
throw new NotFoundException(__('Invalid post'));
}
$user_info = array('conditions' => array('User.user_ID' => $user_id));
$user = $this->User->find('all', $user_info);
if (!$user) {
throw new NotFoundException(__('Invalid User ID'));
}
if ($this->request->is('put')) {
$this->User->user_id = "user_id";
$data = $this->request->input('json_decode');
if($this->User->save($data));
}}
错误 -
Error: SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry '0' for key 'PRIMARY' SQL Query: INSERT INTO
Smooch
.users
(status
) VALUES ('inaaaaaaactive')
这把它变成一个INSERT语句,不知道为什么。
我试图上面,它导致更多的问题,所以我试着回到官方cakePHP教程说做到这一点 - $ this-> Post-> id = $ id;这是我认为魔术发生的地方,使它成为更新声明。如果我的主键是user_id,我将如何设置此代码($ this-> Post-> id = $ id;)?我会更新我的帖子以反映当前的代码。 – retroCheck
您可以使用$ this-> Post-> primaryKey ='field'将主键设置为其他字段;我不知道这是否适合你。 –
,不幸的是没有工作。 – retroCheck