I want to delete all the child node(child categories) while deleting the parent category(using 'dependent'=> true). Here is the brief explanation I am working with a Self-linking model. Let's consider I have 2 tables
1. Categories
2. Products
where the category is multilevel. so that I have a column in the Categories table named as "parent_category_id". I have self-linked my model as bellow,
$this->belongsTo('FileCategories', [
'foreignKey' => 'parent_category_id',
'className' => 'FileCategories',
'joinType' => 'INNER',
'dependent'=> true
]);
Dependent "true" is not working in this cenerio, so Is I am doing something wrong or there is another method to do this.
You should also add hasMany relationship as below.
$this->hasMany('Parent', [
'foreignKey' => 'parent_category_id',
'className' => 'FileCategories',
'joinType' => 'INNER',
'dependent'=> true
]);
If it does not work, you can try this option ('cascadeCallbacks' => true).
$this->hasMany('Parent', [
'foreignKey' => 'parent_category_id',
'className' => 'FileCategories',
'joinType' => 'INNER',
'dependent'=> true,
'cascadeCallbacks' => true
]);