<?php
namespace App\Core\CoreBundle\Manager;
use App\Core\CoreBundle\Traits\Containerable;
use Doctrine\Common\Persistence\ObjectRepository;
use Doctrine\ORM\EntityManagerInterface;
abstract class BaseManager
{
use Containerable;
/** @var ObjectRepository */
protected $repo;
protected $em;
public function __construct(EntityManagerInterface $em)
{
$this->em = $em;
}
public function removeAndFlush($entity)
{
$this->em->remove($entity);
$this->em->flush();
}
/**
* @return ObjectRepository
*/
public function getRepository()
{
return $this->repo;
}
/**
* @return EntityManagerInterface
*/
public function getManager()
{
return $this->em;
}
public function setRepository($repository)
{
$this->repo = $this->em->getRepository($repository);
}
public function findMany($criterias = array(), $orders = array(), $numbers = array(), $options = array())
{
$qb = $this->repo->findMany($criterias, $orders, $numbers, $options);
return (!isset($options['_locale'])) ? $this->repo->getManyResult($qb) : $this->repo->getManyResult($qb, $options['_locale']);
}
public function findOne($criterias = array(), $options = array())
{
$qb = $this->repo->findOne($criterias, $options);
return (!isset($options['_locale'])) ? $this->repo->getOneResult($qb) : $this->repo->getOneResult($qb, $options['_locale']);
}
public function find($id)
{
return $this->repo->find((int)$id);
}
public function findAll()
{
return $this->repo->findAll();
}
public function findBy($criterias = [], $orderBy = [], $limit = null, $offset = null)
{
return $this->repo->findBy($criterias, $orderBy, $limit, $offset);
}
public function findOneBy($criterias = [], $orderBy = [])
{
return $this->repo->findOneBy($criterias, $orderBy);
}
public function updateVisite($object, $_locale)
{
$object->setLocale($_locale);
$object->setCount($object->getCount() + 1);
$this->persistAndFlush($object);
}
public function persistAndFlush($entity)
{
$this->em->persist($entity);
$this->em->flush();
}
public function query($query)
{
$this->query = $query;
$this->statement = $this->em->getConnection()->prepare($query);
$this->statement->execute();
return $this;
}
public function getResult()
{
return $this->statement->fetchAll();
}
private function getLastID($tableName)
{
$dbName = getenv('DATABASE_NAME');
$sql = "SELECT `AUTO_INCREMENT` FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = '$dbName' AND TABLE_NAME = '$tableName'";
$cnx = $this->em->getConnection();
$stmt = $cnx->prepare($sql);
$stmt->execute();
$fetchs = $stmt->fetchAll();
return (int)$fetchs[0]['AUTO_INCREMENT'];
}
}