app/Customize/Repository/NewsRepository.php line 88

Open in your IDE?
  1. <?php
  2. namespace Customize\Repository;
  3. use Eccube\Repository\AbstractRepository;
  4. use Doctrine\Common\Collections\ArrayCollection;
  5. use Doctrine\Common\Collections\Criteria;
  6. use Doctrine\DBAL\Exception\DriverException;
  7. use Doctrine\DBAL\Exception\ForeignKeyConstraintViolationException;
  8. use Customize\Entity\News;
  9. use Symfony\Bridge\Doctrine\RegistryInterface;
  10. /**
  11.  * NewsRepository
  12.  *
  13.  * This class was generated by the Doctrine ORM. Add your own custom
  14.  * repository methods below.
  15.  */
  16. class NewsRepository extends AbstractRepository
  17. {
  18.     public function __construct(RegistryInterface $registry)
  19.     {
  20.         parent::__construct($registryNews::class);
  21.     }
  22.     /**
  23.      * 新着情報を登録します.
  24.      *
  25.      * @param $News
  26.      */
  27.     public function save($News)
  28.     {
  29.         $em $this->getEntityManager();
  30.         $em->persist($News);
  31.         $em->flush($News);
  32.     }
  33.     /**
  34.      * 新着情報を削除します.
  35.      *
  36.      * @param News $News
  37.      *
  38.      * @throws ForeignKeyConstraintViolationException 外部キー制約違反の場合
  39.      * @throws DriverException SQLiteの場合, 外部キー制約違反が発生すると, DriverExceptionをthrowします.
  40.      */
  41.     public function delete($News)
  42.     {
  43.         $em $this->getEntityManager();
  44.         $em->remove($News);
  45.         $em->flush($News);
  46.     }
  47.     /**
  48.      * @return \Doctrine\ORM\QueryBuilder
  49.      */
  50.     public function getQueryBuilderAll($ClassName null)
  51.     {
  52.         $qb $this->createQueryBuilder('n');
  53.         $qb->select('n.id''n.publish_date AS publishDate''n.title''n.file_name''n.visible')
  54.             ->leftJoin('\Customize\Entity\NewsCategoryData''d', \Doctrine\ORM\Query\Expr\Join::WITH'n.id = d.news_id')
  55.             ->leftJoin('\Customize\Entity\NewsCategory''c', \Doctrine\ORM\Query\Expr\Join::WITH'd.category_id = c.id')
  56.             ->orderBy('n.publish_date''DESC')
  57.             ->addOrderBy('n.id''DESC')
  58.             ->groupBy('n.id');
  59.         if ($ClassName) {
  60.             $qb->where('c.class_name = :ClassName')
  61.                  ->setParameter('ClassName'$ClassName);
  62.         }
  63.         return $qb;
  64.     }
  65.     /**
  66.      * @return News[]|ArrayCollection
  67.      */
  68.     public function getList($limit=null$offset=null)
  69.     {
  70.         // second level cacheを効かせるためfindByで取得
  71.         $Results $this->findBy(['visible' => true], ['publish_date' => 'DESC''id' => 'DESC'], $limit$offset);
  72.         // 公開日時前のNewsをフィルター
  73.         $criteria Criteria::create();
  74.         $criteria->where(Criteria::expr()->lte('publish_date', new \DateTime()));
  75.         $News = new ArrayCollection($Results);
  76.         return $News->matching($criteria);
  77.     }
  78. }