简体   繁体   中英

Search in many-to-many relationship with Doctrine2

This is probably an easy one but I can't figure it out nor find an answer.

I have a simple Article and ArticleTag Entities with many to many relationship. How can I get all articles with a certain tag (or tags)?

My following tries:

$qb = $repository->createQueryBuilder('a')
    // ...
    ->andWhere('a.tags = :tag')
    ->setParameter('tag', 'mytag')
    // ...

or

    ->andWhere(':tag in a.tags')
    ->setParameter('tag', 'mytag')

...didn't work. Thanks!

And the winner is ... drumroll, please ...

$qb = $repository->createQueryBuilder('a')
    // ...
    ->andWhere(':tag MEMBER OF a.tags');
    ->setParameter('tag', $tag);
    // ...

Thanks to everyone who has taken the time to read and think about my question!

我想你可以提供这个例子(来自文档):

$query = $em->createQuery('SELECT u.id FROM CmsUser u WHERE EXISTS (SELECT p.phonenumber FROM CmsPhonenumber p WHERE p.user = u.id)');

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