Answer the question
In order to leave comments, you need to log in
Nested tree removal, how to recalculate lft, rgt?
Colleagues, good evening!
I'm deleting the entire thread.
Like this
/**
* @param $node
* @return void
*/
public function removeTree(Service $node)
{
$this->createQueryBuilder("s")
->delete()
->where("s.lft = :lft")->setParameter("lft", $node->getLft())
->andWhere("s.rgt = :rgt")->setParameter("rgt", $node->getRgt())
->andWhere("s.root = :root")->setParameter("root", $node->getRoot())
->getQuery()
->execute();
}
Answer the question
In order to leave comments, you need to log in
Decision.
As a first approximation, I recalculate as follows.
class ServiceRepository extends NestedTreeRepository
{
...
/**
* @param Service $node
* @return void
* @throws ORMException
* @throws OptimisticLockException
*/
public function removeTree(Service $node)
{
$this->createQueryBuilder("s")
->delete()
->where("s.lft >= :lft")->setParameter("lft", $node->getLft())
->andWhere("s.rgt <= :rgt")->setParameter("rgt", $node->getRgt())
->andWhere("s.root = :root")->setParameter("root", $node->getRoot())
->getQuery()
->execute();
$em = $this->getEntityManager();
$em->beginTransaction();
$repository = $em->getRepository("App:Service");
$refreshLeftAndRight = function($root, $left) use ($repository, &$refreshLeftAndRight) {
$right = $left + 1;
$children = $repository->findBy(['parent' => $root,]);
foreach ($children as $entity) {
$right = $refreshLeftAndRight($entity, $right);
}
$root->setlft($left);
$root->setRgt($right);
return $right + 1;
};
foreach ($repository->findBy(["parent" => null]) as $rootEntry) {
$refreshLeftAndRight($rootEntry, 1);
}
$em->flush();
$em->commit();
}
...
Good time of the day.
To implement the tree, there is a ready-made bundle of extensions for doctrine .
Look, it can be easier to use a ready-made solution.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question