<?php
namespace App\Repository;
use App\Entity\Article;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\ORM\Tools\Pagination\Paginator;
use Doctrine\Persistence\ManagerRegistry;
/**
* ArticleRepository
*
* This class was generated by the Doctrine ORM. Add your own custom
* repository methods below.
*/
class ArticleRepository extends ServiceEntityRepository
{
public function __construct(ManagerRegistry $registry)
{
parent::__construct($registry, Article::class);
}
public function getOthers(Article $article, $limit = "3")
{
$qb = $this->createQueryBuilder("a");
$qb->andWhere($qb->expr()->isNull("a.deletedAt"));
$qb->andWhere($qb->expr()->neq("a.id", $article->getId()));
$qb->orderBy('a.createdAt', "DESC");
$qb->setMaxResults($limit);
return $qb->getQuery()->getResult();
}
public function getArticlesList($search = null)
{
$now = new \DateTime("now", new \DateTimeZone("Europe/Paris"));
$parameters = [
"now" => $now->format("Y-m-d H:i") . "%"
];
$qb = $this->createQueryBuilder("a");
if ($search != null) {
$qb->leftJoin('a.translations', 'at');
$qb->andWhere($qb->expr()->orX(
$qb->expr()->like('at.title', ':search'),
$qb->expr()->like('at.content', ':search')
));
$parameters["search"] = "%" . $search . "%";
}
$qb->andWhere($qb->expr()->isNull("a.deletedAt"));
$qb->andWhere($qb->expr()->orX(
$qb->expr()->isNull('a.startDate'),
$qb->expr()->lte('a.startDate', ':now')
));
$qb->andWhere($qb->expr()->orX(
$qb->expr()->isNull('a.endDate'),
$qb->expr()->gte('a.endDate', ':now')
))
->setParameters($parameters);
$qb->orderBy('a.sort', "ASC");
$pagination = new Paginator($qb->getQuery());
return $pagination->getIterator();
}
public function getAllActiveArticles()
{
$now = new \DateTime("now", new \DateTimeZone("Europe/Paris"));
$parameters = [
"now" => $now->format("Y-m-d H:i") . "%"
];
$qb = $this->createQueryBuilder("a")->addSelect('c');
$qb->leftJoin('a.categories', 'c');
$qb->andWhere($qb->expr()->isNull("a.deletedAt"));
$qb->andWhere($qb->expr()->orX(
$qb->expr()->isNull('a.startDate'),
$qb->expr()->lte('a.startDate', ':now')
));
$qb->andWhere($qb->expr()->orX(
$qb->expr()->isNull('a.endDate'),
$qb->expr()->gte('a.endDate', ':now')
));
$qb->setParameters($parameters);
$qb->orderBy('a.sort', "ASC");
return $qb->getQuery()->getResult();
}
public function getArticleBy($catId, $search = null, $limit = null)
{
$now = new \DateTime("now", new \DateTimeZone("Europe/Paris"));
$parameters = [
"now" => $now->format("Y-m-d H:i") . "%",
'id' => $catId
];
$qb = $this->createQueryBuilder('a');
$qb->join('a.categories', 'c')
->where('a.deletedAt is null')
->andWhere('c.deletedAt is null')
->andWhere('c.id = :id')
->andWhere($qb->expr()->orX(
$qb->expr()->isNull('a.startDate'),
$qb->expr()->lte('a.startDate', ':now')
))
->andWhere($qb->expr()->orX(
$qb->expr()->isNull('a.endDate'),
$qb->expr()->gte('a.endDate', ':now')
));
if ($search != null) {
$qb->leftJoin('a.translations', 'at');
$qb->andWhere($qb->expr()->orX(
$qb->expr()->like('at.title', ':search'),
$qb->expr()->like('at.content', ':search')
));
$parameters["search"] = "%" . $search . "%";
}
$qb
->setMaxResults($limit)
->setParameters($parameters);
$qb->orderBy('a.sort', "ASC");
return $qb->getQuery()->getResult();
}
}