2015-08-27 50 views
1

我使用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 AS User SET User . status = ooactive WHERE User . 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語句,不知道爲什麼。

回答

0

嗯,沒關係。該文件說:(http://book.cakephp.org/2.0/en/models/saving-your-data.html#model-updateall-array-fields-mixed-conditions

The $fields array accepts SQL expressions. Literal values should be quoted manually using DboSource::value(). For example if one of your model methods was calling updateAll() you would do the following:

$db = $this->getDataSource(); 
$value = $db->value($value, 'string'); 
$this->updateAll(
    array('Baker.approved' => true), 
    array('Baker.created <=' => $value) 
); 

因此,使用$db->value('ooactive', 'string')逃跑狀態。

+0

我試圖上面,它導致更多的問題,所以我試着回到官方cakePHP教程說做到這一點 - $ this-> Post-> id = $ id;這是我認爲魔術發生的地方,使它成爲更新聲明。如果我的主鍵是user_id,我將如何設置此代碼($ this-> Post-> id = $ id;)?我會更新我的帖子以反映當前的代碼。 – retroCheck

+0

您可以使用$ this-> Post-> primaryKey ='field'將主鍵設置爲其他字段;我不知道這是否適合你。 –

+0

,不幸的是沒有工作。 – retroCheck

0

如文檔所述,updateAll()不能像2.x中的save()一樣使用。 您需要手動引用您的輸入,因此它在邏輯上(和錯誤消息指出您需要使用):

$this->User->updateAll( array('User.status' => '"ooactive"'), array('User.user_id' => $user_id) );

0

$這個 - > Bookcopy-> updateAll([「Bookcopy.status」 = '有空']>,[ 'Bookcopy.status'=> 'REQUESTED'])

錯誤: SQLSTATE [42S22]:柱未找到:1054未知列在 '字段列表'

'可用'

SQL查詢: UPDATE practice_bookchumbookcopies AS Bookcopy LEFT JOIN practice_bookchumbooks_stores AS Book_Store ON(Bookcopybook_store_id = Book_Storeid)SET Bookcopystatus =可用在哪裏Bookcopystatus = 'REQUESTED'

型號:: updateAll(數組$字段,混合$條件)當我通過$字段作爲準數組值發生

錯誤

I did it....

$db = $this->Bookcopy->getDataSource(); 
$value = 'AVAILABLE'; 
$value = $db->value($value, 'String'); 

$this->Bookcopy->updateAll(['status' => $value],['Bookcopy.status'=>'REQUESTED']) 

{data: "Borrow", status: "success"} 
data:"Borrow" 
status:"success"