index.rst 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  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($count) {
  11. return $count > 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 that satisfy the predicate. The order of the elements is preserved.
  102. .. code-block:: php
  103. $collection = new ArrayCollection([1, 2, 3]);
  104. $filteredCollection = $collection->filter(function($count) {
  105. return $count > 1;
  106. }); // [2, 3]
  107. forAll
  108. ------
  109. Tests whether the given predicate holds for all elements of this collection.
  110. .. code-block:: php
  111. $collection = new ArrayCollection([1, 2, 3]);
  112. $forAll = $collection->forAll(function($key, $value) {
  113. return $value > 1;
  114. }); // false
  115. indexOf
  116. -------
  117. 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.
  118. .. code-block:: php
  119. $collection = new ArrayCollection([1, 2, 3]);
  120. $indexOf = $collection->indexOf(3); // 2
  121. key
  122. ---
  123. Gets the key/index of the element at the current iterator position.
  124. .. code-block:: php
  125. $collection = new ArrayCollection([1, 2, 3]);
  126. $collection->next();
  127. $key = $collection->key(); // 1
  128. last
  129. ----
  130. Sets the internal iterator to the last element in the collection and returns this element.
  131. .. code-block:: php
  132. $collection = new ArrayCollection([1, 2, 3]);
  133. $last = $collection->last(); // 3
  134. map
  135. ---
  136. Applies the given function to each element in the collection and returns a new collection with the elements returned by the function.
  137. .. code-block:: php
  138. $collection = new ArrayCollection([1, 2, 3]);
  139. $mappedCollection = $collection->map(function($value) {
  140. return $value + 1;
  141. }); // [2, 3, 4]
  142. next
  143. ----
  144. Moves the internal iterator position to the next element and returns this element.
  145. .. code-block:: php
  146. $collection = new ArrayCollection([1, 2, 3]);
  147. $next = $collection->next(); // 2
  148. partition
  149. ---------
  150. Partitions this collection in two collections according to a predicate. Keys are preserved in the resulting collections.
  151. .. code-block:: php
  152. $collection = new ArrayCollection([1, 2, 3]);
  153. $mappedCollection = $collection->partition(function($key, $value) {
  154. return $value > 1
  155. }); // [[2, 3], [1]]
  156. remove
  157. ------
  158. Removes the element at the specified index from the collection.
  159. .. code-block:: php
  160. $collection = new ArrayCollection([1, 2, 3]);
  161. $collection->remove(0); // [2, 3]
  162. removeElement
  163. -------------
  164. Removes the specified element from the collection, if it is found.
  165. .. code-block:: php
  166. $collection = new ArrayCollection([1, 2, 3]);
  167. $collection->removeElement(3); // [1, 2]
  168. set
  169. ---
  170. Sets an element in the collection at the specified key/index.
  171. .. code-block:: php
  172. $collection = new ArrayCollection();
  173. $collection->set('name', 'jwage');
  174. slice
  175. -----
  176. 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.
  177. .. code-block:: php
  178. $collection = new ArrayCollection([0, 1, 2, 3, 4, 5]);
  179. $slice = $collection->slice(1, 2); // [1, 2]
  180. toArray
  181. -------
  182. Gets a native PHP array representation of the collection.
  183. .. code-block:: php
  184. $collection = new ArrayCollection([0, 1, 2, 3, 4, 5]);
  185. $array = $collection->toArray(); // [0, 1, 2, 3, 4, 5]
  186. Selectable Methods
  187. ==================
  188. Some Doctrine Collections, like ``Doctrine\Common\Collections\ArrayCollection``,
  189. implement an interface named ``Doctrine\Common\Collections\Selectable``
  190. that offers the usage of a powerful expressions API, where conditions
  191. can be applied to a collection to get a result with matching elements
  192. only.
  193. matching
  194. --------
  195. Selects all elements from a selectable that match the expression and
  196. returns a new collection containing these elements.
  197. .. code-block:: php
  198. use Doctrine\Common\Collections\Criteria;
  199. use Doctrine\Common\Collections\Expr\Comparison;
  200. $collection = new ArrayCollection([
  201. [
  202. 'name' => 'jwage',
  203. ],
  204. [
  205. 'name' => 'romanb',
  206. ],
  207. ]);
  208. $expr = new Comparison('name', '=', 'jwage');
  209. $criteria = new Criteria();
  210. $criteria->where($expr);
  211. $matched = $collection->matching($criteria); // ['jwage']
  212. You can read more about expressions :ref:`here <expressions>`.