php 选择一列 Doctrine DQL
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14411229/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Select One column Doctrine DQL
提问by h3g0r_
I need a simple column for a table.
我需要一个简单的表格列。
By example a table "project", with column id, nameand year.
例如一个表“项目”,列id,name和year。
If I do:
如果我做:
$q = Doctrine_Query::create()
->select('a.pro_id')
->from('fndr_proyecto a')
->where('a.pro_id =?',1);
$pro = $q->execute();
json_encode($pro->toArray());
The answer is all column like
答案是所有的列
{"id":1,"name":"Project name","year":2013}
but I need only one column. I expect:
但我只需要一栏。我预计:
{"id":1}
It is with DQL because with native SQL work fine.
它使用 DQL,因为使用本机 SQL 可以正常工作。
The ORM is build automaticaly with a Visual Paradigm.
ORM 是使用 Visual Paradigm 自动构建的。
回答by j0k
This is because Doctrine hydrate the response with all the object information, so all columns.
这是因为 Doctrine 将响应与所有对象信息水合,所以所有列。
You need to use a different hydration method, there are many one, but let's focus on 5 of them:
您需要使用不同的补水方法,有很多方法,但让我们重点介绍其中的 5 种:
HYDRATE_RECORD, the default oneHYDRATE_ARRAYHYDRATE_NONEHYDRATE_SCALARHYDRATE_ARRAY_SHALLOW
HYDRATE_RECORD,默认的HYDRATE_ARRAYHYDRATE_NONEHYDRATE_SCALARHYDRATE_ARRAY_SHALLOW
You need the HYDRATE_ARRAY_SHALLOWhydration method. Here's why.
你需要HYDRATE_ARRAY_SHALLOW补水方法。这是为什么。
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}]"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"}]"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"]]"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"}]"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"}]"
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()));这将使用对象来水合结果,并水合关系(如果您在查询中使用 leftJoin)。由于它返回对象,我们需要调用
toArray()才能发送一个正确的 json:[{"id":1,"name":"Project name","year":2013}]"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));这将水合物结果作为数组并自动添加主键:
[{"id":"1","pro_id":"1"}]"水合物_无
$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));这不会水合结果,并只返回值:
[["1"]]"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));这将水合物来自选择的结果,但使用键索引作为具有表别名的列名:
[{"a_pro_id":"1"}]"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));这将水合物来自选择的结果,但键索引作为没有表别名的列名:
"[{"pro_id":"1"}]"
回答by Julien Lamarche
I'm not sure what version of Doctrine j0k was using. It provided some answers, but I did have trouble finding Doctrine_Query and Doctrine_Core classes. I am using Doctrine 2.3.4. The following worked for me.
我不确定使用的是什么版本的 Doctrine j0k。它提供了一些答案,但我确实很难找到 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;
}
This did however return a array of arrays. ie, the id of the first event was is
然而,这确实返回了一个数组数组。即,第一个事件的 id 是
$result[0]['id'];

