jmc
jmc

Reputation: 312

Symfony 2 DQL query

I have this query in Symfony 2. It works great but I was wondering if it was possible to make something cleaner.

I'm new to Symfony and QueryBuilder and I couldn't get to do something that works with the queryBuilder.

$em = $this->getEntityManager();
    $query = $em->createQuery(
    'SELECT t '.
    'FROM MyBundle:Task t '.
    'WHERE t.folder IN '.
            '(SELECT f.id '.
            'FROM MyBundle:Folder f '.
            'WHERE f.priority = '.$prio.
            ' AND f.user IN '.
              '(SELECT u.id '.
                'FROM MyBundle:User u '.
                'WHERE u.id ='.$user->getId().'))');

The scheme is simple A User has Many Folders, a Folder has many Tasks and I m looking for all tasks for one user.

Thanks a lot !

Upvotes: 0

Views: 203

Answers (1)

mohsenJsh
mohsenJsh

Reputation: 2108

do you have entity relation for this three entity? if yes try this

$em->createQuery('SELECT t FROM MyBundle:Task t'
                . 'JOIN t.folder f'
                . 'JOIN f.user u'
                . 'WHERE u.id=:id')
                ->setParameter('id', $user->getId());

Upvotes: 2

Related Questions