Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to perform a join query using Symfony and Doctrine Query Builder

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 :)

like image 847
viarnes Avatar asked Aug 21 '13 12:08

viarnes


People also ask

Does Symfony use doctrine?

Symfony provides all the tools you need to use databases in your applications thanks to Doctrine, the best set of PHP libraries to work with databases.

How does query builder work?

Using Query Builder, you can search and filter database objects, select objects and columns, create relationships between objects, view formatted query results, and save queries with little or no SQL knowledge.

Is query Builder an ORM?

The ORM's query builder provides a simple to use fluent interface for creating and running queries.

What is query Builder in C#?

SQL query builder, written in c#, helps you build complex queries easily, supports SqlServer, MySql, PostgreSql, Oracle, Sqlite and Firebird. sqlkata.com.


1 Answers

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

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

like image 133
Uriziel Avatar answered Sep 24 '22 01:09

Uriziel