从cakephp中的父控制器获取所有子数据

时间:2017-04-24 10:03:21

标签: php mysql cakephp-3.0

我试图从父控制器获取所有子数据以对所有子数据进行编辑,并且我已经使这个功能得到它们,

 public function edit($id = 'Contractors.id') {

    $contractor = $this->Contractors->get($id, [
        'contain' => ["ContractorsAttachments"]
    ]);
  $ContractorsAttachments = $this->Contractors->ContractorsAttachments->find()->where(['ContractorsAttachments.contractor_id', $id])->all();

    if ($this->request->is(['patch', 'post', 'put'])) {


        $array_dif = array();


            if (!empty($this->request->data['contractors_attachments'])) {
            foreach ($this->request->data['contractors_attachments'] as $attach) {
                if (isset($attach['id']))
                    $array_dif[] = $attach['id'];
            }
        }

        $ContractorsAttachments = $this->Contractors->patchEntity($ContractorsAttachments, $this->request->data,[
            'associated' => ['ContractorsAttachments']
        ]);

        if ($this->Contractors->save($ContractorsAttachments)) {
            $this->Flash->success(__('The contractor has been saved.'));
            return $this->redirect(['action' => 'index']);
        } else {
            $this->Flash->error(__('The contractor could not be saved. Please, try again.'));
        }
    }
    $attachments = $this->Contractors->ContractorsAttachments->find('list', ['limit' => 200]);

但它回来了每个父ID的子表中的所有数据! ,我只需要那个条件就可以回来了 where([' ContractorsAttachments.contractor_id',Contractors.id])

那么,我的代码出了什么问题!

0 个答案:

没有答案