假设我给一个函数一个数组:
$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个对象,重复的呢?有可能吗?
谢谢。
最佳答案
这可能不是你的问题的答案,以实现这一理论,但可以是一个解决你的问题。然后生成完整的数组呢?
$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;
关于php - SQL原则查询的结果与参数中传递的值一样多,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/56808634/