简体   繁体   中英

Symfony2 with Doctrine: Nested conditions for findBy

I'm trying to select only submissions that belongs to unlocked users (I'm using FOSUserBundle). Since I have a ManyToOne relationship on the submission entity, I figured that it might work to use nested conditions with findBy, it does not crash but gives me an empty result.

$submissions = $this->getDoctrine()
    ->getRepository('MyBundle:Submission')
    ->findBy(
        array(
            'author' => array(
                'locked' => false
            )
        ),
        array('createdAt' => 'DESC'),
        12
    );

Do I have to use query builder with joins for this to work? Something like the above would be so much cleaner code. It seemes to me that this would be a common problem for blog posts in a blog system and so on...

No you can't do such thing. But you will get even cleaner code if you move building query to a custom repository for Submission entity. For example the code will be look like:

$submissions = $this->getDoctrine()
    ->getRepository('MyBundle:Submission')
    ->fetchByUnlockedAuthor(12);

Where fetchByUnlockedAuthor would be a method in your custom repository.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM