DocumentRepository.php 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. <?php
  2. namespace AppBundle\Entity\Core\Repository;
  3. use AppBundle\Entity\Organization\Organization;
  4. use AppBundle\Entity\AccessAndFunction\Access;
  5. use Doctrine\ORM\EntityRepository;
  6. /**
  7. * Description of DocumentRepository
  8. *
  9. * @author sebastienhupin
  10. */
  11. class DocumentRepository extends EntityRepository {
  12. /**
  13. * Find the template
  14. *
  15. * @param type $name
  16. * @param Access $author
  17. * @param Organization $organization
  18. */
  19. public function getTemplate($name, Access $author = null, Organization $organization = null) {
  20. $em = $this->getEntityManager();
  21. $qb = $em->createQueryBuilder('t');
  22. $expr = $qb->expr();
  23. $qb->select('t');
  24. $qb->from($this->getClassName(), 't');
  25. $qb->where($expr->eq('t.slug',':name'))
  26. ->andWhere($expr->eq('t.template', ':template'))
  27. ->andWhere($expr->orX(
  28. $expr->eq('t.author', ':author'),
  29. $expr->isNull('t.author')
  30. ))
  31. ->andWhere($expr->orX(
  32. $expr->eq('t.organization', ':organization'),
  33. $expr->isNull('t.organization')
  34. ));
  35. $qb->orderBy('t.author', 'DESC')
  36. ->addOrderBy('t.organization', 'DESC');
  37. $qb->setMaxResults(1);
  38. $qb->setParameter('name', $name);
  39. $qb->setParameter('template', true);
  40. $qb->setParameter('author', $author);
  41. $qb->setParameter('organization', $organization);
  42. $result = $qb->getQuery()->getOneOrNullResult();
  43. return $result;
  44. }
  45. }