CakePHP updateAll()不起作用

时间:2015-08-27 20:35:41

标签: php mysql 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 ));
    }
}

我收到此错误 -

  

错误:SQLSTATE [42S22]:找不到列:1054'字段列表'中的未知列'ooactive'   SQL查询:更新Smoochusers AS User设置Userstatus = ooactive WHERE Useruser_id = 3 < / p>

我不知道为什么我会收到这个错误,任何帮助都会很棒,谢谢

修改

当前代码,我决定按照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));
    }}

错误 -

  

错误:SQLSTATE [23000]:完整性约束违规:1062键'PRIMARY'重复输入'0'   SQL查询:INSERT INTO Smoochusersstatus)VALUES('inaaaaaaactive')

这会把它变成INSERT语句,不知道为什么。

3 个答案:

答案 0 :(得分:0)

嗯,没关系。文档说:(http://book.cakephp.org/2.0/en/models/saving-your-data.html#model-updateall-array-fields-mixed-conditions

  

$ fields数组接受SQL表达式。应使用DboSource :: value()手动引用文字值。例如,如果您的某个模型方法正在调用updateAll(),那么您将执行以下操作:

private class DownloadFilesTask extends AsyncTask<URL, Integer, Long> {

     protected Long doInBackground(URL... urls) {

        ServerLoader.getSharedInstance().get(url, 
            new new AsyncCallback<GetResult<Long>>() {

            @Override
            public void onFailure(Exception e) {
            }

            @Override
            public void onSuccess(GetResult<Long> result) {
              Long myResult = result.getValue();
            }

        );

        // return ??; How do I make the return?
     }

 }

因此,使用$db = $this->getDataSource(); $value = $db->value($value, 'string'); $this->updateAll( array('Baker.approved' => true), array('Baker.created <=' => $value) ); 来逃避状态。

答案 1 :(得分:0)

如文档所述,updateAll()不能像2.x中的save()一样使用。 您需要手动引用输入,因此逻辑上(并且错误消息指出您需要使用):

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

答案 2 :(得分:0)

$ this-&gt; Bookcopy-&gt; updateAll([&#39; Bookcopy.status&#39; =&gt;&#39; AVAILABLE&#39;],[&#39; Bookcopy.status&#39; = &GT;&#39; REQUESTED&#39;])

错误: SQLSTATE [42S22]:未找到列:1054未知列&#39;可用&#39;在&#39;字段列表&#39;

SQL查询:更新practice_bookchumbookcopies AS Bookcopy LEFT JOIN practice_bookchumbooks_stores AS Book_Store ON(Bookcopybook_store_id = Book_Storeid)SET Bookcopystatus = AVAILABLE WHERE Bookcopy。{{1 }} =&#39;请求&#39;

Model :: updateAll(array $ fields,mixed $ conditions)

当我将$ fields作为关联数组传递给值

时,会发生

错误

  

我做到了....

status