QueryInterface.php 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. <?php
  2. namespace Drupal\Core\Entity\Query;
  3. use Drupal\Core\Database\Query\AlterableInterface;
  4. /**
  5. * Interface for entity queries.
  6. *
  7. * Never instantiate classes implementing this interface directly. Always use
  8. * the QueryFactory class.
  9. *
  10. * @ingroup database
  11. */
  12. interface QueryInterface extends AlterableInterface {
  13. /**
  14. * Gets the ID of the entity type for this query.
  15. *
  16. * @return string
  17. */
  18. public function getEntityTypeId();
  19. /**
  20. * Add a condition to the query or a condition group.
  21. *
  22. * For example, to find all entities containing both the Turkish 'merhaba'
  23. * and the Polish 'siema' within a 'greetings' text field:
  24. * @code
  25. * $entity_ids = \Drupal::entityQuery($entity_type)
  26. * ->condition('greetings', 'merhaba', '=', 'tr')
  27. * ->condition('greetings.value', 'siema', '=', 'pl')
  28. * ->execute();
  29. * @endcode
  30. *
  31. * @param $field
  32. * Name of the field being queried. It must contain a field name, optionally
  33. * followed by a column name. The column can be the reference property,
  34. * usually "entity", for reference fields and that can be followed
  35. * similarly by a field name and so on. Additionally, the target entity type
  36. * can be specified by appending the ":target_entity_type_id" to "entity".
  37. * Some examples:
  38. * - nid
  39. * - tags.value
  40. * - tags
  41. * - tags.entity.name
  42. * - tags.entity:taxonomy_term.name
  43. * - uid.entity.name
  44. * - uid.entity:user.name
  45. * "tags" "is the same as "tags.value" as value is the default column.
  46. * If two or more conditions have the same field names they apply to the
  47. * same delta within that field. In order to limit the condition to a
  48. * specific item a numeric delta should be added between the field name and
  49. * the column name.
  50. * @code
  51. * ->condition('tags.5.value', 'news')
  52. * @endcode
  53. * This will require condition to be satisfied on a specific delta of the
  54. * field. The condition above will require the 6th value of the field to
  55. * match the provided value. Further, it's possible to create a condition on
  56. * the delta itself by using '%delta'. For example,
  57. * @code
  58. * ->condition('tags.%delta', 5)
  59. * @endcode
  60. * will find only entities which have at least six tags. Finally, the
  61. * condition on the delta itself accompanied with a condition on the value
  62. * will require the value to appear in the specific delta range. For
  63. * example,
  64. * @code
  65. * ->condition('tags.%delta', 0, '>'))
  66. * ->condition('tags.%delta.value', 'news'))
  67. * @endcode
  68. * will only find the "news" tag if it is not the first value. It should be
  69. * noted that conditions on specific deltas and delta ranges are only
  70. * supported when querying content entities.
  71. * @param $value
  72. * The value for $field. In most cases, this is a scalar and it's treated as
  73. * case-insensitive. For more complex operators, it is an array. The meaning
  74. * of each element in the array is dependent on $operator.
  75. * @param $operator
  76. * Possible values:
  77. * - '=', '<>', '>', '>=', '<', '<=', 'STARTS_WITH', 'CONTAINS',
  78. * 'ENDS_WITH': These operators expect $value to be a literal of the
  79. * same type as the column.
  80. * - 'IN', 'NOT IN': These operators expect $value to be an array of
  81. * literals of the same type as the column.
  82. * - 'BETWEEN': This operator expects $value to be an array of two literals
  83. * of the same type as the column.
  84. * @param $langcode
  85. * Language code (optional). If omitted, any translation satisfies the
  86. * condition. However, if two or more conditions omit the langcode within
  87. * one condition group then they are presumed to apply to the same
  88. * translation. If within one condition group one condition has a langcode
  89. * and another does not they are not presumed to apply to the same
  90. * translation.
  91. *
  92. * @return \Drupal\Core\Entity\Query\QueryInterface
  93. * @see \Drupal\Core\Entity\Query\andConditionGroup
  94. * @see \Drupal\Core\Entity\Query\orConditionGroup
  95. */
  96. public function condition($field, $value = NULL, $operator = NULL, $langcode = NULL);
  97. /**
  98. * Queries for a non-empty value on a field.
  99. *
  100. * @param $field
  101. * Name of a field.
  102. * @param $langcode
  103. * Language code (optional).
  104. * @return \Drupal\Core\Entity\Query\QueryInterface
  105. */
  106. public function exists($field, $langcode = NULL);
  107. /**
  108. * Queries for an empty field.
  109. *
  110. * @param $field
  111. * Name of a field.
  112. * @param $langcode
  113. * Language code (optional).
  114. * @return \Drupal\Core\Entity\Query\QueryInterface
  115. */
  116. public function notExists($field, $langcode = NULL);
  117. /**
  118. * Enables a pager for the query.
  119. *
  120. * @param $limit
  121. * An integer specifying the number of elements per page. If passed a false
  122. * value (FALSE, 0, NULL), the pager is disabled.
  123. * @param $element
  124. * An optional integer to distinguish between multiple pagers on one page.
  125. * If not provided, one is automatically calculated.
  126. *
  127. * @return \Drupal\Core\Entity\Query\QueryInterface
  128. * The called object.
  129. */
  130. public function pager($limit = 10, $element = NULL);
  131. /**
  132. * @param null $start
  133. * @param null $length
  134. * @return \Drupal\Core\Entity\Query\QueryInterface
  135. * The called object.
  136. */
  137. public function range($start = NULL, $length = NULL);
  138. /**
  139. * @param $field
  140. * Name of a field.
  141. * @param string $direction
  142. * @param $langcode
  143. * Language code (optional).
  144. * @return \Drupal\Core\Entity\Query\QueryInterface
  145. * The called object.
  146. */
  147. public function sort($field, $direction = 'ASC', $langcode = NULL);
  148. /**
  149. * Makes this a count query.
  150. *
  151. * For count queries, execute() returns the number entities found.
  152. *
  153. * @return \Drupal\Core\Entity\Query\QueryInterface
  154. * The called object.
  155. */
  156. public function count();
  157. /**
  158. * Enables sortable tables for this query.
  159. *
  160. * @param $headers
  161. * An array of headers of the same structure as described in
  162. * template_preprocess_table(). Use a 'specifier' in place of a 'field' to
  163. * specify what to sort on. This can be an entity or a field as described
  164. * in condition().
  165. *
  166. * @return \Drupal\Core\Entity\Query\QueryInterface
  167. * The called object.
  168. */
  169. public function tableSort(&$headers);
  170. /**
  171. * @return \Drupal\Core\Entity\Query\QueryInterface
  172. * The called object.
  173. */
  174. public function accessCheck($access_check = TRUE);
  175. /**
  176. * Execute the query.
  177. *
  178. * @return int|array
  179. * Returns an integer for count queries or an array of ids. The values of
  180. * the array are always entity ids. The keys will be revision ids if the
  181. * entity supports revision and entity ids if not.
  182. */
  183. public function execute();
  184. /**
  185. * Creates a new group of conditions ANDed together.
  186. *
  187. * For example, consider a drawing entity type with a 'figures' multi-value
  188. * field containing 'shape' and 'color' columns. To find all drawings
  189. * containing both a red triangle and a blue circle:
  190. * @code
  191. * $query = \Drupal::entityQuery('drawing');
  192. * $group = $query->andConditionGroup()
  193. * ->condition('figures.color', 'red')
  194. * ->condition('figures.shape', 'triangle');
  195. * $query->condition($group);
  196. * $group = $query->andConditionGroup()
  197. * ->condition('figures.color', 'blue')
  198. * ->condition('figures.shape', 'circle');
  199. * $query->condition($group);
  200. * $entity_ids = $query->execute();
  201. * @endcode
  202. *
  203. * @return \Drupal\Core\Entity\Query\ConditionInterface
  204. */
  205. public function andConditionGroup();
  206. /**
  207. * Creates a new group of conditions ORed together.
  208. *
  209. * For example, consider a map entity with an 'attributes' field
  210. * containing 'building_type' and 'color' columns. To find all green and
  211. * red bikesheds:
  212. * @code
  213. * $query = \Drupal::entityQuery('map');
  214. * $group = $query->orConditionGroup()
  215. * ->condition('attributes.color', 'red')
  216. * ->condition('attributes.color', 'green');
  217. * $entity_ids = $query
  218. * ->condition('attributes.building_type', 'bikeshed')
  219. * ->condition($group)
  220. * ->execute();
  221. * @endcode
  222. * Note that this particular example can be simplified:
  223. * @code
  224. * $entity_ids = $query
  225. * ->condition('attributes.color', array('red', 'green'))
  226. * ->condition('attributes.building_type', 'bikeshed')
  227. * ->execute();
  228. * @endcode
  229. *
  230. * @return \Drupal\Core\Entity\Query\ConditionInterface
  231. */
  232. public function orConditionGroup();
  233. /**
  234. * Queries the current revision.
  235. *
  236. * @return $this
  237. */
  238. public function currentRevision();
  239. /**
  240. * Queries the latest revision.
  241. *
  242. * The latest revision is the most recent revision of an entity. This will be
  243. * either the default revision, or a pending revision if one exists and it is
  244. * newer than the default.
  245. *
  246. * @return $this
  247. */
  248. public function latestRevision();
  249. /**
  250. * Queries all the revisions.
  251. *
  252. * @return $this
  253. */
  254. public function allRevisions();
  255. }