How to select fields using doctrine 2 query builder
Solution 1
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').
Solution 2
When you use the select()
method, you override the default one which is in $this->createQueryBuilder('m')
. That's why you lost the m
alias. To avoide this, use an addSelect()
or specify the alias
in the from()
method:
->from('Bundle:Entity', 'ALIAS')
Solution 3
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();
Dr.Knowitall
I'm a fullstack engineering breaking into the data science world. I'm interested in computer visualization, linguistics processing, machine learning and their applications in quantitative finance.
Updated on July 09, 2022Comments
-
Dr.Knowitall almost 2 years
I have the following query I'm executing in Symfony2 Repository. The query looks like
$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...
[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.