【问题标题】:delete all the existing entries before inserting new entries in cakephp 3 for the same data在 cakephp 3 中为相同数据插入新条目之前删除所有现有条目
【发布时间】:2020-06-25 06:09:23
【问题描述】:

这是我的控制器 =>

当我使用 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'));
}

【问题讨论】:

  • 这显示了什么:echo $this->CustomerProblems->find()->where($delete)->sql();

标签: php mysql sql cakephp-3.0


【解决方案1】:
        $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.'));
    

【讨论】:

    【解决方案2】:

    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 逻辑可能需要稍作更改才能考虑到这一点。

    【讨论】:

      猜你喜欢
      • 2012-01-23
      • 1970-01-01
      • 1970-01-01
      • 2014-05-15
      • 1970-01-01
      • 2013-04-24
      • 2018-10-16
      • 2012-11-23
      • 1970-01-01
      相关资源
      最近更新 更多