You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

AbstractRepositoryQuery.php 6.4KB

3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. <?php
  2. namespace Lc\SovBundle\Repository;
  3. use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
  4. use Doctrine\ORM\QueryBuilder;
  5. use Knp\Component\Pager\PaginatorInterface;
  6. use Lc\SovBundle\Doctrine\EntityInterface;
  7. use Lc\SovBundle\Model\User\UserInterface;
  8. abstract class AbstractRepositoryQuery implements RepositoryQueryInterface
  9. {
  10. protected ServiceEntityRepository $repository;
  11. protected QueryBuilder $query;
  12. protected PaginatorInterface $paginator;
  13. protected string $id;
  14. public function __construct(ServiceEntityRepository $repository, string $id, PaginatorInterface $paginator = null)
  15. {
  16. $this->repository = $repository;
  17. $this->query = $repository->createQueryBuilder($id);
  18. $this->paginator = $paginator;
  19. $this->id = $id;
  20. }
  21. public function __call(string $name, $params): self
  22. {
  23. foreach ($params as $key => $value) {
  24. $this->populateDqlId($params[$key]);
  25. }
  26. call_user_func_array([$this->query, $name], $params);
  27. return $this;
  28. }
  29. public function create()
  30. {
  31. $class = get_called_class();
  32. return new $class($this->repository, $this->paginator);
  33. }
  34. public function call(callable $fn): self
  35. {
  36. $fn($this->query, $this);
  37. return $this;
  38. }
  39. public function count()
  40. {
  41. return $this->query->getQuery()
  42. ->getSingleScalarResult();
  43. }
  44. public function findOne()
  45. {
  46. return $this->query->getQuery()
  47. ->setMaxResults(1)
  48. ->getOneOrNullResult();
  49. }
  50. public function find(): array
  51. {
  52. return $this->query->getQuery()->getResult();
  53. }
  54. public function limit(int $maxResults):self
  55. {
  56. $this->query->setMaxResults($maxResults);
  57. return $this;
  58. }
  59. public function paginate(int $page = 1, int $limit = 20)
  60. {
  61. return $this->paginator->paginate($this->query->getQuery(), $page, $limit);
  62. }
  63. public function getRepository(): ServiceEntityRepository
  64. {
  65. return $this->repository;
  66. }
  67. public function getQueryBuilder(): QueryBuilder
  68. {
  69. return $this->query;
  70. }
  71. protected function populateDqlId(&$data)
  72. {
  73. if (is_string($data)) {
  74. $words = explode(' ', $data);
  75. foreach ($words as $k => $v) {
  76. if (isset($v[0]) && '.' === $v[0]) {
  77. $words[$k] = $this->id . $v;
  78. }
  79. }
  80. $data = implode(' ', $words);
  81. } elseif (is_array($data)) {
  82. foreach ($data as $k => $v) {
  83. $this->populateDqlId($data[$k]);
  84. }
  85. }
  86. return $data;
  87. }
  88. public function groupBy(string $field): self
  89. {
  90. if (strpos($field, '.')!==false) {
  91. $this->addGroupBy($field) ;
  92. } else {
  93. $this->addGroupBy('.'.$field) ;
  94. }
  95. return $this;
  96. }
  97. /*
  98. public function addGroupBy(string $field): self
  99. {
  100. if (strpos($field, '.')!==false) {
  101. $this->query->addGroupBy($field) ;
  102. } else {
  103. $this->query->addGroupBy('.'.$field) ;
  104. }
  105. return $this;
  106. }*/
  107. public function orderBy(string $field, string $sort = 'ASC'): self
  108. {
  109. if (strpos($field, '.')!==false) {
  110. return $this->addOrderBy($field, $sort);
  111. } else {
  112. return $this->addOrderBy('.' . $field, $sort);
  113. }
  114. }
  115. public function setOrderBy(string $field, string $sort = 'ASC'): self
  116. {
  117. $this->resetDQLParts(['orderBy']);
  118. return $this->orderBy($field, $sort);
  119. }
  120. public function filterById(int $id):self
  121. {
  122. return $this
  123. ->andWhere('.id = :id')
  124. ->setParameter('id', $id);
  125. }
  126. public function filterByCreatedBy(UserInterface $user):self
  127. {
  128. return $this
  129. ->andWhere('.createdBy = :user')
  130. ->setParameter('user', $user);
  131. }
  132. public function filterByUpdatedBy(UserInterface $user):self
  133. {
  134. return $this
  135. ->andWhere('.updatedBy = :user')
  136. ->setParameter('user', $user);
  137. }
  138. public function andWhereEqual($field, $value)
  139. {
  140. return $this->andWhere('.'.$field.' = :'.$field)->setParameter($field, $value);
  141. }
  142. public function filterByOldUrl(string $oldUrl): self
  143. {
  144. return $this->andWhere('.oldUrls LIKE :oldUrl')->setParameter('oldUrl', '%'.$oldUrl.'%');
  145. }
  146. public function resetRelationsJoin(): void
  147. {
  148. }
  149. /*
  150. * DEVALIAS
  151. */
  152. public function filterByDevAlias(string $devAlias): self
  153. {
  154. return $this
  155. ->andWhere('.devAlias = :devAlias')
  156. ->setParameter('devAlias', $devAlias);
  157. }
  158. /*
  159. * SLUG
  160. */
  161. public function filterBySlug(string $slug):self
  162. {
  163. return $this
  164. ->andWhere('.slug = :slug')
  165. ->setParameter('slug', $slug);
  166. }
  167. /*
  168. * TREE
  169. */
  170. public function filterIsParent():self
  171. {
  172. return $this->andWhere('.parent IS NULL');
  173. }
  174. public function filterIsChildren():self
  175. {
  176. return $this->andWhere('.parent IS NOT NULL');
  177. }
  178. public function filterByParent(EntityInterface $parent = null):self
  179. {
  180. return $this->andWhere('.parent = :parent')->setParameter('parent', $parent);
  181. }
  182. /*
  183. * STATUS
  184. */
  185. public function filterIsOffline():self
  186. {
  187. return $this->andWhereStatus($this->id, 0);
  188. }
  189. public function filterIsOnline():self
  190. {
  191. return $this->andWhereStatus($this->id, 1);
  192. }
  193. public function filterIsDeleted():self
  194. {
  195. return $this->andWhereStatus($this->id, -1);
  196. }
  197. public function filterIsOnlineAndOffline():self
  198. {
  199. return $this->andWhere('.status >= 0');
  200. }
  201. /*
  202. * POSITION
  203. */
  204. public function filterByPositionBiggerThan(int $position)
  205. {
  206. return $this->andWhere('.position > :position')->setParameter('position', $position);
  207. }
  208. public function filterByPositionSmallerThan(int $position)
  209. {
  210. return $this->andWhere('.position < :position')->setParameter('position', $position);
  211. }
  212. public function enableCache($lifetime, $idCache)
  213. {
  214. return $this->getQueryBuilder()->getQuery()
  215. ->useQueryCache(true)
  216. ->setQueryCacheLifetime($lifetime)
  217. ->enableResultCache($lifetime, $idCache);
  218. }
  219. }