2015-08-27 51 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"