CakePHP updateAll()不工作
我使用的是updateAll()而不是教程所建议的CakePHP updateAll()不工作,php,mysql,cakephp,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.u
$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查询:将Smooch
用户
更新为用户
设置用户
状态
=ooactive其中用户
用户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));
}}
错误-
错误:SQLSTATE[23000]:完整性约束冲突:1062项“主”的重复项“0”
SQL查询:插入到Smooch
用户(状态
)值('inaaaactive'))
这会将其转换为INSERT语句,但不确定原因。好吧,没关系。文件上说:()
$fields数组接受SQL表达式。应使用DboSource::value()手动引用文字值。例如,如果您的一个模型方法正在调用updateAll(),您将执行以下操作:
$db=$this->getDataSource();
$value=$db->value($value,'string');
$this->updateAll(
数组('Baker.approved'=>true),
数组('Baker.created如文档所述,updateAll()不能像2.x中的save()那样使用。
您需要手动引用您的输入,因此它符合逻辑(错误消息说明您需要使用):
$this->User->updateAll(
数组('User.status'=>'ooactive'),
数组('User.User\u id'=>$User\u id)
);
$this->Bookcopy->updateAll(['Bookcopy.status'=>'AVAILABLE'],['Bookcopy.status'=>'REQUESTED']))
错误:SQLSTATE[42S22]:未找到列:1054“字段列表”中的未知列“可用”
SQL查询:更新practice\u bookchum
bookcopies
ASBookcopy
左连接practice\u bookchum
books\u Store
ASBook\u Store
Book\u Store id
=Book\u Store
id>)设置Bookcopy
状态
=在Bookcopy
状态下可用
=“已请求”
模型::updateAll(数组$fields,混合$conditions)
将$fields作为关联数组与值传递时出错
我做到了
我尝试了上面的方法,但它导致了更多的问题,所以我试着回到cakePHP官方教程所说的方法-$this->Post->id=$id;这就是我认为使它成为更新语句的神奇之处。我该如何设置这段代码($this->Post->id=$id;)如果我的主键是user_id?我将更新我的帖子以反映当前代码。你可以使用$this->post->primaryKey='field'将主键设置为其他字段;我不知道这是否适用于你。
$db = $this->getDataSource();
$value = $db->value($value, 'string');
$this->updateAll(
array('Baker.approved' => true),
array('Baker.created <=' => $value)
);
$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"