Doctrine - self-referencing entity - disable fetching of children

phpdoctrinechildrenentityfetchingdisablereferencingselfassociation

Last Update : 2023-09-22 UTC 10:11:56 AM

Answers of > Doctrine - self-referencing entity - disable fetching of children

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;
    }
}

Current topics : Doctrine - self-referencing entity - disable fetching of children

Newly Added Questions

Similar Questions

Questions :

How To Group Array Key Value

Last Update : 2023-09-22 UTC 13:56:16 PM

Questions :

PhpStorm Warning For React Attributes In Jsx File With SCSS File

Last Update : 2023-09-22 UTC 13:56:10 PM

Questions :

Why Is The File Not Showing Up In Request.files And In Request.forms Instead?

Last Update : 2023-09-22 UTC 13:55:54 PM

Questions :

Proxying Assets From React App Directory In Slim Framework?

Last Update : 2023-09-22 UTC 13:55:47 PM

Questions :

Laravel 5.4 Can't Run “php Artisan Preset React” Comand

Last Update : 2023-09-22 UTC 13:55:35 PM

Questions :

How To Update Session Values Without Signing Out?

Last Update : 2023-09-22 UTC 13:55:17 PM

Questions :

Array Is Not Visible

Last Update : 2023-09-22 UTC 13:55:05 PM

Questions :

React Routing For Login Using Symfony

Last Update : 2023-09-22 UTC 13:54:46 PM

Questions :

Sanctum With React SPA Returning 419 Page Expired

Last Update : 2023-09-22 UTC 13:54:33 PM

Questions :

How Do I Import An Input String Into Another Page

Last Update : 2023-09-22 UTC 13:54:17 PM

Top
© 2023 W3CODED - All Rights Reserved.