Doctrine paginator在querybuilder中不使用count

时间:2015-10-30 12:55:49

标签: php symfony doctrine-orm doctrine dql

每个Disque(d)都有一个Note(n)的相关集合。 我的查询构建器是根据关联的Note数来获取每个Disque。

这是课程的最高峰

class Disque
{
    ...

    /**
    * @ORM\ManyToMany(targetEntity="Note", cascade={"persist"})
    */
    private $notes;

    ...

}

class Note
{
    ...

    /**
     * @ORM\ManyToOne(targetEntity="NoteValeur", cascade={"persist"})
     * @ORM\JoinColumn(nullable=true)
     */
    private $noteValeur;

    ...
}

class NoteValeur
{
    ...
    /**
     * @Gedmo\Slug(fields={"titre"})
     * @ORM\Column(name="slug", unique=true, length=32)
     */
    private $slug;
}

我使用Doctrine Querybuilder来获取结果,使用计数来设置阈值。

<?php

...

class DisqueRepository extends \Doctrine\ORM\EntityRepository
{
    public function getDisquesByNotesAndAllInfo($slug, $seuil, $page, $nbPerPage)
    {
        $q = $this->createQueryBuilder('d');
        $q
                ->select('d as infosDisque')
                ->leftJoin('d.pochettes', 'p')
                ->addSelect('p')
                ->leftJoin('d.groupes','g')
                ->addSelect('g')
                ->leftJoin('d.labelDisque', 'l')
                ->addSelect('l')
                ->leftJoin('d.chroniques','c')
                ->addSelect('c')
                ->leftJoin('d.notes', 'n')
                ->addSelect('n')
                ->innerJoin('n.noteValeur','nv')
                ->addSelect('nv')
                ->groupBy('d')
                ->addSelect('COUNT(d) as NbNotes')
                ->where('nv.slug LIKE :slug')
                ->setParameter('slug', $slug)
                ->having($q->expr()->gte('NbNotes',':seuil'))
                ->setParameter('seuil', $seuil)
                ->setFirstResult( ($page-1) * $nbPerPage)
                ->setMaxResults($nbPerPage)
                ->orderBy('d.dateSortieDisque', 'desc')
        ;

        return new Paginator($q, true);

    }
}

结果集非常好。问题是,当我更改$ page值时,SQL中的偏移量不会增加。无论如何,我都能获得相同的结果。

Symfony探查器为我提供了以下可执行查询

SELECT DISTINCT id_44 
FROM (
    SELECT 
        COUNT(s0_.id) AS sclr_0, s0_.id AS id_1, s0_.titre AS titre_2, s0_.datesortiedisque AS datesortiedisque_3, s0_.nombreDisque AS nombreDisque_4, s0_.remarque AS remarque_5, s0_.tracklist AS tracklist_6, s0_.lineup AS lineup_7, s0_.slug AS slug_8, s0_.coderef AS coderef_9, 
        s1_.id AS id_10, s1_.alt AS alt_11, s1_.url AS url_12, s1_.coderef AS coderef_13, s1_.ordre AS ordre_14, s1_.fichier AS fichier_15, 
        s2_.id AS id_16, s2_.nom AS nom_17, s2_.site AS site_18, s2_.motto AS motto_19, s2_.popularite AS popularite_20, s2_.slug AS slug_21, s2_.coderef AS coderef_22, 
        s3_.id AS id_23, s3_.titre AS titre_24, s3_.slug AS slug_25, s3_.coderef AS coderef_26, 
        s4_.id AS id_27, s4_.texte AS texte_28, s4_.resume AS resume_29, s4_.dateSaisie AS dateSaisie_30, s4_.lectures AS lectures_31, s4_.slug AS slug_32, s4_.coderef AS coderef_33, 
        s5_.id AS id_34, s5_.dateSaisie AS dateSaisie_35, s5_.coderef AS coderef_36, s6_.id AS id_37, 
        s6_.titre AS titre_38, s6_.valeur AS valeur_39, s6_.image AS image_40, s6_.coderef AS coderef_41, s6_.octal AS octal_42, s6_.slug AS slug_43, 
        s0_.id AS id_44, s0_.titre AS titre_45, s0_.datesortiedisque AS datesortiedisque_46, s0_.nombreDisque AS nombreDisque_47, s0_.remarque AS remarque_48, s0_.tracklist AS tracklist_49, s0_.lineup AS lineup_50, s0_.slug AS slug_51, s0_.coderef AS coderef_52 
    FROM 
        se_disque s0_ 
        LEFT JOIN disque_pochette d7_ ON s0_.id = d7_.disque_id 
        LEFT JOIN se_pochette s1_ ON s1_.id = d7_.pochette_id 
        LEFT JOIN disque_groupe d8_ ON s0_.id = d8_.disque_id 
        LEFT JOIN se_groupe s2_ ON s2_.id = d8_.groupe_id 
        LEFT JOIN se_labeldisque s3_ ON s0_.label_disque_id = s3_.id 
        LEFT JOIN disque_chronique d9_ ON s0_.id = d9_.disque_id 
        LEFT JOIN se_chronique s4_ ON s4_.id = d9_.chronique_id 
        LEFT JOIN disque_note d10_ ON s0_.id = d10_.disque_id 
        LEFT JOIN se_note s5_ ON s5_.id = d10_.note_id 
        INNER JOIN se_notevaleur s6_ ON s5_.note_valeur_id = s6_.id 
    WHERE 
        s6_.slug LIKE 'classique' 
    GROUP BY 
        s0_.id, s0_.titre, s0_.datesortiedisque, s0_.nombreDisque, s0_.remarque, s0_.tracklist, s0_.lineup, s0_.slug, s0_.coderef, s0_.label_disque_id, s0_.format_id, s0_.format_discographique_id 
    HAVING 
        sclr_0 >= 2
) dctrn_result 
ORDER BY datesortiedisque_3 DESC LIMIT 10 OFFSET 10 

无论我传递给$ q-&gt; setFirstResult()方法,我似乎无法更改此查询中的偏移量。

有人有线索吗?

1 个答案:

答案 0 :(得分:0)

好吧,猜猜看,我在我的控制器中使用了调试值而忘记了它:

$disques = $dr->getDisquesByNotesAndAllInfo($slug, $seuil, 2, $nbPerPage);

一切正常,因为我改变了这个凌乱的&#34; 2&#34;值。 对于那个很抱歉。感谢你们两位的帮助!