简体   繁体   English

Doctrine2复杂多对多搜索查询

[英]Doctrine2 complex multiple many-to-many search query

I am creating an application that has an Entity that has multiple many-to-many relationships with other objects. 我正在创建一个具有实体的应用程序,该实体与其他对象具有多个多对多关系。 Let me layout the Entities: 让我布置实体:

  • Entry = the main object Entry =主要对象
  • Region = has many-to-many with Entry via "_entry_region" reference table 通过“_entry_region”参考表,Region =具有多对多的Entry
  • Type = has many-to-many with Entry via "_entry_type" reference table 通过“_entry_type”引用表,Type =具有多对多的Entry
  • Tag = has many-to-many with Entry via "_entry_tag" reference table 通过“_entry_tag”参考表,Tag =具有多对多的Entry

Now in the frontend the user can setup some filters for the entries that need to be loaded. 现在,在前端,用户可以为需要加载的条目设置一些过滤器。 In human language the query needs to be like this. 在人类语言中,查询需要像这样。

Get the entries WHERE  region is (1 or 2 or 3) AND type is ( 3 or 4 or 5 ) AND tag is ( 4 OR 6 or 1)

i currently strugle with this piece of code: 我目前正在使用这段代码:

$query = $this->em->createQuery('SELECT m.id FROM Entity\Entry e WHERE :region_id MEMBER OF e.regions);
$query->setParameter('region_id',  1);      
$ids = $query->getResult();

This gives me the ID's of the entry's of the corresponding region. 这给了我相应区域的条目ID。 but it is not possible to add an array of the region id's in the setParameter(). 但是不可能在setParameter()中添加区域id的数组。 I also can't find in the docs how to do this on multiple related entities like my human based query: 我也无法在文档中找到如何在多个相关实体上执行此操作,例如基于人类的查询:

Get the entries WHERE  region is (1 or 2 or 3) AND type is ( 3 or 4 or 5 ) AND tag is ( 

You might also consider taking a look at the D2 query builder. 您还可以考虑查看D2查询构建器。 Bit more verbose but it's much easier to build more complicated queries once you get the syntax down. 更加冗长,但是一旦语法失效,构建更复杂的查询就会容易得多。

Something like: 就像是:

    $qb = $this->em->createQueryBuilder();

    $qb->addSelect('entry');
    $qb->addSelect('region');
    $qb->addSelect('type');
    $qb->addSelect('tag');

    $qb->from('MyBundle:Entry',   'entry');
    $qb->leftJoin('entry.regions','region');
    $qb->leftJoin('entry.types',  'type');
    $qb->leftJoin('entry.tags',   'tag');


    $qb->andWhere($qb->expr()->in('region.id',$regions));
    $qb->andWhere($qb->expr()->in('type.id',  $types));
    $qb->andWhere($qb->expr()->in('tag.id',   $tags));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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