yii2-relation-trait icon indicating copy to clipboard operation
yii2-relation-trait copied to clipboard

deleteWithRelated only works with direct relations

Open optmsp opened this issue 3 years ago • 0 comments

The method deleteWithRelated() only seems to work with direct relations.

So this works just fine:

parent -> child

If I $parent->deleteWithRelated() then both parent and child are soft deleted.

However, this fails:

parent -> child -> child_of_child

In this situation, only parent and child are soft deleted, while child_of_child is untouched.

The solution is to update yii2-relation-trait with the needed information:

class parent()...
{
    public function relationNames()
    {
        return [
            'child',
            'child_of_child',
        ];
    }
    
    public function getChildOfChilds()
    {
        return $this->hasMany(ChildOfChild::className(), ['child_id' => 'id'])->via('Childs');
    }
}

However, if I remove relationNames() in the class def and let deleteWithRelated real-time determine this, it doesn't soft delete child_of_child. (https://github.com/mootensai/yii2-relation-trait/blob/aa8ff00c0fac616f58fd6efb0f7efd9a7ec931b6/RelationTrait.php#L461)

Is this correct behaviour?

optmsp avatar Jan 02 '22 19:01 optmsp