在cakephp中更新多个表

在cakephp中更新多个表,php,mysql,cakephp,cakephp-3.0,Php,Mysql,Cakephp,Cakephp 3.0,我想知道这个函数有什么问题,我只需要更新表中的一个字段或多关联表中的所有字段。 多重更新可以很好地工作,但是当我只更新第一列时,它就不能很好地工作 public function edit($id) { $contractor = $this->Contractors->get($id); $associated = ['ContractorsAttachments' ]; // Used to get the all attachments associ

我想知道这个函数有什么问题,我只需要更新表中的一个字段或多关联表中的所有字段。 多重更新可以很好地工作,但是当我只更新第一列时,它就不能很好地工作

public function edit($id) {
    $contractor = $this->Contractors->get($id);

     $associated = ['ContractorsAttachments' ];
    // Used to get the all attachments associated with Contractors 
     $ContractorsAttachments = $this->Contractors->ContractorsAttachments->find()->where(['contractor_id' => $id])->all();
    if ($this->request->is(['patch', 'post', 'put'])) {
      $contractor = $this->Contractors->patchEntity($contractor, $this->request->data );

       if ($this->Contractors->save($contractor)) {

            $this->Flash->success(__('The Contractors has been saved.'));

            return $this->redirect(['action' => 'index']);
        }
        $this->Flash->error(__('The Contractors could not be saved. Please, try again.'));
    }
    $this->set(compact('contractor','ContractorsAttachments'));
    $this->set('_serialize', ['contractor']);
    $this->render('add');
    }

可以使用获取关联的模型数据

使用

调整后,您的代码将如下所示

     $contractor = $this->Contractors->get($id, ['contain'=>'ContractorsAttachments']);

     if ($this->request->is(['patch', 'post', 'put'])) {
       $contractor = $this->Contractors->patchEntity($contractor, $this->request->data );

       if ($this->Contractors->save($contractor,['associated' => ['ContractorsAttachments']])) {

            $this->Flash->success(__('The Contractors has been saved.'));

            return $this->redirect(['action' => 'index']);
        }
        $this->Flash->error(__('The Contractors could not be saved. Please, try again.'));
    }
        $this->hasMany('ContractorsAttachments', [
            'foreignKey' => 'contractor_id'
        ]);
还要验证您的承包商模型是否定义了如下关联

     $contractor = $this->Contractors->get($id, ['contain'=>'ContractorsAttachments']);

     if ($this->request->is(['patch', 'post', 'put'])) {
       $contractor = $this->Contractors->patchEntity($contractor, $this->request->data );

       if ($this->Contractors->save($contractor,['associated' => ['ContractorsAttachments']])) {

            $this->Flash->success(__('The Contractors has been saved.'));

            return $this->redirect(['action' => 'index']);
        }
        $this->Flash->error(__('The Contractors could not be saved. Please, try again.'));
    }
        $this->hasMany('ContractorsAttachments', [
            'foreignKey' => 'contractor_id'
        ]);

希望这会有所帮助:)

您可以使用获取关联的模型数据

使用

调整后,您的代码将如下所示

     $contractor = $this->Contractors->get($id, ['contain'=>'ContractorsAttachments']);

     if ($this->request->is(['patch', 'post', 'put'])) {
       $contractor = $this->Contractors->patchEntity($contractor, $this->request->data );

       if ($this->Contractors->save($contractor,['associated' => ['ContractorsAttachments']])) {

            $this->Flash->success(__('The Contractors has been saved.'));

            return $this->redirect(['action' => 'index']);
        }
        $this->Flash->error(__('The Contractors could not be saved. Please, try again.'));
    }
        $this->hasMany('ContractorsAttachments', [
            'foreignKey' => 'contractor_id'
        ]);
还要验证您的承包商模型是否定义了如下关联

     $contractor = $this->Contractors->get($id, ['contain'=>'ContractorsAttachments']);

     if ($this->request->is(['patch', 'post', 'put'])) {
       $contractor = $this->Contractors->patchEntity($contractor, $this->request->data );

       if ($this->Contractors->save($contractor,['associated' => ['ContractorsAttachments']])) {

            $this->Flash->success(__('The Contractors has been saved.'));

            return $this->redirect(['action' => 'index']);
        }
        $this->Flash->error(__('The Contractors could not be saved. Please, try again.'));
    }
        $this->hasMany('ContractorsAttachments', [
            'foreignKey' => 'contractor_id'
        ]);

希望这会有所帮助:)

什么是使用
$associated=['ContractorsAttachments']
什么是使用
$associated=['ContractorsAttachments']
谢谢你的评论,问题也出在这里,回到我表格的这一行。你知道如何使文件不需要吗?谢谢你的评论,问题也出在我的表格上,又回到了这一行。你知道如何使文件不需要吗?