Symfony2 教义查询

2022-01-16 00:00:00 php symfony doctrine

我是 symfony2 的新手,我不知道如何使用 createQuery() 在 symfony2 中编写以下查询

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?

任何帮助表示赞赏.

推荐答案

如果您指定了引用实体的名称,doctrine2 中的 DQL 将不知道如何连接表.您只能使用一个实体及其字段(可能有关系).

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

相关文章