Doctrine 2 QueryBuilder添加多个选择元素/参数? [英] Doctrine 2 QueryBuilder add multiple select elements /parameters?
问题描述
在这两种情况下,doctrine $ queryBuilder-> getQuery() - > getResults()返回一个数组,其中实体以字符串而不是对象表示:
情况1:
$ queryBuilder = $ this-> em-> ; createQueryBuilder();
$ queryBuilder-> select(e,99 as number);
它返回
code> array(4){
[0] =>
array(2){
[0] => string(30)Profile_Entity//注意这是一个字符串,但它应该是一个对象实例
[number] => string(2)99
}
...
...
如果我会写
$ queryBuilder = $ this-> em-> createQueryBuilder();
$ queryBuilder-> select(e);
$ queryBuilder-> select(99 as number);
它将返回与情况1相同的事情
下面是通常是这样的(这里只是select表达式中的一个元素)
array(4){
[0] => object(stdClass)#935(39){
[__CLASS __] => string(30)Profile_Entity
[id] => int(46)
[headline] => string(7)asdasd
...
...
...
我正在努力解决类似的问题。
我发现的答案比较简单。 p>
$ queryBuilder - > select('table.column1 alias1,table.column2 alias2');
通过使用逗号分隔,并使用列后面的空格放置列的别名。
编辑:
您不能将对象与值混合使用,您只能执行以下操作之一:
$ qb - > select('tableAlias1','tableAlias2')
$ qb - > from('table1','tableAlias1')
$ qb - > leftJoin('table2','tableAlias2');
$ qb - > select('tableAlias1.column1','tableAlias2.column1'
$ qb - > from('table1','tableAlias1')
$ qb - > leftJoin('table2','tableAlias2');
如果您确定需要,则必须指定要从每个表中检索的每个列以提取表格顶部的某些列。
I am still struggling with doctrine QueryBuilder as I think it is not working properly when I want to add another element into a select expression .
In this both situations doctrine $queryBuilder->getQuery()->getResults() is returning an array where entities are represented as a string instead of objects :
Situation 1 :
$queryBuilder = $this->em->createQueryBuilder();
$queryBuilder->select("e, 99 as number");
it returns
array(4) {
[0]=>
array(2) {
[0]=> string(30) "Profile_Entity" //notice this is a string but it should be an object instance
["number"]=> string(2) "99"
}
...
...
if i will write
$queryBuilder = $this->em->createQueryBuilder();
$queryBuilder->select("e");
$queryBuilder->select("99 as number");
It will return the same things as situation 1
Below is how it should normally be ( here is just one element in select expression )
array(4) {
[0]=> object(stdClass)#935 (39) {
["__CLASS__"]=> string(30) "Profile_Entity"
["id"]=> int(46)
["headline"]=> string(7) "asdasd
...
...
...
I was struggling with a similar problem.
The answer I found was relatively simple..
$queryBuilder -> select('table.column1 alias1, table.column2 alias2');
You seperate by using a comma, and use a space after the column to put the alias of the column.
EDIT:
You can't mix objects with values. You can only do one of these:
$qb -> select('tableAlias1', 'tableAlias2')
$qb -> from('table1','tableAlias1')
$qb -> leftJoin('table2','tableAlias2');
OR
$qb -> select('tableAlias1.column1', 'tableAlias2.column1')
$qb -> from('table1','tableAlias1')
$qb -> leftJoin('table2','tableAlias2');
You will have to specify each column that you want to retrive from each table if you decide that you need to extract certain columns on top of a table.
这篇关于Doctrine 2 QueryBuilder添加多个选择元素/参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!