MembreRepository.php 2.73 KB
Newer Older
1 2 3 4
<?php

namespace AssoBundle\Repository;

5

6 7 8 9 10 11 12 13
/**
 * MembreRepository
 *
 * This class was generated by the Doctrine ORM. Add your own custom
 * repository methods below.
 */
class MembreRepository extends \Doctrine\ORM\EntityRepository
{
Mat's avatar
Mat committed
14 15 16 17 18

    /**
     *  @return Membre[]
     */
    public function findAllByIndex()
Mat's avatar
Mat committed
19 20 21 22 23 24 25
    {
        $qb = $this->createQueryBuilder('m')
                ->join('m.association', 'a')
                ->addSelect('a')
                ->orderBy('m.id', 'ASC');

        return $qb->getQuery()->getResult();
Mat's avatar
Mat committed
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
    }

    /**
     *  @param $id
     *  @return Membre
     *  @throws \Doctrine\ORM\NonUniqueResultException
     */
    public function findOneByUpdate($id)
    {
        $qb = $this->createQueryBuilder('m')
                ->join('m.association', 'a')
                ->addSelect('a')
                ->where('m.id = :id')
                ->setParameter(':id', $id);

        return $qb->getQuery()->getOneOrNullResult();
    }

    /**
     *  @param $id
     *  @return Membre
     *  @throws \Doctrine\ORM\NonUniqueResultException
     */
    public function findOneByDelete($id)
    {
        $qb = $this->createQueryBuilder('m')
                ->where('m.id = :id')
                ->setParameter(':id', $id);
Mat's avatar
Mat committed
54

Mat's avatar
Mat committed
55
        return $qb->getQuery()->getOneOrNullResult();
Mat's avatar
Mat committed
56
    }
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78

    /**
     *  @param $id
     *  @return Array
     */
    public function countMembresByAsso($id)
    {
        // Cette fonction ne sert finalement pas.
        // Je la laisse pour l'exemple DQL avec INNER JOIN

        //  SELECT COUNT(m.association_id) FROM membre m
        //  INNER JOIN association a ON m.association_id = a.id
        //  WHERE a.id = 1

        $qb = $this->createQueryBuilder('m')
        ->select('COUNT(m.association)')
        ->innerJoin('m.association', 'a', 'WITH', 'a.id = m.association' )
        ->where('a.id = :id')
        ->setParameter(':id', $id);

        return $qb->getQuery()->getSingleResult();
    }
79 80 81 82 83 84 85 86 87 88 89


    /**
     * @param $paginate
     * @param $start
     * @param $column
     * @param $order
     * @return Membre[]
     */
    public function findAllForAPIReadPaginate($paginate, $start, $column = null, $order = null)
    {
90 91 92 93
        $qb = $this->createQueryBuilder('m')
            ->join('m.association', 'a')
            ->addSelect('a')
            ->setMaxResults($paginate)
94
            ->setFirstResult(($start - 1) * $paginate);
95 96 97 98 99 100 101 102 103

        if ($column !== null) {
            $qb ->orderBy(sprintf('m.%s', $column), $order);
            // ou plus simplement 'm.' . $column
        }
        // Toutes ces méthodes du queryBuilder sont déjà sécurisées.
        // Concrêtement on utilise le binding de paramètres (:id)
        // principalement pour le where.

104 105 106
        return $qb->getQuery()->getResult();

    }
107
}