A
A
Arthur Strezhinov2018-12-23 15:57:22
Yii
Arthur Strezhinov, 2018-12-23 15:57:22

Removing category chains?

When category 1 is deleted, then all categories after it should be deleted along the chain, you need to do a recursion, but I don’t understand how ???
4101bb548e.png
DB

id | parent_id | title
1 |        0        | Категория 1
2 |        1        | Категория  1.1
3 |        2        | Категория  1.1.1
4 |        3        | Категория  1.1.1.1
------------------------------------------
5 |        0        | Категория  2
6 |        5        | Категория  2.2
7 |        6        | Категория  2.2.2

In the controller, it's complete nonsense
public function actionDelete($id)
    {
            //$this->findModel($id)->delete();
            $parent = Category::find()->where(['parent_id' => $id])->one();
            if ($parent) {
                $child1 = Category::find()->where(['parent_id' => $parent->id])->one();
                $child2 = Category::find()->where(['parent_id' => $child1->id])->one();

                VarDumper::dump($child1->id,11,1);
                VarDumper::dump($child1->parent_id,11,1);
                VarDumper::dump($child2->id,11,1);
                VarDumper::dump($id,11,1);die();
            }

        return $this->redirect(['index']);
    }

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Maxim Timofeev, 2018-12-23
@webinar

The answer depends on how you store your category tree. There are many storage methods (adjustment list, Nested Sets, etc.), while the database architecture is completely different, so the solutions will be fundamentally different. And you can also have your own custom version. So before asking such a question, you need to paint your database architecture.
But I'm sure you already have the recursion needed when building the front tree. So don't reinvent the wheel. Although they usually use ready-made classes like https://github.com/creocoder/yii2-nested-sets and then there are no problems at all, since the methods have already been implemented.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question