Answer the question
In order to leave comments, you need to log in
How to manage categories?
Hello colleagues!
I know that there is a nested set
. It's kind of complicated, I haven't fully understood how it works yet.
Time is running out, and categories need to be managed yesterday.
/** @var Category $parent */
$parent = $repo->getById(1222); // 1222 идентификатор родителя
$p = $parent->getParent(); // object или null
$lvl = 0; // Уровень
while (true) {
$lvl++; // Повышаю уровень
if (is_null($p)) {
break; // если родителей больше нет
}
$p = $p->getParent(); // Получаем родителя
}
$new_category = new Category();
$new_category->setName("children 1");
$new_category->setParent($parent);
$new_category->setLvl($lvl);
$this->em->persist($new_category);
$this->em->flush();
{
"count": 47,
"items": [
{
"alt_name": "Розовые авто",
"name": "Авто",
"id": 2,
"lvl": "0"
},
{
"alt_name": null,
"name": "Безопасность",
"id": 3,
"lvl": "0"
},
{
"alt_name": null,
"name": "Бизнес",
"id": 4,
"lvl": "0"
},
...
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question