Last Update : 2023-09-22 UTC 10:11:56 AM
I have a very simple entity(WpmMenu) that w3coded doctrine disable holds menu items connected to one another in a w3coded doctrine disable self-referencing relationship (adjecent list w3coded doctrine disable it's called)? so in my entity I have:,Q: Is w3coded doctrine disable there a way to block/disable this behaviour and w3coded doctrine disable tell these entities they they ARE in sync with w3coded doctrine disable the db so no additional querying is w3coded doctrine disable needed?,Making statements based on opinion; back w3coded doctrine disable them up with references or personal w3coded doctrine disable experience.,5 queries to get the 3 w3coded doctrine disable children(level=2) of each of the level 1 items w3coded doctrine disable +
Integrating it in the code of WpmMenuRepository, it becomes:
public function setupTree() {
$qb = $this->createQueryBuilder("res");
/** @var $res Array */
$res = $qb->select("res")->orderBy('res.level', 'DESC')->addOrderBy('res.name','DESC')->getQuery()->getResult();
/** @var $prop ReflectionProperty */
$prop = $this->getClassMetadata()->reflFields["children"];
foreach($res as &$entity) {
$prop->getValue($entity)->setInitialized(true);//getValue will return a \Doctrine\ORM\PersistentCollection
}
/** @var $treeRoot WpmMenu */
$treeRoot = array_pop($res);
$treeRoot->setupTreeFromFlatCollection($res);
return($treeRoot);
}
Example:
/**
* @ManyToMany(targetEntity="User", mappedBy="groups", fetch="EAGER")
*/
The only thing needed is a custom repository that is going to query the flat tree structure, and then, by iterating this array it will, first mark the children collection as initialized and then will hydratate it with the addChild setter present in the parent entity..
<?php
namespace Domain\Repositories;
use Doctrine\ORM\EntityRepository;
class PageRepository extends EntityRepository
{
public function getPageHierachyBySiteId($siteId)
{
$roots = [];
$flatStructure = $this->_em->createQuery('SELECT p FROM Domain\Page p WHERE p.site = :id ORDER BY p.order')->setParameter('id', $siteId)->getResult();
$prop = $this->getClassMetadata()->reflFields['children'];
foreach($flatStructure as &$entity) {
$prop->getValue($entity)->setInitialized(true); //getValue will return a \Doctrine\ORM\PersistentCollection
if ($entity->getParent() != null) {
$entity->getParent()->addChild($entity);
} else {
$roots[] = $entity;
}
}
return $roots;
}
}
Last Update : 2023-09-22 UTC 13:56:10 PM
Last Update : 2023-09-22 UTC 13:55:54 PM
Last Update : 2023-09-22 UTC 13:55:47 PM
Last Update : 2023-09-22 UTC 13:55:35 PM
Last Update : 2023-09-22 UTC 13:55:17 PM
Last Update : 2023-09-22 UTC 13:54:33 PM
Last Update : 2023-09-22 UTC 13:54:17 PM