php—删除所有现有条目,然后在cakephp3中为相同的数据插入新条目

fivyi3re  于 2021-07-24  发布在  Java
关注(0)|答案(2)|浏览(388)

这是我的控制器=>
当我使用deleteall()时,得到以下错误=>error:sqlstate[21000]:基数冲突:1241操作数应包含1列
当我使用delete()时,我得到这个错误=>error:unknown method isnew

  1. public function addSolution($ids = null)
  2. {
  3. $user_id=$this->Auth->User('id');
  4. $city_id=$this->Auth->User('city_id');
  5. $location_id=$this->Auth->User('location_id');
  6. $this->viewBuilder()->layout('super_admin_layout');
  7. $id = $this->EncryptingDecrypting->decryptData($ids);
  8. $customerProblem = $this->CustomerProblems->get($id, [
  9. 'contain' => []
  10. ]);
  11. $cust_id = $customerProblem['customer_id'];
  12. $order_no = $customerProblem['order_no'];
  13. if ($this->request->is(['patch', 'post', 'put'])) {
  14. $customerProblem = $this->CustomerProblems->patchEntity($customerProblem, $this->request->getData());
  15. $search=$this->request->getData();
  16. $solutions=$this->request->getData()['solution'];
  17. $delete = $this->CustomerProblems->find()->where([
  18. 'customer_id' => $search['customer_id'],
  19. 'mobile_no' => $search['mobile_no'],
  20. 'order_no' => $search['order_no'],
  21. 'problem' => $search['problem'],
  22. 'resolve_status' => $search['resolve_status'],
  23. 'status' => $search['status']
  24. ]);
  25. if($this->CustomerProblems->deleteAll($delete)){
  26. foreach ($solutions as $solution) {
  27. $customerProblem = $this->CustomerProblems->newEntity();
  28. $customerProblem->city_id = $city_id;
  29. $customerProblem->created_by = $user_id;
  30. $customerProblem->customer_id = $this->request->getData()['customer_id'];
  31. $customerProblem->mobile_no = $this->request->getData()['mobile_no'];
  32. $customerProblem->order_no = $this->request->getData()['order_no'];
  33. $customerProblem->problem = $this->request->getData()['problem'];
  34. $customerProblem->resolve_status = $this->request->getData()['resolve_status'];
  35. $customerProblem->status = $this->request->getData()['status'];
  36. $customerProblem->solution = $solution;
  37. $this->CustomerProblems->save($customerProblem);
  38. }
  39. $this->Flash->success(__('The customer problem has been saved.'));
  40. return $this->redirect(['action' => 'index']);
  41. }
  42. $this->Flash->error(__('The customer problem could not be saved. Please, try again.'));
  43. }
  44. $customers=$this->CustomerProblems->Customers->find()->where(['Customers.id'=>$cust_id,'Customers.status'=>'Active']);
  45. $customer=[];
  46. foreach($customers as $data1){
  47. $customer[]= ['value'=>$data1->id,'text'=>ucwords($data1->name)." (".$data1->username.")"];
  48. }
  49. $getAllSolution = $this->CustomerProblems->find()->where(['CustomerProblems.order_no'=>$order_no,'CustomerProblems.status'=>'Active'])->extract('solution');
  50. $this->set(compact('customerProblem','customer','getAllSolution'));
  51. }
xmjla07d

xmjla07d1#

  1. $delete = $this->CustomerProblems->find()->where([
  2. 'customer_id' => $search['customer_id'],
  3. 'mobile_no' => $search['mobile_no'],
  4. 'order_no' => $search['order_no'],
  5. 'problem' => $search['problem'],
  6. 'resolve_status' => $search['resolve_status'],
  7. 'status' => $search['status']
  8. ]);
  9. $flag = 0;
  10. // pr($delete->toArray());die;
  11. foreach ($delete as $key) {
  12. $this->CustomerProblems->delete($key);
  13. $flag = 1;
  14. }
  15. if($flag == 1){
  16. foreach ($solutions as $solution) {
  17. $customerProblem = $this->CustomerProblems->newEntity();
  18. $customerProblem->city_id = $city_id;
  19. $customerProblem->created_by = $user_id;
  20. $customerProblem->customer_id = $this->request->getData()['customer_id'];
  21. $customerProblem->mobile_no = $this->request->getData()['mobile_no'];
  22. $customerProblem->order_no = $this->request->getData()['order_no'];
  23. $customerProblem->problem = $this->request->getData()['problem'];
  24. $customerProblem->resolve_status = $this->request->getData()['resolve_status'];
  25. $customerProblem->status = $this->request->getData()['status'];
  26. $customerProblem->solution = $solution;
  27. $this->CustomerProblems->save($customerProblem);
  28. }
  29. $this->Flash->success(__('The customer problem has been saved.'));
  30. return $this->redirect(['action' => 'index']);
  31. }
  32. $this->Flash->error(__('The customer problem could not be saved. Please, try again.'));
展开查看全部
ffscu2ro

ffscu2ro2#

deleteAll 不接受要删除的实体列表,它接受条件数组并删除所有匹配的内容。

  1. if($this->CustomerProblems->deleteAll([
  2. 'customer_id' => $search['customer_id'],
  3. 'mobile_no' => $search['mobile_no'],
  4. 'order_no' => $search['order_no'],
  5. 'problem' => $search['problem'],
  6. 'resolve_status' => $search['resolve_status'],
  7. 'status' => $search['status']
  8. ])) {

请注意,它返回受影响的行数,而不是true/false,因此 if 考虑到这一点,逻辑可能需要有所改变。

相关问题