QueryAggregateInterface.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. <?php
  2. namespace Drupal\Core\Entity\Query;
  3. /**
  4. * Defines a interface for aggregated entity queries.
  5. */
  6. interface QueryAggregateInterface extends QueryInterface {
  7. /**
  8. * Specifies a field and a function to aggregate on.
  9. *
  10. * Available functions: SUM, AVG, MIN, MAX and COUNT.
  11. *
  12. * @todo What about GROUP_CONCAT support?
  13. *
  14. * @param string $field
  15. * The name of the field to aggregate by.
  16. * @param string $function
  17. * The aggregation function, for example COUNT or MIN.
  18. * @param string $langcode
  19. * (optional) The language code.
  20. * @param string $alias
  21. * (optional) The key that will be used on the resultset.
  22. *
  23. * @return $this
  24. * The called object.
  25. */
  26. public function aggregate($field, $function, $langcode = NULL, &$alias = NULL);
  27. /**
  28. * Specifies the field to group on.
  29. *
  30. * @param string $field
  31. * The name of the field to group by.
  32. *
  33. * @return $this
  34. * The called object.
  35. */
  36. public function groupBy($field);
  37. /**
  38. * Sets a condition for an aggregated value.
  39. *
  40. * @param string $field
  41. * The name of the field to aggregate by.
  42. * @param string $function
  43. * The aggregation function, for example COUNT or MIN.
  44. * @param mixed $value
  45. * The actual value of the field.
  46. * @param $operator
  47. * Possible values:
  48. * - '=', '<>', '>', '>=', '<', '<=', 'STARTS_WITH', 'CONTAINS',
  49. * 'ENDS_WITH': These operators expect $value to be a literal of the
  50. * same type as the column.
  51. * - 'IN', 'NOT IN': These operators expect $value to be an array of
  52. * literals of the same type as the column.
  53. * - 'BETWEEN': This operator expects $value to be an array of two literals
  54. * of the same type as the column.
  55. * @param string $langcode
  56. * (optional) The language code.
  57. *
  58. * @return $this
  59. * The called object.
  60. *
  61. * @see \Drupal\Core\Entity\Query\QueryInterface::condition()
  62. */
  63. public function conditionAggregate($field, $function = NULL, $value = NULL, $operator = '=', $langcode = NULL);
  64. /**
  65. * Queries for the existence of a field.
  66. *
  67. * @param string $field
  68. * The name of the field.
  69. * @param string $function
  70. * The aggregate function.
  71. * @param $langcode
  72. * (optional) The language code.
  73. *
  74. * @return $this
  75. * The called object.
  76. */
  77. public function existsAggregate($field, $function, $langcode = NULL);
  78. /**
  79. * Queries for the nonexistence of a field.
  80. *
  81. * @param string $field
  82. * The name of a field.
  83. * @param string $function
  84. * The aggregate function.
  85. * @param string $langcode
  86. * (optional) The language code.
  87. *
  88. * @return $this
  89. * The called object.
  90. */
  91. public function notExistsAggregate($field, $function, $langcode = NULL);
  92. /**
  93. * Creates an object holding a group of conditions.
  94. *
  95. * See andConditionAggregateGroup() and orConditionAggregateGroup() for more.
  96. *
  97. * @param string $conjunction
  98. * - AND (default): this is the equivalent of andConditionAggregateGroup().
  99. * - OR: this is the equivalent of andConditionAggregateGroup().
  100. *
  101. * @return ConditionInterface
  102. * An object holding a group of conditions.
  103. */
  104. public function conditionAggregateGroupFactory($conjunction = 'AND');
  105. /**
  106. * Sorts by an aggregated value.
  107. *
  108. * @param string $field
  109. * The name of a field.
  110. * @param string $function
  111. * The aggregate function. This is only marked optional for interface
  112. * compatibility, it is illegal to leave it out.
  113. * @param string $direction
  114. * The order of sorting, either DESC for descending of ASC for ascending.
  115. * @param string $langcode
  116. * (optional) The language code.
  117. *
  118. * @return $this
  119. * The called object.
  120. */
  121. public function sortAggregate($field, $function, $direction = 'ASC', $langcode = NULL);
  122. /**
  123. * Executes the aggregate query.
  124. *
  125. * @return array
  126. * A list of result row arrays. Each result row contains the aggregate
  127. * results as keys and also the groupBy columns as keys:
  128. * @code
  129. * $result = $query
  130. * ->aggregate('nid', 'count')
  131. * ->condition('status', 1)
  132. * ->groupby('type')
  133. * ->executeAggregate();
  134. * @endcode
  135. * Will return:
  136. * @code
  137. * $result[0] = array('count_nid' => 3, 'type' => 'page');
  138. * $result[1] = array('count_nid' => 1, 'type' => 'poll');
  139. * $result[2] = array('count_nid' => 4, 'type' => 'story');
  140. * @endcode
  141. */
  142. public function execute();
  143. }