DQL - select multiple fields, but select only hidden fields from the result set

Given the following DQL query:

SELECT p, (p.views * 0.1) + (p.likes * 0.9) as ratingPhoto FROM AppBundle:Photo p ORDER BY ratingPhoto DESC 

My result will look like this:

 array (size=14) 0 => array (size=2) 0 => object(Photo) 'ratingPhoto' => string '1.42' (length=4) 1 => array (size=2) 0 => object(Photo) 'ratingPhoto' => string '1.31' (length=4) ... 

Is there any DQL functionality so that I only retrieve Photo objects and exclude other selected fields from the result set? Here is what I expect as a result:

 array (size=14) 0 => object(Photo) 1 => object(Photo) ... 
+4
source share
2 answers

What you are looking for are the calculated values ​​in the ORDER and HIDDEN clauses.

In general, you need to do something like the following:

 SELECT a, b, (SOME_COMPUTATION()) AS computed FROM ... ORDER BY computed ASC 

To β€œhide” the computed value from the result set, starting with Doctrine ORM 2.3, you can use the HIDDEN clause:

 SELECT a, b, (SOME_COMPUTATION()) AS HIDDEN computed FROM ... ORDER BY computed ASC 

Here's what your DQL looks like:

 SELECT p, (p.views * 0.1) + (p.likes * 0.9) AS HIDDEN ratingPhoto FROM AppBundle:Photo p ORDER BY ratingPhoto DESC 
+5
source

You can rewrite your DQL as follows:

 $dql = "SELECT p FROM AppBundle:Photo p ORDER BY ((p.views * 0.1) + (p.likes * 0.9)) DESC"; 
-3
source

All Articles