0

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 !

1 Answer 1

2

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());
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks a lot ! I just had to add some spacings and a parameter for the priority but it works.
Or with the builder $em->createQueryBuilder('t')->join('t.folder', 'f')->join('f.user', 'u')->where('u.id', ':id')->setParameter('id', $user->getId())
I had to rearrange it a little bit but thanks to you I learned a bit more about this kind of syntax. The right syntax for the where is where('u.id = :id') Thanks ;-)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.