[英]Doctrine multi-row groupBy count
I have the following Query with doctrine: 我有以下查询与学说:
$q = Doctrine_Query::create()
->select("q.id, q.user_id, count(ua.id) as quantity")
->from("question q")
->leftJoin("q.Answers a")
->leftJoin("a.UserAnswers ua")
->groupBy("ua.user_id");
Generated SQL is: 生成的SQL为:
SELECT q.id AS q__id, q.user_id AS q__user_id, COUNT(u.id) AS u__0 FROM question q LEFT JOIN answer a ON q.id = a.question_id LEFT JOIN user_answer u ON a.id = u.answer_id GROUP BY u.user_id
And the result of this query in MySQL is like: 在MySQL中此查询的结果如下:
+-------+------------+------+
+ q__id + q__user_id + u__0 +
+-------+------------+------+
+ 1 + 1 + 0 +
+-------+------------+------+
+ 26 + 6 + 2 +
+-------+------------+------+
+ 26 + 6 + 1 +
+-------+------------+------+
Using Doctrine, there is a way to obtain "u__0" column values? 使用Doctrine,有没有一种方法可以获取“ u__0”列值?
I tried: 我试过了:
$questions = $q->execute();
echo (count($questions));
but the result has only 2 rows (not 3). 但结果只有2行(不是3行)。
How can I get "u__0" column values? 如何获取“ u__0”列值? (0, 2, 1) using Doctrine? (0,2,1)使用主义?
You have to loop for each result and retrieve the quantity
value: 您必须为每个结果循环并检索quantity
值:
$q = Doctrine_Query::create()
->select("q.id as id, q.user_id as question_user_id, ua.user_id as answer_user_id, count(ua.id) as quantity")
->from("question q")
->leftJoin("q.Answers a")
->leftJoin("a.UserAnswers ua")
->groupBy("answer_user_id");
$results = $q->execute(array(), Doctrine_Core::HYDRATE_ARRAY);
foreach ($results as $result)
{
var_dump($result['quantity']);
}
I changed SQL query to: 我将SQL查询更改为:
$q = Doctrine_Query::create()
->select("q.id, q.user_id, a.id, count(ua.id) as quantity")
->from("Answer a")
->leftJoin("a.Question q")
->leftJoin("a.UserAnswers ua")
->groupBy("ua.user_id");
And it works ;) 它有效;)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.