CakePHP 3关联删除

我是Cake
PHP的新手,以及所有关联的东西.当我想删除一个类别时,我也想删除链接到该类别的权限.这是我的桌子型号:

CategoriesTable.php

class CategoriesTable extends Table
{
    public function initialize(array $config)
    {
        parent::initialize($config);

        $this->belongsToMany('Competences');
    }
}

CompetencesTable.php

class CompetencesTable extends Table
{
     public function initialize(array $config)
     {
        parent::initialize($config);

        $this->belongsToMany('Categories');
        $this->belongsToMany('CategoriesCompetences');
     }
}

CategoriesCompetencesTable.php

class CategoriesCompetencesTable extends Table
{
    public function initialize(array $config)
    {
        parent::initialize($config);

        $this->belongsTo('Categories');
        $this->hasMany('Competences');
    }
}

当我删除一个类别时,它会删除链接表中的行,但不会删除CompetenceTable中的权限.我知道我忘了一些东西,但无法弄清楚是什么.

最佳答案 我不确定为什么你的连接表中有一个hasMany assoc和Competences.有原因吗?它应该属于.如果没有尝试设置依赖关联选项=>如果是hasMany assoc,则为true.

See the documentation for hasMany().仔细阅读整个页面是个好主意.

public function initialize(array $config)
{
    $this->hasMany('Competences', [
        'foreignKey' => 'article_id',
        'dependent' => true,
    ]);
}
点赞