Guest User

Untitled

a guest
Apr 26th, 2018
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.67 KB | None | 0 0
  1. <?php
  2.  
  3. namespace App\Doctrine\ORM\Repository;
  4.  
  5. use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
  6. use Doctrine\ORM\QueryBuilder;
  7.  
  8. abstract class AbstractRepository extends ServiceEntityRepository
  9. {
  10. /**
  11. * Parses a nested array and adds expressions to a query builder
  12. *
  13. * Example Array:
  14. *
  15. * Input:
  16. *
  17. * $criteria = [
  18. * 'condition' => 'andX',
  19. * 'expressions' => [
  20. * [
  21. * 'propertyPath' => 'taskList.project.id',
  22. * 'operator' => 'eq',
  23. * 'value' => '1',
  24. * ],
  25. * [
  26. * 'propertyPath' => 'taskList.project.workspace.id',
  27. * 'operator' => 'eq',
  28. * 'value' => '1',
  29. * ],
  30. * ],
  31. * ];
  32. *
  33. * DQL:
  34. *
  35. * SELECT Task
  36. * FROM App\Entity\Task Task
  37. * INNER JOIN Task.taskList taskList
  38. * INNER JOIN Task.project taskList_project
  39. * INNER JOIN Task.workspace taskList_project_workspace
  40. * WHERE taskList_project.id = 1
  41. * AND taskList_project_workspace.id = 1
  42. *
  43. * @param QueryBuilder $qb
  44. * @param array $criteria
  45. * @param null $alias
  46. */
  47. public function filterByCriteria(QueryBuilder $qb, array $criteria, $alias = null)
  48. {
  49. $alias = $alias ?: $qb->getRootAliases()[0];
  50. $expressionBuilder = $this->getEntityManager()->getExpressionBuilder();
  51. $expressions = [];
  52.  
  53. foreach ($criteria['expressions'] as $expression) {
  54. if (key_exists('condition', $expression)) {
  55. $expressions[] = $this->filterByCriteria($qb, $expression, $alias);
  56. } else {
  57.  
  58. // Get the operator and value
  59. switch ($expression['operator']) {
  60. case 'eq':
  61. case 'neq':
  62. case 'lt':
  63. case 'lte':
  64. case 'gt':
  65. case 'gte':
  66. case 'between':
  67. case 'isNull':
  68. case 'isNotNull':
  69. break;
  70. case 'startsWith':
  71. $expression['operator'] = 'LIKE';
  72. $expression['value'] = $expression['value'] . '%';
  73. break;
  74. case 'endsWith':
  75. $expression['operator'] = 'LIKE';
  76. $expression['value'] = '%' . $expression['value'];
  77. break;
  78. case 'contains':
  79. $expression['operator'] = 'LIKE';
  80. $expression['value'] = '%' . $expression['value'] . '%';
  81. break;
  82. }
  83.  
  84. // Explode the property path into it's parts
  85. $propertyPathParts = explode('.', $expression['propertyPath']);
  86. // Pop the property off the end
  87. $property = array_pop($propertyPathParts);
  88.  
  89. /*
  90. * Property is a dot delimited list.
  91. * We need to make sure each alias is added
  92. * These next few lines add the joins if required
  93. *
  94. * - task.taskList -> `$qb->join(task.taskList taskList)`
  95. * - task.taskList.project -> `$qb->join(taskList.project taskList_project)`
  96. * - task.taskList.project.workspace -> `$qb->oin(taskList_project.workspace taskList_project_workspace)`
  97. */
  98.  
  99. $joinAlias = $alias;
  100. foreach($propertyPathParts as $count => $propertyPart) {
  101. $join = sprintf('%s.%s', $alias, $propertyPart);
  102. $joinAlias = implode("_", array_slice($propertyPathParts, 0, $count+1));
  103. if(!in_array($joinAlias, $qb->getAllAliases())) {
  104. $qb->join($join, $joinAlias);
  105. }
  106. }
  107.  
  108. // Now we have the final alias
  109. // Join the alias and the property together
  110. $expression['propertyPath'] = sprintf('%s.%s', $joinAlias, $property);
  111. // Create the arguments
  112. $expressionArguments = [$expression['propertyPath'], $expression['value']];
  113. // Create the expression
  114. $expressions[] = call_user_func_array(
  115. [$expressionBuilder, $expression['operator']],
  116. $expressionArguments
  117. );
  118. }
  119. }
  120.  
  121. // Apply all the expressions
  122. $expression = call_user_func_array([
  123. $expressionBuilder,
  124. $criteria['condition'],
  125. ], $expressions);
  126.  
  127. $qb->andWhere($expression);
  128. }
  129. }
Add Comment
Please, Sign In to add comment