假设我给函数一个数组:
$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 个重复的对象怎么办?这可能吗?
谢谢。
最佳答案
用 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;
关于php - 具有与参数中传递的值一样多的结果的 SQL 原则查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56808634/