假设我给一个函数一个数组:
$arrValues = ['19', '4', '4', '18', '19']
功能:
$objRequeteDoctrine = $this->getEntityManager()->createQueryBuilder()
->select('o')
->from('xxxxxx', 'o')
->where('o.id IN (:values)')
->andwhere('o.actif = 1')
->setParameter(':values', $arrValues );
return $objRequeteDoctrine->getQuery()->getResult();
在这里,它将检索3个对象(已删除重复项)。但是,如果我想检索5个重复的对象怎么办?有可能吗?
谢谢。
答案 0 :(得分:0)
用Doctrine实现这可能不是您问题的答案,但可以解决您的问题。 那以后生成完整的阵列怎么办?
$arrValues = ['19', '4', '4', '18', '19'];
$objRequeteDoctrine = $this->getEntityManager()->createQueryBuilder()
->select('o')
->from('xxxxxx', 'o')
->where('o.id IN (:values)')
->andwhere('o.actif = 1')
->setParameter(':values', $arrValues );
$result = $objRequeteDoctrine->getQuery()->getResult();
// Get the object ids in a separate array to find their position in the result
$idsOnly = [];
foreach($result as $entity) {
$idsOnly[] = $entity->getId();
}
// Find the key of the object by id in the first result
$fullResult = [];
foreach ($arrValues as $id) {
$keyFound = array_search($id, $idsOnly);
if ($keyFound !== false) {
$fullResult[] = $result[$keyFound];
}
}
return $fullResult;