symfonydoctrine-odmdoctrine-querydoctrine-phpcr

How to filter by child value in Symfony PHPCR Query Builder


Trying to build a query in symfony that finds all PHPCR nodes of a certain document type with a given name and filters by the city of its Address child document.

    $qb->from()
            ->document('My\Bundle\Document\MyDocument', 'm')
        ->end()
        ->where()
        ->andX()
            ->eq()
                ->upperCase()->field('m.name')->end()
                ->literal('SOME-NAME')
            ->end()
            ->child('address', 'a')
                ->eq()
                    ->upperCase()->field('a.city')->end()
                    ->literal('MADRID'))
            ->end();

Can't get this working, what would be the best approach?

Thanks!


Solution

  • A working approach is to join the documents and use the child condition:

        $qb
            ->from('u')
            ->joinInner()
                ->left()->document('My\Bundle\Document\MyDocument', 'm')->end()
                ->right()->document('My\Bundle\Document\Address', 'a')->end()
                    ->condition()->child('a', 'm')->end()
                ->end()
            ->end()
            ->where()
                ->eq()
                    ->upperCase()->field('u.name')->end()
                    ->literal('SOME-NAME')->end()
                ->end()
            ->andWhere()
                ->eq()
                    ->upperCase()->field('a.city')->end()
                    ->literal('SOME-CITY')->end()
            ->end();