Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- namespace Fcm\Entity\Repository;
- use DoctrineORMModule\Paginator\Adapter\DoctrinePaginator as PaginatorAdapter;
- use Doctrine\ORM\Tools\Pagination\Paginator as ORMPaginator;
- use Zend\Paginator\Paginator as ZendPaginator;
- use Doctrine\ORM\EntityRepository;
- class ClientRepository extends EntityRepository implements \Doctrine\Common\Persistence\ObjectRepository
- {
- /**
- * @var \Zend\Escaper\Escaper
- */
- private static $escaperInstance = NULL;
- // Pattern Singleton for EscaperInstance
- public static function getEscaperInstance()
- {
- if (!self::$escaperInstance)
- self::$escaperInstance = new \Zend\Escaper\Escaper('utf-8');
- return self::$escaperInstance;
- }
- /**
- * Display all Entities with pagination and sorting system
- *
- * @param string $order_by the field to sort
- * @param string $order the type of sorting (ASC or DESC)
- * @param integer $page the current page
- *
- * @return object ZendPaginator
- */
- public function displayAllEntities($order_by, $order, $page)
- {
- // Creation query
- $qb = $this->getEntityManager()->createQueryBuilder();
- $qb->add('select', 'c')
- ->add('from', 'Fcm\Entity\client c')
- ->add('orderBy', 'c.id DESC');
- if ($order_by != null) {
- if ($order == 'ASC') {
- $qb->add('orderBy', 'c.' . self::getEscaperInstance()->escapeHtmlAttr($order_by) . ' ASC');
- } elseif ($order == 'DESC') {
- $qb->add('orderBy', 'c.' . self::getEscaperInstance()->escapeHtmlAttr($order_by) . ' DESC');
- }
- }
- $qb->setFirstResult(1)
- ->setMaxResults(5);
- $query = $qb->getQuery();
- // Creation paginator
- $paginator = new ZendPaginator(new PaginatorAdapter(new ORMPaginator($query)));
- $paginator
- ->setCurrentPageNumber($page)
- ->setItemCountPerPage(5)
- ->setPageRange(10);
- return $paginator;
- }
- /**
- * Search Sytem
- *
- * @param string $order_by the field to sort
- * @param string $order the type of sorting (ASC or DESC)
- * @param integer $page the current page
- *
- * @return object ZendPaginator
- */
- public function search($key)
- {
- //Creation query
- $qb = $this->getEntityManager()->createQueryBuilder();
- $qb->add('select', 'c')
- ->add('from', 'Fcm\Entity\client c')
- ->add('orderBy', 'c.id ASC');
- $qb->orWhere('c.id LIKE :key')
- ->orWhere('c.name LIKE :key')
- ->orWhere('c.website LIKE :key')
- ->orWhere('c.phone LIKE :key')
- ->orWhere('c.email LIKE :key')
- ->orWhere('c.contact LIKE :key')
- ->orWhere('c.comment LIKE :key')
- ->setParameter('key', '%' . $key . '%');
- $qb->setFirstResult(1)
- ->setMaxResults(5);
- $query = $qb->getQuery();
- // Creation paginator
- $paginator = new ZendPaginator(new PaginatorAdapter(new ORMPaginator($query)));
- $paginator
- ->setCurrentPageNumber(1)
- ->setItemCountPerPage(5)
- ->setPageRange(10);
- return $paginator;
- }
- /**
- * Finds an entity by its primary key / identifier.
- *
- * @param string $order_by the field to sort
- *
- * @return object
- */
- public function findEntity($id)
- {
- $client = $this->getEntityManager()->find("Fcm\Entity\Client", $id);
- // $campaign = $this->getEntityManager()->find("Fcm\Entity\Campaign", 1);
- // $campaign->client = $client;
- // $em = $this->getEntityManager();
- // $em->persist($campaign);
- // $em->flush();
- // die(var_dump($campaign->typeCampaign->type));
- return $client;
- }
- /**
- * Finds all entities as a array
- *
- * @return Array
- */
- public function findAllNameId($client_id = null)
- {
- if ($client_id != null) {
- $query = $this->getEntityManager()->createQuery('SELECT c.id,c.name FROM Fcm\Entity\Client c WHERE c.id = :client_id');
- $query->setParameter('client_id', $client_id);
- } else {
- $query = $this->getEntityManager()->createQuery('SELECT c.id,c.name FROM Fcm\Entity\Client c');
- }
- $entities = $query->getArrayResult();
- return $entities;
- }
- /**
- */
- public function checkIfEmailExists($email)
- {
- $query = $this->getEntityManager()->createQuery('SELECT c.email FROM Fcm\Entity\Client c WHERE c.email= :email');
- $query->setParameter('email', $email);
- return $query->getResult();
- }
- /**
- * Finds an entity by its primary key / identifier.
- *
- * @param string $order_by the field to sort
- *
- * @return object
- */
- public function insert(array $data)
- {
- $entity = $this->getEntityName();
- $entity = new $entity;
- $entity->populate($data);
- $em = $this->getEntityManager();
- $em->persist($entity);
- $em->flush();
- return $entity;
- }
- /**
- * Update an entity
- *
- * @param array $data
- *
- * @return object
- */
- public function update(array $data)
- {
- $entity = $this->getReference($data['id']);
- $entity->populate($data);
- $em = $this->getEntityManager();
- $em->persist($entity);
- $em->flush();
- return $entity;
- }
- /**
- * Delete an entity by its primary key / identifier.
- *
- * @param string $order_by the field to sort
- *
- * @return object
- */
- public function delete($id)
- {
- $entity = $this->getReference($id);
- if ($entity) {
- $em = $this->getEntityManager();
- $em->remove($entity);
- $em->flush();
- return $entity;
- }
- }
- /**
- * Create entity
- *
- * @param array $data entity data
- *
- * @return object
- */
- protected function createEntity(array $data)
- {
- $entity = $this->getEntityName();
- return new $entity($data);
- }
- /**
- * Get reference entity
- *
- * @param integer $id id entity
- *
- * @return object
- */
- protected function getReference($id, $entity = null)
- {
- if (null === $entity) {
- $entity = $this->getEntityName();
- }
- return $this->getEntityManager()->getReference($entity, $id);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement