删除cakephp 3中的父行时删除所有关联的子行

时间:2017-05-04 11:40:31

标签: php mysql cakephp-3.0

我是cake php框架的新手,我想删除数据库中的行以及子表中的相关行。 现在,我只能删除父行,但没有它的子行,

如何删除所有关联的行。

这是我父母的功能:

    public function delete($id = null) {
    $contractor = $this->Contractors->get($id);
        if ($this->Contractors->delete($contractor)) {

        $this->Flash->success(__('The contractor has been deleted.'));
    } 
    else {
        $this->Flash->error(__('The contractor could not be deleted. Please, try again.'));
    }

    return $this->redirect(['action' => 'index']);
}

这里是孩子的功能

 public function deleteSub($id = null) {
    // $this->request->allowMethod(['post', 'delete']);

    $contractorAttachments = $this->Contractors->ContractorsAttachments->get($id);
       if($contractorAttachments->Contractors->ContractorsAttachments->delete($contractorAttachments))
    {
          $this->Flash->success(__('The contractor Attacments has been deleted.'));
    }
    else {
        $this->Flash->error(__('The contractor Attacments could not be deleted. Please, try again.'));
    }

    return $this->redirect(['action' => 'index']);
}

但我需要删除子行indidiual或删除所有父行和所有关联的子行。

任何人都可以帮助我吗?

0 个答案:

没有答案