Symfony/Doctrine中关联实体集合的筛选
如果我有一个关联的对象是集合,我可以限制结果吗 例如:Producer实体具有属性translations,其中包含其他实体的集合(ProducerTranslation) 生产控制器:Symfony/Doctrine中关联实体集合的筛选,symfony,doctrine-orm,Symfony,Doctrine Orm,如果我有一个关联的对象是集合,我可以限制结果吗 例如:Producer实体具有属性translations,其中包含其他实体的集合(ProducerTranslation) 生产控制器: $producers = $this->getDoctrine() ->getRepository('ProducerBundle:Producer') ->findAll(); 结果: Producer id: 1 translations:
$producers = $this->getDoctrine()
->getRepository('ProducerBundle:Producer')
->findAll();
结果:
Producer
id: 1
translations:
en: ProducerTranslation
de: ProducerTranslation
没关系。但我只想得到一种语言的一个实体。
预期结果:
$producers = $this->getDoctrine()
->getRepository('ProducerBundle:Producer')
->findByLocale('en');
Producer
id: 1
translations:
en: ProducerTranslation
如何操作?如果您只想查看结果,则需要使用
findOneBy
前缀:
$producers = $this->getDoctrine()
->getRepository('ProducerBundle:Producer')
->findOneByTranslations('en');
您必须在这里使用正确的属性名称您有
翻译
,因此它将是findonebytransations
来限制子集合,您可以像这样使用querybuilder(假设区域设置是ProducerTranslation的属性):
那会得到你想要的。请注意,select('p,pt')部分很重要,因为它只会将您想要的项目提取到收集结果中
$producers = $this->getDoctrine()
->getRepository('ProducerBundle:Producer')
->findOneByTranslations('en');
$qb = $this->getEntityManager()->createQueryBuilder();
$qb->select('p, pt')
->from('ProducerBundle:Producer', 'p')
->join('p.translations', 'pt')
->where($qb->expr()->eq('pt.locale', ':locale'))
->setParameter('locale', 'en')
->getQuery()
->getResult();