如何使用 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 执行连接查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Convert JSON integers and floats to strings(将JSON整数和浮点数转换为字符串)
in php how do I use preg replace to turn a url into a tinyurl(在php中,如何使用preg替换将URL转换为TinyURL)
all day appointment for ics calendar file wont work(ICS日历文件的全天约会不起作用)
trim function is giving unexpected values php(Trim函数提供了意外的值php)
Basic PDO connection to MySQL(到MySQL的基本PDO连接)
PHP number_format returns 1.00(Php number_Format返回1.00)