KnpLabs / DoctrineBehaviors

Doctrine2 behavior traits that help handling Blameable, Loggable, Sluggable, SoftDeletable, Uuidable, Timestampable, Translatable, Tree behavior
http://knplabs.com
MIT License
911 stars 287 forks source link

[Tree] Inserting without parent #597

Closed phpSimplex closed 2 years ago

phpSimplex commented 3 years ago

When there is no parent, the "MaterializedPath" will be stored as a /. If you call the $entity->getFlatTreeQB() the sorting is based on the MaterializedPath. the sorting is not right becouse the entitys with no parent will be first in line.

If there is no parent it should be stored as /{id} to create a right sorting on the MaterializedPath. My workaround;

/**

  • @ORM\PostPersist()
  • @ORM\PostUpdate() */ public function postInsert(LifecycleEventArgs $event): void { // set parent child relation path $em = $event->getEntityManager(); if (null !== ($parent = $this->getParent())) { $child = $this; $child->setMaterializedPath($parent->getParentMaterializedPath() . '/'.$child->getId()); $em->persist($child); $em->persist($parent); } else { $this->setMaterializedPath('/' . $this->getId()); } $em->flush(); }
TomasVotruba commented 2 years ago

Hi, I'm going through old opended issues and PRs to cleanup too-long-opened items and close them. Thank you for your contribution and time.

We needs a feature with test and passing CI to be able to merge it without letting a bug in. If this is still relevant for you, please add code with test and make CI pass first. Then we'll able to merge your feature without further waiting :+1:

Thank for understanding.