index.rst 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. Introduction
  2. ============
  3. Doctrine Collections is a library that contains classes for working with
  4. arrays of data. Here is an example using the simple
  5. ``Doctrine\Common\Collections\ArrayCollection`` class:
  6. .. code-block:: php
  7. <?php
  8. use Doctrine\Common\Collections\ArrayCollection;
  9. $collection = new ArrayCollection([1, 2, 3]);
  10. $filteredCollection = $collection->filter(function($element) {
  11. return $element > 1;
  12. }); // [2, 3]
  13. Collection Methods
  14. ==================
  15. Doctrine Collections provides an interface named ``Doctrine\Common\Collections\Collection``
  16. that resembles the nature of a regular PHP array. That is,
  17. it is essentially an **ordered map** that can also be used
  18. like a list.
  19. A Collection has an internal iterator just like a PHP array. In addition,
  20. a Collection can be iterated with external iterators, which is preferable.
  21. To use an external iterator simply use the foreach language construct to
  22. iterate over the collection, which calls ``getIterator()`` internally, or
  23. explicitly retrieve an iterator though ``getIterator()`` which can then be
  24. used to iterate over the collection. You can not rely on the internal iterator
  25. of the collection being at a certain position unless you explicitly positioned it before.
  26. The methods available on the interface are:
  27. add
  28. ---
  29. Adds an element at the end of the collection.
  30. .. code-block:: php
  31. $collection->add('test');
  32. clear
  33. -----
  34. Clears the collection, removing all elements.
  35. .. code-block:: php
  36. $collection->clear();
  37. contains
  38. --------
  39. Checks whether an element is contained in the collection. This is an O(n) operation, where n is the size of the collection.
  40. .. code-block:: php
  41. $collection = new Collection(['test']);
  42. $contains = $collection->contains('test'); // true
  43. containsKey
  44. -----------
  45. Checks whether the collection contains an element with the specified key/index.
  46. .. code-block:: php
  47. $collection = new Collection(['test' => true]);
  48. $contains = $collection->containsKey('test'); // true
  49. current
  50. -------
  51. Gets the element of the collection at the current iterator position.
  52. .. code-block:: php
  53. $collection = new Collection(['first', 'second', 'third']);
  54. $current = $collection->current(); // first
  55. get
  56. ---
  57. Gets the element at the specified key/index.
  58. .. code-block:: php
  59. $collection = new Collection([
  60. 'key' => 'value',
  61. ]);
  62. $value = $collection->get('key'); // value
  63. getKeys
  64. -------
  65. Gets all keys/indices of the collection.
  66. .. code-block:: php
  67. $collection = new Collection(['a', 'b', 'c']);
  68. $keys = $collection->getKeys(); // [0, 1, 2]
  69. getValues
  70. ---------
  71. Gets all values of the collection.
  72. .. code-block:: php
  73. $collection = new Collection([
  74. 'key1' => 'value1',
  75. 'key2' => 'value2',
  76. 'key3' => 'value3',
  77. ]);
  78. $values = $collection->getValues(); // ['value1', 'value2', 'value3']
  79. isEmpty
  80. -------
  81. Checks whether the collection is empty (contains no elements).
  82. .. code-block:: php
  83. $collection = new Collection(['a', 'b', 'c']);
  84. $isEmpty = $collection->isEmpty(); // false
  85. first
  86. -----
  87. Sets the internal iterator to the first element in the collection and returns this element.
  88. .. code-block:: php
  89. $collection = new Collection(['first', 'second', 'third']);
  90. $first = $collection->first(); // first
  91. exists
  92. ------
  93. Tests for the existence of an element that satisfies the given predicate.
  94. .. code-block:: php
  95. $collection = new Collection(['first', 'second', 'third']);
  96. $exists = $collection->exists(function($key, $value) {
  97. return $value === 'first';
  98. }); // true
  99. filter
  100. ------
  101. Returns all the elements of this collection for which your callback function returns `true`.
  102. The order and keys of the elements are preserved.
  103. .. code-block:: php
  104. $collection = new ArrayCollection([1, 2, 3]);
  105. $filteredCollection = $collection->filter(function($element) {
  106. return $element > 1;
  107. }); // [2, 3]
  108. forAll
  109. ------
  110. Tests whether the given predicate holds for all elements of this collection.
  111. .. code-block:: php
  112. $collection = new ArrayCollection([1, 2, 3]);
  113. $forAll = $collection->forAll(function($key, $value) {
  114. return $value > 1;
  115. }); // false
  116. indexOf
  117. -------
  118. Gets the index/key of a given element. The comparison of two elements is strict, that means not only the value but also the type must match. For objects this means reference equality.
  119. .. code-block:: php
  120. $collection = new ArrayCollection([1, 2, 3]);
  121. $indexOf = $collection->indexOf(3); // 2
  122. key
  123. ---
  124. Gets the key/index of the element at the current iterator position.
  125. .. code-block:: php
  126. $collection = new ArrayCollection([1, 2, 3]);
  127. $collection->next();
  128. $key = $collection->key(); // 1
  129. last
  130. ----
  131. Sets the internal iterator to the last element in the collection and returns this element.
  132. .. code-block:: php
  133. $collection = new ArrayCollection([1, 2, 3]);
  134. $last = $collection->last(); // 3
  135. map
  136. ---
  137. Applies the given function to each element in the collection and returns a new collection with the elements returned by the function.
  138. .. code-block:: php
  139. $collection = new ArrayCollection([1, 2, 3]);
  140. $mappedCollection = $collection->map(function($value) {
  141. return $value + 1;
  142. }); // [2, 3, 4]
  143. next
  144. ----
  145. Moves the internal iterator position to the next element and returns this element.
  146. .. code-block:: php
  147. $collection = new ArrayCollection([1, 2, 3]);
  148. $next = $collection->next(); // 2
  149. partition
  150. ---------
  151. Partitions this collection in two collections according to a predicate. Keys are preserved in the resulting collections.
  152. .. code-block:: php
  153. $collection = new ArrayCollection([1, 2, 3]);
  154. $mappedCollection = $collection->partition(function($key, $value) {
  155. return $value > 1
  156. }); // [[2, 3], [1]]
  157. remove
  158. ------
  159. Removes the element at the specified index from the collection.
  160. .. code-block:: php
  161. $collection = new ArrayCollection([1, 2, 3]);
  162. $collection->remove(0); // [2, 3]
  163. removeElement
  164. -------------
  165. Removes the specified element from the collection, if it is found.
  166. .. code-block:: php
  167. $collection = new ArrayCollection([1, 2, 3]);
  168. $collection->removeElement(3); // [1, 2]
  169. set
  170. ---
  171. Sets an element in the collection at the specified key/index.
  172. .. code-block:: php
  173. $collection = new ArrayCollection();
  174. $collection->set('name', 'jwage');
  175. slice
  176. -----
  177. Extracts a slice of $length elements starting at position $offset from the Collection. If $length is null it returns all elements from $offset to the end of the Collection. Keys have to be preserved by this method. Calling this method will only return the selected slice and NOT change the elements contained in the collection slice is called on.
  178. .. code-block:: php
  179. $collection = new ArrayCollection([0, 1, 2, 3, 4, 5]);
  180. $slice = $collection->slice(1, 2); // [1, 2]
  181. toArray
  182. -------
  183. Gets a native PHP array representation of the collection.
  184. .. code-block:: php
  185. $collection = new ArrayCollection([0, 1, 2, 3, 4, 5]);
  186. $array = $collection->toArray(); // [0, 1, 2, 3, 4, 5]
  187. Selectable Methods
  188. ==================
  189. Some Doctrine Collections, like ``Doctrine\Common\Collections\ArrayCollection``,
  190. implement an interface named ``Doctrine\Common\Collections\Selectable``
  191. that offers the usage of a powerful expressions API, where conditions
  192. can be applied to a collection to get a result with matching elements
  193. only.
  194. matching
  195. --------
  196. Selects all elements from a selectable that match the expression and
  197. returns a new collection containing these elements.
  198. .. code-block:: php
  199. use Doctrine\Common\Collections\Criteria;
  200. use Doctrine\Common\Collections\Expr\Comparison;
  201. $collection = new ArrayCollection([
  202. [
  203. 'name' => 'jwage',
  204. ],
  205. [
  206. 'name' => 'romanb',
  207. ],
  208. ]);
  209. $expr = new Comparison('name', '=', 'jwage');
  210. $criteria = new Criteria();
  211. $criteria->where($expr);
  212. $matched = $collection->matching($criteria); // ['jwage']
  213. You can read more about expressions :ref:`here <expressions>`.