沃梦达 / 编程问答 / php问题 / 正文

如何使用 Symfony 和 Doctrine Query Builder 执行连接查询

How to perform a join query using Symfony and Doctrine Query Builder(如何使用 Symfony 和 Doctrine Query Builder 执行连接查询)

本文介绍了如何使用 Symfony 和 Doctrine Query Builder 执行连接查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个通过 1:1 关系连接的实体,例如:MyEntity.idRelatedEntity我想创建一个 Doctrine 查询,我可以根据 RelatedEntity 中某个列的值从 MyEntity 检索数据.像这样的东西(当然不起作用):

I have two entities which are connected through a 1:1 relationship, e.g: MyEntity.idRelatedEntity I want to create a Doctrine query where I can retrieve data from MyEntity depending on a value from a certain column in RelatedEntity. Something like this (it doesn't work of course):

$entity = $em
    ->getRepository('MyBundle:RelatedEntity')
    ->createQueryBuilder('e')
    ->leftJoin('MyBundle:RelatedEntity', 'r')
    ->where('r.foo = 1')
    ->getQuery()
    ->getResult();

任何帮助将不胜感激:)

Any help would be much appreciated :)

推荐答案

$entity = $em
    ->getRepository('MyBundle:MyEntity')
    ->createQueryBuilder('e')
    ->join('e.idRelatedEntity', 'r')
    ->where('r.foo = 1')
    ->getQuery()
    ->getResult();

左连接在这里也没有任何意义(因为 where 子句会让它像内连接一样工作)

Also left join makes no sense here (because of where clause that will make it work like inner join)

这篇关于如何使用 Symfony 和 Doctrine Query Builder 执行连接查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:如何使用 Symfony 和 Doctrine Query Builder 执行连接查询

基础教程推荐