Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Doctrine 2 QueryBuilder add multiple select elements /parameters?

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
    ...
  ...
...
like image 783
Webdesign7 London Avatar asked Nov 30 '12 13:11

Webdesign7 London


1 Answers

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.

like image 76
He Hui Avatar answered Oct 30 '22 19:10

He Hui