选择一个 Doctrine DQL 列。

14

我需要一个简单的表格列。

例如,一个名为"project"的表格,有列idnameyear

如果我这样做:

$q = Doctrine_Query::create()
        ->select('a.pro_id')
        ->from('fndr_proyecto a')
        ->where('a.pro_id =?',1);
    $pro = $q->execute();
    json_encode($pro->toArray());

答案是所有列都像。
{"id":1,"name":"Project name","year":2013}

但我只需要一列。我的期望是:
{"id":1}

由于使用本地 SQL 可以正常工作,因此需要使用 DQL。

ORM 是使用 Visual Paradigm 自动构建的。

3个回答

41
这是因为Doctrine使用所有对象信息来填充响应,所以包括所有列。
你需要使用不同的填充方法,有很多种方法可选, 但让我们重点关注其中的5种:
  • HYDRATE_RECORD,默认值
  • HYDRATE_ARRAY
  • HYDRATE_NONE
  • HYDRATE_SCALAR
  • HYDRATE_ARRAY_SHALLOW
你需要使用HYDRATE_ARRAY_SHALLOW填充方法。原因如下。
  1. HYDRATE_RECORD

    $q = Doctrine_Query::create()
        ->select('a.pro_id')
        ->from('fndr_proyecto a')
        ->where('a.pro_id = ?',1);
    $pro = $q->execute(array(), Doctrine_Core::HYDRATE_RECORD);
    var_dump(json_encode($pro->toArray()));
    

    This will hydrate the result using object, and also hydrate relations (if you use a leftJoin inside your query). Since it returns object, we need to call toArray() to be able to send a propre json:

    [{"id":1,"name":"Project name","year":2013}]"
    
  2. HYDRATE_ARRAY

    $q = Doctrine_Query::create()
        ->select('a.pro_id')
        ->from('fndr_proyecto a')
        ->where('a.pro_id = ?',1);
    $pro = $q->execute(array(), Doctrine_Core::HYDRATE_ARRAY);
    var_dump(json_encode($pro));
    

    This will hydrate result as an array an automatically add the primary key:

    [{"id":"1","pro_id":"1"}]"
    
  3. HYDRATE_NONE

    $q = Doctrine_Query::create()
        ->select('a.pro_id')
        ->from('fndr_proyecto a')
        ->where('a.pro_id = ?',1);
    $pro = $q->execute(array(), Doctrine_Core::HYDRATE_NONE);
    var_dump(json_encode($pro));
    

    This won't hydrate result, and return just values:

    [["1"]]"
    
  4. HYDRATE_SCALAR

    $q = Doctrine_Query::create()
        ->select('a.pro_id')
        ->from('fndr_proyecto a')
        ->where('a.pro_id = ?',1);
    $pro = $q->execute(array(), Doctrine_Core::HYDRATE_SCALAR);
    var_dump(json_encode($pro));
    

    This will hydrate result from the select but with key index as the column name with the table alias:

    [{"a_pro_id":"1"}]"
    
  5. HYDRATE_ARRAY_SHALLOW

    $q = Doctrine_Query::create()
        ->select('a.pro_id')
        ->from('fndr_proyecto a')
        ->where('a.pro_id = ?',1);
    $pro = $q->execute(array(), Doctrine_Core::HYDRATE_ARRAY_SHALLOW);
    var_dump(json_encode($pro));
    

    This will hydrate result from the select but with key index as the column name without the table alias:

    "[{"pro_id":"1"}]"
    

1
当然!现在在文档中找到“数据水合器”章节。我不知道它是关于这个主题的。 非常感谢!;) - h3g0r_
@h3g0r_ 试试数组填充,它通常比记录填充更快、更轻! - j0k
1
非常好的解释,我一直对水合剂的工作原理感到困惑,但现在我的思路已经清晰了。谢谢! - TwystO

3

从Doctrine 2.10开始,您可以使用标量列水合

$query = $em->createQuery('SELECT a.id FROM CmsUser u');
$ids = $query->getResult(Query::HYDRATE_SCALAR_COLUMN);

或者

$ids = $query->getSingleColumnResult();

这会导致一个扁平的数组。
[412, 959, 1234]

2

我不确定j0k使用的Doctrine版本。它提供了一些答案,但我在查找Doctrine_Query和Doctrine_Core类时遇到了麻烦。我正在使用Doctrine 2.3.4版本。以下内容对我有效。

public static function getAllEventIDs($em) {
    return parent::getAllFromColumn('\path\to\Entity\entityName', 'id', $em);
}

public static function getAllFromColumn($tableName, $columnName, $em) {
    $q = $em->createQueryBuilder('t')
    ->select("t.$columnName")
    ->from($tableName, 't');

    $q = $q->getQuery();

    $result = $q->getResult(\Doctrine\ORM\Query::HYDRATE_SCALAR);

    return $result;
}

然而,这确实返回了一个数组的数组。也就是说,第一个事件的ID是

$result[0]['id'];

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接