src/AdminBundle/Repository/BaseRepository.php line 56

Open in your IDE?
  1. <?php
  2. namespace AdminBundle\Repository;
  3. use Doctrine\ORM\EntityRepository;
  4. use Symfony\Component\DependencyInjection\ContainerInterface;
  5. use Symfony\Component\DependencyInjection\ContainerAwareInterface;
  6. class BaseRepository extends EntityRepository implements ContainerAwareInterface
  7. {
  8. protected $container;
  9. public function setContainer(ContainerInterface $container = null)
  10. {
  11. $this->container = $container;
  12. }
  13. public function persist($object) {
  14. $this->_em->persist($object);
  15. }
  16. public function flush() {
  17. $this->_em->flush();
  18. }
  19. /**
  20. * @param array $fields
  21. * @param null $limit
  22. * @param null $offset
  23. * @param null $orderBy
  24. * @return array
  25. */
  26. public function findAllArray($fields = array(), $limit = null, $offset = null, $orderBy = null){
  27. $queryBuilder = $this->createQueryBuilder("a");
  28. if (!empty($fields)) {
  29. foreach ($fields as $field) {
  30. $selectFields[] = 'a.'.$field;
  31. }
  32. $queryBuilder->select($selectFields);
  33. }
  34. if ($orderBy != null) {
  35. $queryBuilder->orderBy("a.".key($orderBy), current($orderBy));
  36. }
  37. $queryBuilder->setFirstResult($offset)
  38. ->setMaxResults($limit);
  39. return $queryBuilder->getQuery()->getArrayResult();
  40. }
  41. /**
  42. * @param array $criteria
  43. * @param array|null $orderBy
  44. * @return mixed
  45. * @throws \Doctrine\ORM\NoResultException
  46. * @throws \Doctrine\ORM\NonUniqueResultException
  47. */
  48. public function findOneByArray(array $criteria, array $orderBy = null)
  49. {
  50. $queryBuilder = $this->createQueryBuilder("a");
  51. foreach($criteria as $key => $item){
  52. $queryBuilder->andWhere("a." . $key . "= :" . $key)
  53. ->setParameter($key, $item);
  54. }
  55. if ($orderBy != null) {
  56. $queryBuilder->orderBy("a.".key($orderBy), current($orderBy));
  57. }
  58. return $queryBuilder->getQuery()->getOneOrNullResult(\Doctrine\ORM\Query::HYDRATE_ARRAY);
  59. }
  60. public function save($object) {
  61. $this->_em->persist($object);
  62. $this->_em->flush();
  63. }
  64. public function remove($object) {
  65. $this->_em->remove($object);
  66. $this->_em->flush();
  67. }
  68. }