SQL原则查询的结果与参数中传递的值一样多

时间:2019-06-28 14:20:27

标签: php sql symfony

假设我给一个函数一个数组:

$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个重复的对象怎么办?有可能吗?

谢谢。

1 个答案:

答案 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;