5

给定以下 DQL 查询:

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

我的结果集如下所示:

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)
...

是否有任何 DQL 功能,以便我只检索Photo对象并从结果集中排除其他选定的字段?这是我期望的结果:

array (size=14)
  0 => object(Photo)
  1 => object(Photo)
...
4

2 回答 2

7

您正在寻找的是ORDER子句和HIDDEN关键字中的计算值。

通常,您必须执行以下操作:

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

要从结果集中“隐藏”computed值,从 Doctrine ORM 2.3 开始,您可以使用以下HIDDEN子句:

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

这就是您的 DQL 的样子:

SELECT 
    p,
    (p.views * 0.1) + (p.likes * 0.9) AS HIDDEN ratingPhoto
FROM
    AppBundle:Photo p
ORDER BY
    ratingPhoto DESC
于 2013-03-29T12:32:54.683 回答
-3

您可以将 DQL 重写为:

$dql = "SELECT p
FROM AppBundle:Photo p
ORDER BY ((p.views * 0.1) + (p.likes * 0.9)) DESC";
于 2013-03-26T15:25:56.083 回答