src/Core/CoreBundle/Manager/BaseManager.php line 48

Open in your IDE?
  1. <?php
  2. namespace App\Core\CoreBundle\Manager;
  3. use App\Core\CoreBundle\Traits\Containerable;
  4. use Doctrine\Common\Persistence\ObjectRepository;
  5. use Doctrine\ORM\EntityManagerInterface;
  6. abstract class BaseManager
  7. {
  8.     use Containerable;
  9.     /** @var ObjectRepository */
  10.     protected $repo;
  11.     protected $em;
  12.     public function __construct(EntityManagerInterface $em)
  13.     {
  14.         $this->em $em;
  15.     }
  16.     public function removeAndFlush($entity)
  17.     {
  18.         $this->em->remove($entity);
  19.         $this->em->flush();
  20.     }
  21.     /**
  22.      * @return ObjectRepository
  23.      */
  24.     public function getRepository()
  25.     {
  26.         return $this->repo;
  27.     }
  28.     /**
  29.      * @return EntityManagerInterface
  30.      */
  31.     public function getManager()
  32.     {
  33.         return $this->em;
  34.     }
  35.     public function setRepository($repository)
  36.     {
  37.         $this->repo $this->em->getRepository($repository);
  38.     }
  39.     public function findMany($criterias = array(), $orders = array(), $numbers = array(), $options = array())
  40.     {
  41.         $qb $this->repo->findMany($criterias$orders$numbers$options);
  42.         return (!isset($options['_locale'])) ? $this->repo->getManyResult($qb) : $this->repo->getManyResult($qb$options['_locale']);
  43.     }
  44.     public function findOne($criterias = array(), $options = array())
  45.     {
  46.         $qb $this->repo->findOne($criterias$options);
  47.         return (!isset($options['_locale'])) ? $this->repo->getOneResult($qb) : $this->repo->getOneResult($qb$options['_locale']);
  48.     }
  49.     public function find($id)
  50.     {
  51.         return $this->repo->find((int)$id);
  52.     }
  53.     public function findAll()
  54.     {
  55.         return $this->repo->findAll();
  56.     }
  57.     public function findBy($criterias = [], $orderBy = [], $limit null$offset null)
  58.     {
  59.         return $this->repo->findBy($criterias$orderBy$limit$offset);
  60.     }
  61.     public function findOneBy($criterias = [], $orderBy = [])
  62.     {
  63.         return $this->repo->findOneBy($criterias$orderBy);
  64.     }
  65.     public function updateVisite($object$_locale)
  66.     {
  67.         $object->setLocale($_locale);
  68.         $object->setCount($object->getCount() + 1);
  69.         $this->persistAndFlush($object);
  70.     }
  71.     public function persistAndFlush($entity)
  72.     {
  73.         $this->em->persist($entity);
  74.         $this->em->flush();
  75.     }
  76.     public function query($query)
  77.     {
  78.         $this->query $query;
  79.         $this->statement $this->em->getConnection()->prepare($query);
  80.         $this->statement->execute();
  81.         return $this;
  82.     }
  83.     public function getResult()
  84.     {
  85.         return $this->statement->fetchAll();
  86.     }
  87.     private function getLastID($tableName)
  88.     {
  89.         $dbName getenv('DATABASE_NAME');
  90.         $sql "SELECT `AUTO_INCREMENT` FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = '$dbName' AND TABLE_NAME = '$tableName'";
  91.         $cnx $this->em->getConnection();
  92.         $stmt $cnx->prepare($sql);
  93.         $stmt->execute();
  94.         $fetchs $stmt->fetchAll();
  95.         return (int)$fetchs[0]['AUTO_INCREMENT'];
  96.     }
  97. }