src/Repository/ArticleRepository.php line 33

Open in your IDE?
  1. <?php
  2. namespace App\Repository;
  3. use App\Entity\Article;
  4. use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
  5. use Doctrine\ORM\Tools\Pagination\Paginator;
  6. use Doctrine\Persistence\ManagerRegistry;
  7. /**
  8.  * ArticleRepository
  9.  *
  10.  * This class was generated by the Doctrine ORM. Add your own custom
  11.  * repository methods below.
  12.  */
  13. class ArticleRepository extends ServiceEntityRepository
  14. {
  15.     public function __construct(ManagerRegistry $registry)
  16.     {
  17.         parent::__construct($registryArticle::class);
  18.     }
  19.     public function getOthers(Article $article$limit "3")
  20.     {
  21.         $qb $this->createQueryBuilder("a");
  22.         $qb->andWhere($qb->expr()->isNull("a.deletedAt"));
  23.         $qb->andWhere($qb->expr()->neq("a.id"$article->getId()));
  24.         $qb->orderBy('a.createdAt'"DESC");
  25.         $qb->setMaxResults($limit);
  26.         return $qb->getQuery()->getResult();
  27.     }
  28.     public function getArticlesList($search null)
  29.     {
  30.         $now = new \DateTime("now", new \DateTimeZone("Europe/Paris"));
  31.         $parameters = [
  32.             "now" => $now->format("Y-m-d H:i") . "%"
  33.         ];
  34.         $qb $this->createQueryBuilder("a");
  35.         if ($search != null) {
  36.             $qb->leftJoin('a.translations''at');
  37.             $qb->andWhere($qb->expr()->orX(
  38.                 $qb->expr()->like('at.title'':search'),
  39.                 $qb->expr()->like('at.content'':search')
  40.             ));
  41.             $parameters["search"] = "%" $search "%";
  42.         }
  43.         $qb->andWhere($qb->expr()->isNull("a.deletedAt"));
  44.         $qb->andWhere($qb->expr()->orX(
  45.             $qb->expr()->isNull('a.startDate'),
  46.             $qb->expr()->lte('a.startDate'':now')
  47.         ));
  48.         $qb->andWhere($qb->expr()->orX(
  49.             $qb->expr()->isNull('a.endDate'),
  50.             $qb->expr()->gte('a.endDate'':now')
  51.         ))
  52.             ->setParameters($parameters);
  53.         $qb->orderBy('a.sort'"ASC");
  54.         $pagination = new Paginator($qb->getQuery());
  55.         return $pagination->getIterator();
  56.     }
  57.     public function getAllActiveArticles()
  58.     {
  59.         $now = new \DateTime("now", new \DateTimeZone("Europe/Paris"));
  60.         $parameters = [
  61.             "now" => $now->format("Y-m-d H:i") . "%"
  62.         ];
  63.         $qb $this->createQueryBuilder("a")->addSelect('c');
  64.         $qb->leftJoin('a.categories''c');
  65.         $qb->andWhere($qb->expr()->isNull("a.deletedAt"));
  66.         $qb->andWhere($qb->expr()->orX(
  67.             $qb->expr()->isNull('a.startDate'),
  68.             $qb->expr()->lte('a.startDate'':now')
  69.         ));
  70.         $qb->andWhere($qb->expr()->orX(
  71.             $qb->expr()->isNull('a.endDate'),
  72.             $qb->expr()->gte('a.endDate'':now')
  73.         ));
  74.         $qb->setParameters($parameters);
  75.         $qb->orderBy('a.sort'"ASC");
  76.         return $qb->getQuery()->getResult();
  77.     }
  78.     public function getArticleBy($catId$search null$limit null)
  79.     {
  80.         $now = new \DateTime("now", new \DateTimeZone("Europe/Paris"));
  81.         $parameters = [
  82.             "now" => $now->format("Y-m-d H:i") . "%",
  83.             'id' => $catId
  84.         ];
  85.         $qb $this->createQueryBuilder('a');
  86.         $qb->join('a.categories''c')
  87.             ->where('a.deletedAt is null')
  88.             ->andWhere('c.deletedAt is null')
  89.             ->andWhere('c.id = :id')
  90.             ->andWhere($qb->expr()->orX(
  91.                 $qb->expr()->isNull('a.startDate'),
  92.                 $qb->expr()->lte('a.startDate'':now')
  93.             ))
  94.             ->andWhere($qb->expr()->orX(
  95.                 $qb->expr()->isNull('a.endDate'),
  96.                 $qb->expr()->gte('a.endDate'':now')
  97.             ));
  98.         if ($search != null) {
  99.             $qb->leftJoin('a.translations''at');
  100.             $qb->andWhere($qb->expr()->orX(
  101.                 $qb->expr()->like('at.title'':search'),
  102.                 $qb->expr()->like('at.content'':search')
  103.             ));
  104.             $parameters["search"] = "%" $search "%";
  105.         }
  106.         $qb
  107.             ->setMaxResults($limit)
  108.             ->setParameters($parameters);
  109.         $qb->orderBy('a.sort'"ASC");
  110.         return $qb->getQuery()->getResult();
  111.     }
  112. }