Doctrine QueryBuilder多个截然不同

时间:2015-11-20 16:11:31

标签: sql symfony doctrine distinct

我想使用doctrine ORM

在两列上使用DISTINCT限制结果

我的功能是这样的:

public function findFdvLinkedToPdv($pointVenteCodes){
    $queryBuilder = 
    $this->createQueryBuilder('r')
                ->leftJoin('r.forceVente', 'forceVente')
                ->leftJoin('r.pointVente', 'pointVente')
                ->leftJoin('r.signature', 'signature')
                ->leftJoin('signature.affaire', 'affaire')
                ->andWhere('pointVente.code IN (:pointvente_codes)')
                ->orderBy('forceVente.matricule', 'ASC')
                ->addOrderBy('pointVente.code', 'ASC')
                ->addOrderBy('affaire.code', 'ASC')
                ->addOrderBy('r.type', 'ASC')
                ->setParameters(array('pointvente_codes' => $pointVenteCodes,))
                ->select(array(
                        'forceVente.matricule AS forcevente_matricule',
                        'pointVente.code AS pointvente_code',
                        'affaire.code AS affaire_code',
                        'r.id AS id',
                        'r.profil AS profil',
                        'r.type AS type',
                        'forceVente.nom AS nom',
                        'forceVente.prenom AS prenom',
                        'forceVente.email AS email',
                        'r.deletedAt AS deletedAt'));
    return $queryBuilder->getQuery()->getArrayResult();
}

对于每个forcevente.matricule和每个pointVente.code,我有2到6行。我想为每对夫妇获得一行forcevente.matricule / pointVente.code我必须在两列上都做distinct,但是当我尝试时:

            ->select(array(
                    'DISTINCT forceVente.matricule AS forcevente_matricule',
                    'DISTINCT pointVente.code AS pointvente_code',
                    'affaire.code AS affaire_code', etc ...

我有一个学说错误......

[编辑] 我在执行过滤结果的请求后在PHP中执行此操作...

//BEGIN PATCH
$i=0;
$linkedForceVentes2 = array();
foreach ($linkedForceVentes as $item) {
    if (!isset($linkedForceVentes2[$item['pointvente_code']][$item['forcevente_matricule']])){
        $linkedForceVentes2[$item['pointvente_code']][$item['forcevente_matricule']] = $item;
    }else{
        unset($linkedForceVentes[$i]);
    }
    $i++;
}
//END PATCH

1 个答案:

答案 0 :(得分:2)

我最终用groupBy解决了它:

->groupBy('forcevente_matricule', 'pointvente_code', 'type' , 'affaire_code');

在select语句之后。