CakePHP 3 Associations Uninstall

Im new to CakePHP and all this with associations. When I want to delete a category, I also want to remove the competencies associated with the category. These are my table models:

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');
    }
}

When I delete a category, it deletes the rows in the link table, but not in the competencies in the CompetenceTable. I know that I forgot something, but I can’t understand that.

+4
source share
1 answer

I'm not sure why you have a hasMany member in your competency join table. Is there a reason? He must belong. If dependent => truethere are no try parameters in the hasMany parameter association parameters,

. hasMany(). .

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

Source: https://habr.com/ru/post/1667046/


All Articles