php - Including a OneToMany object in a DoctrineExtension Tree entity

123

I'm using the NestedSet Tree method to build a tree of libraries. Every bottom level library also has a set of objects.

This is defined in my Library Entity class with:

/**
 * @ORM\OneToMany(targetEntity="Object", mappedBy="library")
 */
private $objects;

In my Object Entity class I have:

/**
 * @ORM\ManyToOne(targetEntity="Library", inversedBy="objects")
 **/
private $library;

And lastly, I retrieve the tree with

$em = $this->getDoctrine()->getManager();
$repo = $em->getRepository('AcmeDemoBundle:Library');
$arrayTree = $repo->childrenHierarchy();

When executing childrenHierarchy(), I get my tree, but without the Objects variable. Example:

[0] => Array
    (
        [id] => 1
        [name] => Foo
        [left] => 1
        [level] => 0
        [right] => 6
        [root] => 1
        [__children] => Array
            (
                [0] => Array
                    (
                        [id] => 2
                        [name] => Bar
                        [left] => 2
                        [level] => 1
                        [right] => 3
                        [root] => 1
                        [__children] => Array
                            (
                            )

                    )
640

Answer

Solution:

Turns out this wasn't so easy to solve.

I ended up writing a DQL query that fetched all libraries with their objects (with a join, so it only does one query).

I then looped through the hierarchy and joined everything together using a recursive function:

$em = $this->getDoctrine()->getManager();
$repo = $em->getRepository('AcmeDemoBundle:Library');
$allLibraries = $repo->findAllJoinedToObject();
$arrayTree = $repo->childrenHierarchy();

// merge the objects into the tree
$merge = function(&$tree) use (&$merge, $allLibraries) {
    foreach($tree as &$library) {
        $id = $library['id'];
        $library['objects'] = $allLibraries[$id]['objects'];

        if(count($library['__children'])) {
            $merge($library['__children']);
        }
    }
};

$merge($arrayTree);

Below is myfindAllJoinedToObject method:

public function findAllJoinedToObject()
{
    $query = $this->getEntityManager()
        ->createQuery('
            SELECT l, o FROM acmeDemoBundle:Library l INDEX BY l.id
            LEFT JOIN l.objects o INDEX BY o.id'
        );

    try {
        return $query->getArrayResult();
    } catch (\Doctrine\ORM\NoResultException $e) {
        return null;
    }
}

People are also looking for solutions to the problem: sql server - How to put Null value in a SQL sever via php code

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.