dorpbn1027 2017-05-29 13:16
浏览 48
已采纳

在自链接模型中,删除父节点时删除子节点

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.

  • 写回答

1条回答 默认 最新

  • dongmu5246 2017-05-29 13:29
    关注

    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
    ]);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?