首页 > 解决方案 > 在 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'));
}

标签: phpmysqlsqlcakephp-3.0

解决方案


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逻辑可能需要进行一些更改以考虑到这一点。


推荐阅读