我使用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