[英]Select One column Doctrine DQL
I need a simple column for a table.我需要一个简单的表格列。
By example a table "project", with column id
, name
and 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 自动构建的。
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 one HYDRATE_RECORD
,默认一个HYDRATE_ARRAY
HYDRATE_NONE
HYDRATE_SCALAR
HYDRATE_ARRAY_SHALLOW
You need the HYDRATE_ARRAY_SHALLOW
hydration method.您需要HYDRATE_ARRAY_SHALLOW
水化方法。 Here's why.这是为什么。
HYDRATE_RECORD 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).这将使用对象来水合结果,并水合关系(如果您在查询中使用 leftJoin)。 Since it returns object, we need to call toArray()
to be able to send a propre json:由于它返回对象,我们需要调用toArray()
才能发送一个正确的 json:
[{"id":1,"name":"Project name","year":2013}]"
HYDRATE_ARRAY 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 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 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"}]"
I'm not sure what version of Doctrine j0k was using.我不确定使用的是什么版本的 Doctrine j0k。 It provided some answers, but I did have trouble finding Doctrine_Query and Doctrine_Core classes.它提供了一些答案,但我确实很难找到 Doctrine_Query 和 Doctrine_Core 类。 I am using Doctrine 2.3.4.我正在使用 Doctrine 2.3.4。 The following worked for me.以下对我有用。
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'];
As of Doctrine 2.10, you can use Scalar Column Hydration :从 Doctrine 2.10 开始,您可以使用Scalar Column Hydration :
$query = $em->createQuery('SELECT a.id FROM CmsUser u');
$ids = $query->getResult(Query::HYDRATE_SCALAR_COLUMN);
or或者
$ids = $query->getSingleColumnResult();
and this results in a flat array这导致了一个平面阵列
[412, 959, 1234]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.