<?php
namespace Customize\Repository;
use Customize\Entity\Brand;
use Doctrine\Persistence\ManagerRegistry as RegistryInterface;
class BrandRepository extends \Eccube\Repository\AbstractRepository
{
public function __construct(RegistryInterface $registry)
{
parent::__construct($registry, Brand::class);
}
/**
* ブランドを保存する.
*
* @param Brand $brand ブランド
*/
public function save($brand)
{
if (!$brand->getId()) {
$sortNoTop = $this->findOneBy([], ['sort_no' => 'DESC']);
$sort_no = 0;
if (!is_null($sortNoTop)) {
$sort_no = $sortNoTop->getSortNo();
}
$brand->setSortNo($sort_no + 1);
}
$em = $this->getEntityManager();
$em->persist($brand);
$em->flush();
}
public function getList()
{
$qb = $this->createQueryBuilder('b');
$Brands = $qb->getQuery()
->useResultCache(true, $this->getCacheLifetime())
->getResult();
return $Brands;
}
/**
* ブランドを削除する.
*
* @param Brand $Brand 削除対象のタグ
*/
public function delete($Brand)
{
$em = $this->getEntityManager();
$em->beginTransaction();
$em->createQuery("DELETE \Customize\Entity\Brand b WHERE b.id = :brand")->execute(['brand' => $Brand]);
$this
->createQueryBuilder('b')
->update()
->set('b.sort_no', 'b.sort_no - 1')
->where('b.sort_no > :sort_no')
->setParameter('sort_no', $Brand->getSortNo())
->getQuery()
->execute();
$em->remove($Brand);
$em->flush();
$em->commit();
}
}