Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Symfony 2: INNER JOIN on non related table with doctrine query builder

I'm trying to build a query with the doctrine query builder which joins a non related table like this:

$query = $this->createQueryBuilder('gpr')         ->select('gpr, p')         ->innerJoin('TPost', 'p')         ->where('gpr.contentId = p.contentId') 

But this doesn't work. I still get an error:

Error: Identification Variable TPost used in join path expression but was not defined before.

I searched for this error message and everybody answered to use the table alias + attribute like p.someAttribute. But the table I want to join isn't related in the table I start my select from.

As a normal mysql query i would write it like this:

SELECT * FROM t_group_publication_rel gpr  INNER JOIN t_post p  WHERE gpr.content_id = p.content_id 

Any ideas what i'm doing wrong?

like image 742
0s1r1s Avatar asked Jun 20 '12 09:06

0s1r1s


People also ask

What helper methods are available in doctrine QueryBuilder?

Here is a complete list of helper methods available in QueryBuilder: Doctrine supports dynamic binding of parameters to your query, similar to preparing queries. You can use both strings and numbers as placeholders, although both have a slightly different syntax. Additionally, you must make your choice: Mixing both styles is not allowed.

How to bind parameters to a query in doctrine?

Doctrine supports dynamic binding of parameters to your query, similar to preparing queries. You can use both strings and numbers as placeholders, although both have a slightly different syntax. Additionally, you must make your choice: Mixing both styles is not allowed. Binding parameters can simply be achieved as follows:

Are arbitrary joins supported in doctrine?

As of Doctrine 2.3 or 2.4 (I don't remember anymore but both are already old anyway), arbitrary joins are supported, allowing to perform such joins without defining bidirectional relations.

What data does inner join take from a table?

INNER JOIN takes only data that exist in both tables. Not sure if I explained well for you, probably better to read about INNER JOIN and LEFT JOIN e.g. on wiki to see the difference on real examples.


2 Answers

Today I was working on similar task and remembered that I opened this issue. I don't know since which doctrine version it's working but right now you can easily join the child classes in inheritance mapping. So a query like this is working without any problem:

$query = $this->createQueryBuilder('c')         ->select('c')         ->leftJoin('MyBundleName:ChildOne', 'co', 'WITH', 'co.id = c.id')         ->leftJoin('MyBundleName:ChildTwo', 'ct', 'WITH', 'ct.id = c.id')         ->orderBy('c.createdAt', 'DESC')         ->where('co.group = :group OR ct.group = :group')         ->setParameter('group', $group)         ->setMaxResults(20); 

I start the query in my parent class which is using inheritance mapping. In my previous post it was a different starting point but the same issue if I remember right.

Because it was a big problem when I started this issue I think it could be also interesting for other people which don't know about it.

like image 161
0s1r1s Avatar answered Sep 29 '22 11:09

0s1r1s


Joins between entities without associations were not possible until version 2.4, where you can generate an arbitrary join with the following syntax:

$query = $em->createQuery('SELECT u FROM User u JOIN Blacklist b WITH u.email = b.email'); 

Reference: http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/reference/dql-doctrine-query-language.html

like image 31
Yamir Avatar answered Sep 29 '22 13:09

Yamir