1 | <?php |
||
33 | class Finder implements FinderInterface |
||
34 | { |
||
35 | |||
36 | /** |
||
37 | * Model |
||
38 | * @var AnnotatedInterface |
||
39 | */ |
||
40 | public $model = null; |
||
41 | |||
42 | /** |
||
43 | * Mangan instance |
||
44 | * @var Mangan |
||
45 | */ |
||
46 | private $mn = null; |
||
47 | |||
48 | /** |
||
49 | * Entity manager instance |
||
50 | * @var EntityManagerInterface |
||
51 | */ |
||
52 | private $em = null; |
||
53 | |||
54 | /** |
||
55 | * Scope manager instance |
||
56 | * @var ScopeManager |
||
57 | */ |
||
58 | private $sm = null; |
||
59 | |||
60 | /** |
||
61 | * Finder criteria |
||
62 | * @var Criteria |
||
63 | */ |
||
64 | private $_criteria = null; |
||
65 | |||
66 | /** |
||
67 | * Whenever to use corsors |
||
68 | * @var bool |
||
69 | */ |
||
70 | private $_useCursor = false; |
||
71 | |||
72 | /** |
||
73 | * Constructor |
||
74 | * |
||
75 | * @param object $model Model instance |
||
76 | * @param EntityManagerInterface $em |
||
77 | * @param Mangan $mangan |
||
78 | */ |
||
79 | 69 | public function __construct($model, $em = null, $mangan = null) |
|
91 | |||
92 | /** |
||
93 | * Create model related finder. |
||
94 | * This will create customized finder if defined in model with Finder annotation. |
||
95 | * If no custom finder is defined this will return default Finder. |
||
96 | * |
||
97 | * @param AnnotatedInterface $model |
||
98 | * @param EntityManagerInterface $em |
||
99 | * @param Mangan $mangan |
||
100 | * @return FinderInterface |
||
101 | */ |
||
102 | 9 | public static function create(AnnotatedInterface $model, $em = null, Mangan $mangan = null) |
|
107 | |||
108 | /** |
||
109 | * Finds a single Document with the specified condition. |
||
110 | * |
||
111 | * @param array|CriteriaInterface $criteria query criteria. |
||
112 | * @return AnnotatedInterface |
||
113 | * @Ignored |
||
114 | */ |
||
115 | 52 | public function find($criteria = null) |
|
126 | |||
127 | /** |
||
128 | * Finds document with the specified primary key. Primary key by default |
||
129 | * is defined by `_id` field. But could be any other. For simple (one column) |
||
130 | * keys use it's value. |
||
131 | * |
||
132 | * For composite use key-value with column names as keys |
||
133 | * and values for values. |
||
134 | * |
||
135 | * Example for simple pk: |
||
136 | * ```php |
||
137 | * $pk = '51b616fcc0986e30026d0748' |
||
138 | * ``` |
||
139 | * |
||
140 | * Composite pk: |
||
141 | * ```php |
||
142 | * $pk = [ |
||
143 | * 'mainPk' => 1, |
||
144 | * 'secondaryPk' => 2 |
||
145 | * ]; |
||
146 | * ``` |
||
147 | * |
||
148 | * @param mixed $pkValue primary key value. Use array for composite key. |
||
149 | * @param array|CriteriaInterface $criteria |
||
150 | * @return AnnotatedInterface|null |
||
151 | * @Ignored |
||
152 | */ |
||
153 | 40 | public function findByPk($pkValue, $criteria = null) |
|
161 | |||
162 | /** |
||
163 | * Finds document with the specified attributes. |
||
164 | * Attributes should be specified as key-value pairs. |
||
165 | * This allows easier syntax for simple queries. |
||
166 | * |
||
167 | * Example: |
||
168 | * ```php |
||
169 | * $attributes = [ |
||
170 | * 'name' => 'John', |
||
171 | * 'title' => 'dr' |
||
172 | * ]; |
||
173 | * ``` |
||
174 | * |
||
175 | * @param mixed[] Array of stributes and values in form of ['attributeName' => 'value'] |
||
176 | * @return AnnotatedInterface|null |
||
177 | */ |
||
178 | 2 | public function findByAttributes(array $attributes) |
|
188 | |||
189 | /** |
||
190 | * Finds all documents satisfying the specified condition. |
||
191 | * See {@link find()} for detailed explanation about $condition and $params. |
||
192 | * |
||
193 | * @param array|CriteriaInterface $criteria query criteria. |
||
194 | * @return AnnotatedInterface[]|Cursor |
||
195 | */ |
||
196 | 18 | public function findAll($criteria = null) |
|
232 | |||
233 | /** |
||
234 | * Finds all documents with the specified attributes. |
||
235 | * |
||
236 | * @param mixed[] Array of stributes and values in form of ['attributeName' => 'value'] |
||
237 | * @return AnnotatedInterface[]|Cursor - Array or cursor of Documents |
||
238 | * @since v1.0 |
||
239 | */ |
||
240 | 1 | public function findAllByAttributes(array $attributes) |
|
251 | |||
252 | /** |
||
253 | * Finds all documents with the specified primary keys. |
||
254 | * In MongoDB world every document has '_id' unique field, so with this method that |
||
255 | * field is in use as PK by default. |
||
256 | * See {@link find()} for detailed explanation about $condition. |
||
257 | * |
||
258 | * @param mixed $pkValues primary key value(s). Use array for multiple primary keys. For composite key, each key value must be an array (column name=>column value). |
||
259 | * @param array|CriteriaInterface $criteria query criteria. |
||
260 | * @return AnnotatedInterface[]|Cursor - Array or cursor of Documents |
||
261 | * @since v1.0 |
||
262 | */ |
||
263 | 7 | public function findAllByPk($pkValues, $criteria = null) |
|
271 | |||
272 | /** |
||
273 | * Counts all documents satisfying the specified condition. |
||
274 | * See {@link find()} for detailed explanation about $condition and $params. |
||
275 | * @param array|CriteriaInterface $criteria query criteria. |
||
276 | * @return integer Count of all documents satisfying the specified condition. |
||
277 | * @since v1.0 |
||
278 | */ |
||
279 | 19 | public function count($criteria = null) |
|
291 | |||
292 | /** |
||
293 | * Counts all documents found by attribute values. |
||
294 | * |
||
295 | * Example: |
||
296 | * ```php |
||
297 | * $attributes = [ |
||
298 | * 'name' => 'John', |
||
299 | * 'title' => 'dr' |
||
300 | * ]; |
||
301 | * ``` |
||
302 | * |
||
303 | * @param mixed[] Array of attributes and values in form of ['attributeName' => 'value'] |
||
304 | * @return int |
||
305 | * @since v1.2.2 |
||
306 | * @Ignored |
||
307 | */ |
||
308 | 1 | public function countByAttributes(array $attributes) |
|
327 | |||
328 | /** |
||
329 | * Checks whether there is document satisfying the specified condition. |
||
330 | * |
||
331 | * @param CriteriaInterface $criteria |
||
332 | * @return bool |
||
333 | */ |
||
334 | 9 | public function exists(CriteriaInterface $criteria = null) |
|
358 | |||
359 | /** |
||
360 | * Whenever to use cursor |
||
361 | * @param bool $useCursor |
||
362 | * @return FinderInterface |
||
363 | */ |
||
364 | 69 | public function withCursor($useCursor = true) |
|
369 | |||
370 | /** |
||
371 | * Resets all scopes and criteria applied including default scope. |
||
372 | * |
||
373 | * @return Finder |
||
374 | * @since v1.0 |
||
375 | */ |
||
376 | public function resetScope() |
||
382 | |||
383 | /** |
||
384 | * Creates an model with the given attributes. |
||
385 | * This method is internally used by the find methods. |
||
386 | * @param mixed[] $data attribute values (column name=>column value) |
||
387 | * @return AnnotatedInterface|null the newly created document. The class of the object is the same as the model class. |
||
388 | * Null is returned if the input data is false. |
||
389 | * @since v1.0 |
||
390 | */ |
||
391 | 59 | protected function populateRecord($data) |
|
409 | |||
410 | /** |
||
411 | * Creates a list of documents based on the input data. |
||
412 | * This method is internally used by the find methods. |
||
413 | * @param MongoCursor $cursor Results found to populate active records. |
||
414 | * @return AnnotatedInterface[] array list of active records. |
||
415 | * @since v1.0 |
||
416 | */ |
||
417 | 17 | protected function populateRecords(MongoCursor $cursor) |
|
426 | |||
427 | } |
||
428 |