Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

doctrine 2 query builder and join tables

I'm trying to get all comments for each post in my home page

return 
$this->createQueryBuilder('c')
->select('c')
->from('Sdz\BlogBundle\Entity\Commentaire' ,'c')                
->leftJoin('a.comments' ,'c')->getQuery()->getResult() ;

but I'm getting this error

[Semantical Error] line 0, col 58 near '.comments c,': Error:
Identification Variable a used in join path expression but was not defined before.

PS : The mapping is correct because I can see the page article with its comments.

like image 557
kosaidpo Avatar asked Nov 06 '11 23:11

kosaidpo


People also ask

Is Query builder an ORM?

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

What is a query builder?

Query Builder provides a graphical user interface for creating SQL queries. You can drag-and-drop multiple tables, views and their columns onto a visual designer to generate SQL statements. You can use Query Builder to perform the following tasks: Working with a graphical representation of a query or with SQL code.

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.

What is Query builder in Java?

The query builder is a utility to generate CQL queries programmatically.


1 Answers

In case this is still giving you problems, here is your query using the syntax found in the examples in the Doctrine 2.1 documentation.

I'm assuming your query resides in a custom repository method, and that 'a' is an abbreviation for 'Article'.

$em = $this->getEntityManager();
$qb = $em->createQueryBuilder();

$qb->select(array('a', 'c'))
   ->from('Sdz\BlogBundle\Entity\Article', 'a')
   ->leftJoin('a.comments', 'c');

$query = $qb->getQuery();
$results = $query->getResult();

return $results;
like image 194
cantera Avatar answered Sep 22 '22 12:09

cantera