Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Symfony2 Doctrine query

I'm new in symfony2 , I don't know how to write a below query in symfony2 using createQuery()

select * from Post inner join Category on Post.category_id=Category.id inner join Priority on Post.priority_id=Priority.id order by priority_number desc

I used repository class,in which ,wrote a function

public function findAllOrderedByPriorityPost()
    {

        return $this->getEntityManager()
                ->createQuery('select p,c,pr from RodasysfourmBundle:Post p inner join 
RodasysfourmBundle:Category c  inner join RodasysfourmBundle:Priority pr order by pr.priorityNumber desc')
                ->getResult();
    }

when I used this function,I got the below error

[Semantical Error] line 0, col 85 near 'c inner join': Error: Identification Variable RodasysfourmBundle:Category used in join path expression but was not defined before. 

Also which method is best using this query in a custom repository or as a service?

any help appreciated.

like image 966
Asish AP Avatar asked Nov 25 '11 06:11

Asish AP


People also ask

What is Doctrine query?

Doctrine Query Language (DQL) is an Object Query Language created for helping users in complex object retrieval. You should always consider using DQL (or raw SQL) when retrieving relational data efficiently (eg. when fetching users and their phonenumbers).

Is Query Builder an ORM?

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

What is Symfony 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. These tools support relational databases like MySQL and PostgreSQL and also NoSQL databases like MongoDB.


1 Answers

DQL in doctrine2 won't know how to join tables if you specify the name of a referenced entity. You can only work with one entity and its fields (which may have relations).

select p,c,pr from RodasysfourmBundle:Post p inner join 
p.Category c inner join c.Priority pr order by pr.priorityNumber desc
like image 193
meze Avatar answered Sep 19 '22 14:09

meze