php 如何使用学说 2 查询构建器选择字段

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18970941/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 18:35:54  来源:igfitidea点击:

How to select fields using doctrine 2 query builder

phpmysqlsymfonyormdoctrine-orm

提问by Dr.Knowitall

I have the following query I'm executing in Symfony2 Repository. The query looks like

我在 Symfony2 Repository 中执行了以下查询。查询看起来像

                 $q = $this
                    ->createQueryBuilder('m')
                    ->select(array('m.reciever','m.created','m.id','m.subject'))
                    ->where('m.reciever = ?1')
                    ->orderBy('m.id','DESC')
                    ->setMaxResults( '?2' )
                    ->setFirstResult( '?3' )
                    ->setParameter(1,$id)
                    ->setParameter(2,$itemsPerPage)
                    ->setParameter(3,$offset)
                    ->getQuery();

Where reciever, created, id, and subject are fields part of my message entity. I do not need to specify which entity I am selecting from. The error I keep getting is such...

其中 receiver、created、id 和 subject 是我的消息实体的字段的一部分。我不需要指定我从哪个实体中选择。我不断收到的错误是这样的......

[Semantical Error] line 0, col 12 near 'reciever, m.created,': Error: Invalid PathExpression. Must be a StateFieldPathExpression. 

I'm not sure what a state field path expression is or what the syntax might be. It seems like everything should be right.

我不确定状态字段路径表达式是什么或语法可能是什么。似乎一切都应该是对的。

回答by S.Thiongane

When you use the select()method, you override the default one which is in $this->createQueryBuilder('m'). That's why you lost the malias. To avoide this, use an addSelect()or specify the aliasin the from()method:

当您使用该select()方法时,您会覆盖$this->createQueryBuilder('m'). 这就是您丢失m别名的原因。为避免这种情况,请在方法中使用addSelect()或 指定:aliasfrom()

->from('Bundle:Entity', 'ALIAS')

回答by sensi

Do you have something like this:=?

你有这样的东西:=?

 $q = $this
                ->createQueryBuilder()
                ->select('m.reciever, m.created ,m.id , m.subject')
                ->from('/Acme/Entity/DemoEntity', 'm')
                ->where('m.reciever = ?1')
                ->orderBy('m.id','DESC')
                ->setMaxResults( '?2' )
                ->setFirstResult( '?3' )
                ->setParameter(1,$id)
                ->setParameter(2,$itemsPerPage)
                ->setParameter(3,$offset)
                ->getQuery();

Im not sure but I think you use the array syntax only if you have multiple tables to join together: array('m.reciever, m.created', 'p.user, p.id').

我不确定,但我认为只有在将多个表连接在一起时才使用数组语法:array('m.reciever, m.created', 'p.user, p.id')

回答by GOPI

i hope this will help u..

我希望这会帮助你..

    $em = $this->getDoctrine()->getManager();
    $q = $em->createQueryBuilder()
            ->select('m.reciever,m.created,m.id,m.subject')
            ->from('bundle:entity','m')
            ->where('m.reciever = ?1')
            ->orderBy('m.id','DESC')
            ->setMaxResults( '?2' )
            ->setFirstResult( '?3' )
            ->setParameter(1,$id)
            ->setParameter(2,$itemsPerPage)
            ->setParameter(3,$offset)
            ->getQuery();