Doctrine select many from the one(多对一单向(不同的捆绑))
Doctrine select many from the one (many-to-one unidirectional (different bundles))
在一个限制可用选项的遗留项目中工作,让我处于需要解决以下问题的情况,最好是根据原则。
我在不同的包中有两个实体,它们具有单向多对一 link。
BundleA 依赖于 BundleB,实体 linked 类似于:
BundleA/Entity/TheMany:
/**
* @var TheOne $theOne
* @ORM\ManyToOne(targetEntity="BundleB\Entity\TheOne")
* @ORM\JoinColumn(name="theone_id", referencedColumnName="id", onDelete="SET NULL")
*
*/
private $theOne;
我现在需要从 BundleB select 所有 TheOne 实体,并且对于每个实体我都需要所有 TheMany 实体。
查询还需要可以在任何 属性 个 TheOne 实体或相关 TheMany 实体的计数上进行排序。
在 Doctrine 中构建一个查询来返回所有 TheOne 实体和每个实体中的一个 TheMany 是相当简单的......但是我在想出一个将返回所有相关实体的 Doctrine 查询时遇到了一些困难TheMany entity 而不仅仅是一个。
我希望有人可能遇到过类似的问题,因此有一些见解?
这可能解释得不够清楚,在这种情况下请指导我进一步解释。
所以你要做的是得到所有的,然后为每一个找到所有的很多。但是你想把所有的很多放在一个数组中,或者你想为实体创建一个数组? (我在这里做了什么)
$em = $this->getDoctrine()->getManager();
$theones = $em->getRepository('BundleA:theOne')
->createQueryBuilder('p')
->OrderBy(//your ordering)
->getQuery()
->getArrayResult()
$theManies = [];
for($theones as $theOne){
$theManies [] = $em->getRepository('BunbleB:theMany')
->createQueryBuilder('p')
->Where('p.theOne = :theOne')
->setParameter('theOne', $theOne)
->getQuery()
->getArrayResult();
$finalOnes[$theOne->getId()] = sizeof($theManies)
}
asort($finalOnes);
return array_keys($finalOnes);
最后,我能够通过使用 GROUP_CONCAT(需要包含 https://github.com/beberlei/DoctrineExtensions)来实现我所需要的。
查询看起来像这样:
$queryBuilder->select(
'to,
GROUP_CONCAT(DISTINCT tm.id SEPARATOR \',\') as theManyIds,
COUNT(DISTINCT tm.id) as HIDDEN theManyCount'
)
->from('BundleB\Entity\TheOne', 'to')
->leftJoin(
'BundleA\Entity\TheMany',
'tm',
Join::WITH,
'to.id = tm.theOne'
)
->groupBy('to.id')
->orderBy($sortString, $direction)
->setFirstResult($start)
->setMaxResults($limit);
我妥协了,接受链接两个包的后果 - 然而,这可以通过使用本机 SQL 和结果集映射 (http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/reference/native-sql.html) 来避免。
在一个限制可用选项的遗留项目中工作,让我处于需要解决以下问题的情况,最好是根据原则。
我在不同的包中有两个实体,它们具有单向多对一 link。
BundleA 依赖于 BundleB,实体 linked 类似于:
BundleA/Entity/TheMany:
/**
* @var TheOne $theOne
* @ORM\ManyToOne(targetEntity="BundleB\Entity\TheOne")
* @ORM\JoinColumn(name="theone_id", referencedColumnName="id", onDelete="SET NULL")
*
*/
private $theOne;
我现在需要从 BundleB select 所有 TheOne 实体,并且对于每个实体我都需要所有 TheMany 实体。
查询还需要可以在任何 属性 个 TheOne 实体或相关 TheMany 实体的计数上进行排序。
在 Doctrine 中构建一个查询来返回所有 TheOne 实体和每个实体中的一个 TheMany 是相当简单的......但是我在想出一个将返回所有相关实体的 Doctrine 查询时遇到了一些困难TheMany entity 而不仅仅是一个。
我希望有人可能遇到过类似的问题,因此有一些见解?
这可能解释得不够清楚,在这种情况下请指导我进一步解释。
所以你要做的是得到所有的,然后为每一个找到所有的很多。但是你想把所有的很多放在一个数组中,或者你想为实体创建一个数组? (我在这里做了什么)
$em = $this->getDoctrine()->getManager();
$theones = $em->getRepository('BundleA:theOne')
->createQueryBuilder('p')
->OrderBy(//your ordering)
->getQuery()
->getArrayResult()
$theManies = [];
for($theones as $theOne){
$theManies [] = $em->getRepository('BunbleB:theMany')
->createQueryBuilder('p')
->Where('p.theOne = :theOne')
->setParameter('theOne', $theOne)
->getQuery()
->getArrayResult();
$finalOnes[$theOne->getId()] = sizeof($theManies)
}
asort($finalOnes);
return array_keys($finalOnes);
最后,我能够通过使用 GROUP_CONCAT(需要包含 https://github.com/beberlei/DoctrineExtensions)来实现我所需要的。
查询看起来像这样:
$queryBuilder->select(
'to,
GROUP_CONCAT(DISTINCT tm.id SEPARATOR \',\') as theManyIds,
COUNT(DISTINCT tm.id) as HIDDEN theManyCount'
)
->from('BundleB\Entity\TheOne', 'to')
->leftJoin(
'BundleA\Entity\TheMany',
'tm',
Join::WITH,
'to.id = tm.theOne'
)
->groupBy('to.id')
->orderBy($sortString, $direction)
->setFirstResult($start)
->setMaxResults($limit);
我妥协了,接受链接两个包的后果 - 然而,这可以通过使用本机 SQL 和结果集映射 (http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/reference/native-sql.html) 来避免。