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

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

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

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'));
}
xmjla07d

xmjla07d1#

$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']
        ]);
        $flag = 0;
        // pr($delete->toArray());die;
        foreach ($delete as $key) {
            $this->CustomerProblems->delete($key);
            $flag = 1;
        }
        if($flag == 1){
            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.'));
ffscu2ro

ffscu2ro2#

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']
])) {

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

相关问题