Answer the question
In order to leave comments, you need to log in
How does relational delete() work in Yii2?
Hello.
There is an infinite structure of categories for products, something like:
category
----
id
title
parent_id
And a table with products that are associated with a category, something like:
items
--------
id
title
text
category_id
On deletion you need to find all parent_ids that contain the id of the element to be deleted, and in the items table you need to delete all positions that relate to category.id and category.parent_id.
In theory it's understandable, but in practice it's stuck. Thanks in advance.
Answer the question
In order to leave comments, you need to log in
In the database for foreign keys, you can specify ON DELETE CASCADE, then when the parent is deleted, all dependent records will be deleted.
By means of the framework, you delete 1 record (the parent), and the database will then destroy itself and delete everything that is needed.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question