本文介绍了Symfony2教义查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

  select * from Post inner join类别Post.category_id = Category.id内部连接优先级Post.priority_id = Priority.id order by priority_number desc 

我使用了存储库类,其中写了一个函数

  public function findAllOrderedByPriorityPost() 
{

return $ this-> getEntityManager()
- > createQuery('select p,c,pr from RodasysfourmBundle:Post p inner join
RodasysfourmBundle :class c inner join RodasysfourmBundle:pr prriorityNumber desc'的优先级顺序)
- > getResult();
}

当我使用这个功能,我得到以下错误

  [语义错误]行0,col 85靠近'c inner join':错误:标识变量RodasysfourmBundle:连接路径表达式中使用的类别,但不是之前定义。 

哪个方法最好在自定义存储库或服务中使用此查询?



任何帮助赞赏。

解决方案

doctrine2中的DQL不知道如何加入指定引用实体的名称的表。您只能使用一个实体及其字段(可能有关系)。

 从RodasysfourmBundle中选择p,c,pr: post p inner join 
p.Category c inner join c.Priority pr order by pr.priorityNumber desc


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.

解决方案

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

这篇关于Symfony2教义查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-26 13:41