G
G
Greg Popov2016-05-03 19:28:57
Yii
Greg Popov, 2016-05-03 19:28:57

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

1 answer(s)
K
Kirill Arutyunov, 2016-05-03
@Gregpopov

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 question

Ask a Question

731 491 924 answers to any question