<?php
namespace Customize\Repository;
use Eccube\Repository\AbstractRepository;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Criteria;
use Doctrine\DBAL\Exception\DriverException;
use Doctrine\DBAL\Exception\ForeignKeyConstraintViolationException;
use Customize\Entity\News;
use Symfony\Bridge\Doctrine\RegistryInterface;
/**
* NewsRepository
*
* This class was generated by the Doctrine ORM. Add your own custom
* repository methods below.
*/
class NewsRepository extends AbstractRepository
{
public function __construct(RegistryInterface $registry)
{
parent::__construct($registry, News::class);
}
/**
* 新着情報を登録します.
*
* @param $News
*/
public function save($News)
{
$em = $this->getEntityManager();
$em->persist($News);
$em->flush($News);
}
/**
* 新着情報を削除します.
*
* @param News $News
*
* @throws ForeignKeyConstraintViolationException 外部キー制約違反の場合
* @throws DriverException SQLiteの場合, 外部キー制約違反が発生すると, DriverExceptionをthrowします.
*/
public function delete($News)
{
$em = $this->getEntityManager();
$em->remove($News);
$em->flush($News);
}
/**
* @return \Doctrine\ORM\QueryBuilder
*/
public function getQueryBuilderAll($ClassName = null)
{
$qb = $this->createQueryBuilder('n');
$qb->select('n.id', 'n.publish_date AS publishDate', 'n.title', 'n.file_name', 'n.visible')
->leftJoin('\Customize\Entity\NewsCategoryData', 'd', \Doctrine\ORM\Query\Expr\Join::WITH, 'n.id = d.news_id')
->leftJoin('\Customize\Entity\NewsCategory', 'c', \Doctrine\ORM\Query\Expr\Join::WITH, 'd.category_id = c.id')
->orderBy('n.publish_date', 'DESC')
->addOrderBy('n.id', 'DESC')
->groupBy('n.id');
if ($ClassName) {
$qb->where('c.class_name = :ClassName')
->setParameter('ClassName', $ClassName);
}
return $qb;
}
/**
* @return News[]|ArrayCollection
*/
public function getList($limit=null, $offset=null)
{
// second level cacheを効かせるためfindByで取得
$Results = $this->findBy(['visible' => true], ['publish_date' => 'DESC', 'id' => 'DESC'], $limit, $offset);
// 公開日時前のNewsをフィルター
$criteria = Criteria::create();
$criteria->where(Criteria::expr()->lte('publish_date', new \DateTime()));
$News = new ArrayCollection($Results);
return $News->matching($criteria);
}
}