可以使用 groupBy 列作为带有 Doctrine 查询构建器的分组集合的关联数组索引吗?
Possible to use groupBy column as associative array index for grouped collections with Doctrine query builder?
给定如下 table 结构:
Matches:
| id | round | home_team | away_team |
| 1 | 1 | Juventus | Milan |
| 2 | 1 | Inter | Roma |
| 3 | 2 | Juventus | Inter |
| 4 | 2 | Roma | Milan |
...是否可以基于其中一列构建集合? 我希望所有 matches
都根据 round
列组织在集合中。
我当前的查询构建器如下所示:
/** @var MatchRepository $matchRepository */
$matchRepository = $em->getRepository('JCNApiBundle:Football\Match');
return $matchRepository->createQueryBuilder('m', 'm.round')
->where('m.competition = :competition')
->setParameter('competition', $competitionId)
->groupBy('m.id, m.round')
->getQuery()
->getArrayResult()
;
不幸的是,每组只有 returns 一行:(每个 round
一个 match
)
[
// Round 1
1 => [
// Match 1
"id" => 1,
"round" => 1,
"home_team" => "Juventus",
"away_team" => "Milan",
],
// Round 2
2 => [
// Match 3
"id" => 3,
"round" => 2,
"home_team" => "Juventus",
"away_team" => "Inter",
]
]
我正在寻找这样的东西:
[
// Round 1
1 => [
// Match 1
0 => [
"id" => 1
"round" => 1
"home_team" => "Juventus"
"away_team" => "Milan"
],
// Match 2
1 => [
"id" => 2
"round" => 1
"home_team" => "Inter"
"away_team" => "Roma"
]
]
// Round 2
2 => [
// Match 3
0 => [
"id" => 3
"round" => 2
"home_team" => "Juventus"
"away_team" => "Inter"
],
// Match 4
1 => [
"id" => 4
"round" => 2
"home_team" => "Roma"
"away_team" => "Milan"
]
]
]
Doctrine 查询生成器可以做到这一点吗?
不,SQL 根本不可能做到这一点。 SQL查询总是return二维数组。你想要一个三维的。
您需要跳过 GROUP BY
部分并迭代 returned 集合以在 PHP 中创建所需的结构。
正如 Jakub Matczak 所提到的,SQL 是不可能的。
但是,如果您希望您的查询 returns 这样的多维数组 - 您可以 write a custom hydrator。在那个水龙头内,手动按 "round" 进行分组。
因此,Doctrine 允许您将此 hydration/grouping 逻辑分离到一个单独的 class 中,但您仍然需要对其进行编码。
给定如下 table 结构:
Matches:
| id | round | home_team | away_team |
| 1 | 1 | Juventus | Milan |
| 2 | 1 | Inter | Roma |
| 3 | 2 | Juventus | Inter |
| 4 | 2 | Roma | Milan |
...是否可以基于其中一列构建集合? 我希望所有 matches
都根据 round
列组织在集合中。
我当前的查询构建器如下所示:
/** @var MatchRepository $matchRepository */
$matchRepository = $em->getRepository('JCNApiBundle:Football\Match');
return $matchRepository->createQueryBuilder('m', 'm.round')
->where('m.competition = :competition')
->setParameter('competition', $competitionId)
->groupBy('m.id, m.round')
->getQuery()
->getArrayResult()
;
不幸的是,每组只有 returns 一行:(每个 round
一个 match
)
[
// Round 1
1 => [
// Match 1
"id" => 1,
"round" => 1,
"home_team" => "Juventus",
"away_team" => "Milan",
],
// Round 2
2 => [
// Match 3
"id" => 3,
"round" => 2,
"home_team" => "Juventus",
"away_team" => "Inter",
]
]
我正在寻找这样的东西:
[
// Round 1
1 => [
// Match 1
0 => [
"id" => 1
"round" => 1
"home_team" => "Juventus"
"away_team" => "Milan"
],
// Match 2
1 => [
"id" => 2
"round" => 1
"home_team" => "Inter"
"away_team" => "Roma"
]
]
// Round 2
2 => [
// Match 3
0 => [
"id" => 3
"round" => 2
"home_team" => "Juventus"
"away_team" => "Inter"
],
// Match 4
1 => [
"id" => 4
"round" => 2
"home_team" => "Roma"
"away_team" => "Milan"
]
]
]
Doctrine 查询生成器可以做到这一点吗?
不,SQL 根本不可能做到这一点。 SQL查询总是return二维数组。你想要一个三维的。
您需要跳过 GROUP BY
部分并迭代 returned 集合以在 PHP 中创建所需的结构。
正如 Jakub Matczak 所提到的,SQL 是不可能的。
但是,如果您希望您的查询 returns 这样的多维数组 - 您可以 write a custom hydrator。在那个水龙头内,手动按 "round" 进行分组。 因此,Doctrine 允许您将此 hydration/grouping 逻辑分离到一个单独的 class 中,但您仍然需要对其进行编码。