php - 在 cakephp 3 中为相同数据插入新条目之前删除所有现有条目
问题描述
这是我的控制器 =>
当我使用 deleteAll() 时,我收到此错误 => 错误:SQLSTATE[21000]:基数违规:1241 操作数应包含 1 列
当我使用 delete() 我得到这个错误=>错误:未知方法是新的
public function addSolution($ids = null)
{
$user_id=$this->Auth->User('id');
$city_id=$this->Auth->User('city_id');
$location_id=$this->Auth->User('location_id');
$this->viewBuilder()->layout('super_admin_layout');
$id = $this->EncryptingDecrypting->decryptData($ids);
$customerProblem = $this->CustomerProblems->get($id, [
'contain' => []
]);
$cust_id = $customerProblem['customer_id'];
$order_no = $customerProblem['order_no'];
if ($this->request->is(['patch', 'post', 'put'])) {
$customerProblem = $this->CustomerProblems->patchEntity($customerProblem, $this->request->getData());
$search=$this->request->getData();
$solutions=$this->request->getData()['solution'];
$delete = $this->CustomerProblems->find()->where([
'customer_id' => $search['customer_id'],
'mobile_no' => $search['mobile_no'],
'order_no' => $search['order_no'],
'problem' => $search['problem'],
'resolve_status' => $search['resolve_status'],
'status' => $search['status']
]);
if($this->CustomerProblems->deleteAll($delete)){
foreach ($solutions as $solution) {
$customerProblem = $this->CustomerProblems->newEntity();
$customerProblem->city_id = $city_id;
$customerProblem->created_by = $user_id;
$customerProblem->customer_id = $this->request->getData()['customer_id'];
$customerProblem->mobile_no = $this->request->getData()['mobile_no'];
$customerProblem->order_no = $this->request->getData()['order_no'];
$customerProblem->problem = $this->request->getData()['problem'];
$customerProblem->resolve_status = $this->request->getData()['resolve_status'];
$customerProblem->status = $this->request->getData()['status'];
$customerProblem->solution = $solution;
$this->CustomerProblems->save($customerProblem);
}
$this->Flash->success(__('The customer problem has been saved.'));
return $this->redirect(['action' => 'index']);
}
$this->Flash->error(__('The customer problem could not be saved. Please, try again.'));
}
$customers=$this->CustomerProblems->Customers->find()->where(['Customers.id'=>$cust_id,'Customers.status'=>'Active']);
$customer=[];
foreach($customers as $data1){
$customer[]= ['value'=>$data1->id,'text'=>ucwords($data1->name)." (".$data1->username.")"];
}
$getAllSolution = $this->CustomerProblems->find()->where(['CustomerProblems.order_no'=>$order_no,'CustomerProblems.status'=>'Active'])->extract('solution');
$this->set(compact('customerProblem','customer','getAllSolution'));
}
解决方案
deleteAll
不需要删除实体列表,它需要一组条件并删除所有匹配的内容。
if($this->CustomerProblems->deleteAll([
'customer_id' => $search['customer_id'],
'mobile_no' => $search['mobile_no'],
'order_no' => $search['order_no'],
'problem' => $search['problem'],
'resolve_status' => $search['resolve_status'],
'status' => $search['status']
])) {
请注意,它返回受影响的行数,而不是真/假,因此您的if
逻辑可能需要进行一些更改以考虑到这一点。
推荐阅读
- sql - 如何在 LinQ Lambda 中按父级分组并对其子级应用聚合函数
- ios - Apple Appstore - 从应用程序直接向苹果应用程序商店发布评级
- windows - exec basename 不能从 Windows makefile 工作
- php - 从php获取nodejs中的跨源资源
- laravel - 客户端错误:`POST https://graph.facebook.com/v3.0/oauth/access_token` 导致`400 Bad Request`
- python - 为什么我不能使用这样的变量 a=(1,2)
- angular - Angular - 材料/自动完成未按预期显示
- javascript - 为什么 CSS 是导入 JSX 文件而不是单独加载?
- javascript - 如何在控制器/组件中使用导入的第三方 js(不是 addon/npm 包)?
- android - 我正在使用 laravel 和 opentok 开发视频聊天应用程序。但我坚持实施一种让用户互相通话的方法