gpt4 book ai didi

php - CakePHP updateAll() 不工作

转载 作者:行者123 更新时间:2023-11-29 02:54:25 31 4
gpt4 key购买 nike

我正在使用 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 语句,不知道为什么。

最佳答案

嗯,没关系。文档说:(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') 来转义状态。

关于php - CakePHP updateAll() 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32258958/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com