Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like DocumentPersister often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use DocumentPersister, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 44 | class DocumentPersister |
||
| 45 | { |
||
| 46 | /** |
||
| 47 | * The PersistenceBuilder instance. |
||
| 48 | * |
||
| 49 | * @var PersistenceBuilder |
||
| 50 | */ |
||
| 51 | private $pb; |
||
| 52 | |||
| 53 | /** |
||
| 54 | * The DocumentManager instance. |
||
| 55 | * |
||
| 56 | * @var DocumentManager |
||
| 57 | */ |
||
| 58 | private $dm; |
||
| 59 | |||
| 60 | /** |
||
| 61 | * The EventManager instance |
||
| 62 | * |
||
| 63 | * @var EventManager |
||
| 64 | */ |
||
| 65 | private $evm; |
||
| 66 | |||
| 67 | /** |
||
| 68 | * The UnitOfWork instance. |
||
| 69 | * |
||
| 70 | * @var UnitOfWork |
||
| 71 | */ |
||
| 72 | private $uow; |
||
| 73 | |||
| 74 | /** |
||
| 75 | * The ClassMetadata instance for the document type being persisted. |
||
| 76 | * |
||
| 77 | * @var ClassMetadata |
||
| 78 | */ |
||
| 79 | private $class; |
||
| 80 | |||
| 81 | /** |
||
| 82 | * The MongoCollection instance for this document. |
||
| 83 | * |
||
| 84 | * @var \MongoCollection |
||
| 85 | */ |
||
| 86 | private $collection; |
||
| 87 | |||
| 88 | /** |
||
| 89 | * Array of queued inserts for the persister to insert. |
||
| 90 | * |
||
| 91 | * @var array |
||
| 92 | */ |
||
| 93 | private $queuedInserts = array(); |
||
| 94 | |||
| 95 | /** |
||
| 96 | * Array of queued inserts for the persister to insert. |
||
| 97 | * |
||
| 98 | * @var array |
||
| 99 | */ |
||
| 100 | private $queuedUpserts = array(); |
||
| 101 | |||
| 102 | /** |
||
| 103 | * The CriteriaMerger instance. |
||
| 104 | * |
||
| 105 | * @var CriteriaMerger |
||
| 106 | */ |
||
| 107 | private $cm; |
||
| 108 | |||
| 109 | /** |
||
| 110 | * The CollectionPersister instance. |
||
| 111 | * |
||
| 112 | * @var CollectionPersister |
||
| 113 | */ |
||
| 114 | private $cp; |
||
| 115 | |||
| 116 | /** |
||
| 117 | * Initializes this instance. |
||
| 118 | * |
||
| 119 | * @param PersistenceBuilder $pb |
||
| 120 | * @param DocumentManager $dm |
||
| 121 | * @param EventManager $evm |
||
| 122 | * @param UnitOfWork $uow |
||
| 123 | * @param HydratorFactory $hydratorFactory |
||
| 124 | * @param ClassMetadata $class |
||
| 125 | * @param CriteriaMerger $cm |
||
| 126 | */ |
||
| 127 | 693 | public function __construct( |
|
| 146 | |||
| 147 | /** |
||
| 148 | * @return array |
||
| 149 | */ |
||
| 150 | public function getInserts() |
||
| 154 | |||
| 155 | /** |
||
| 156 | * @param object $document |
||
| 157 | * @return bool |
||
| 158 | */ |
||
| 159 | public function isQueuedForInsert($document) |
||
| 163 | |||
| 164 | /** |
||
| 165 | * Adds a document to the queued insertions. |
||
| 166 | * The document remains queued until {@link executeInserts} is invoked. |
||
| 167 | * |
||
| 168 | * @param object $document The document to queue for insertion. |
||
| 169 | */ |
||
| 170 | 493 | public function addInsert($document) |
|
| 174 | |||
| 175 | /** |
||
| 176 | * @return array |
||
| 177 | */ |
||
| 178 | public function getUpserts() |
||
| 182 | |||
| 183 | /** |
||
| 184 | * @param object $document |
||
| 185 | * @return boolean |
||
| 186 | */ |
||
| 187 | public function isQueuedForUpsert($document) |
||
| 191 | |||
| 192 | /** |
||
| 193 | * Adds a document to the queued upserts. |
||
| 194 | * The document remains queued until {@link executeUpserts} is invoked. |
||
| 195 | * |
||
| 196 | * @param object $document The document to queue for insertion. |
||
| 197 | */ |
||
| 198 | 76 | public function addUpsert($document) |
|
| 202 | |||
| 203 | /** |
||
| 204 | * Gets the ClassMetadata instance of the document class this persister is used for. |
||
| 205 | * |
||
| 206 | * @return ClassMetadata |
||
| 207 | */ |
||
| 208 | public function getClassMetadata() |
||
| 212 | |||
| 213 | /** |
||
| 214 | * Executes all queued document insertions. |
||
| 215 | * |
||
| 216 | * Queued documents without an ID will inserted in a batch and queued |
||
| 217 | * documents with an ID will be upserted individually. |
||
| 218 | * |
||
| 219 | * If no inserts are queued, invoking this method is a NOOP. |
||
| 220 | * |
||
| 221 | * @param array $options Options for batchInsert() and update() driver methods |
||
| 222 | */ |
||
| 223 | 493 | public function executeInserts(array $options = array()) |
|
| 224 | { |
||
| 225 | 493 | if ( ! $this->queuedInserts) { |
|
| 226 | return; |
||
| 227 | } |
||
| 228 | |||
| 229 | 493 | $inserts = array(); |
|
| 230 | 493 | foreach ($this->queuedInserts as $oid => $document) { |
|
| 231 | 493 | $data = $this->pb->prepareInsertData($document); |
|
| 232 | |||
| 233 | // Set the initial version for each insert |
||
| 234 | 492 | View Code Duplication | if ($this->class->isVersioned) { |
| 235 | 39 | $versionMapping = $this->class->fieldMappings[$this->class->versionField]; |
|
| 236 | 39 | if ($versionMapping['type'] === 'int') { |
|
| 237 | 37 | $nextVersion = max(1, (int) $this->class->reflFields[$this->class->versionField]->getValue($document)); |
|
| 238 | 37 | $this->class->reflFields[$this->class->versionField]->setValue($document, $nextVersion); |
|
| 239 | 2 | } elseif ($versionMapping['type'] === 'date') { |
|
| 240 | 2 | $nextVersionDateTime = new \DateTime(); |
|
| 241 | 2 | $nextVersion = new \MongoDate($nextVersionDateTime->getTimestamp()); |
|
| 242 | 2 | $this->class->reflFields[$this->class->versionField]->setValue($document, $nextVersionDateTime); |
|
| 243 | } |
||
| 244 | 39 | $data[$versionMapping['name']] = $nextVersion; |
|
| 245 | } |
||
| 246 | |||
| 247 | 492 | $inserts[$oid] = $data; |
|
| 248 | } |
||
| 249 | |||
| 250 | 492 | if ($inserts) { |
|
| 251 | try { |
||
| 252 | 492 | $this->collection->batchInsert($inserts, $options); |
|
| 253 | 7 | } catch (\MongoException $e) { |
|
| 254 | 7 | $this->queuedInserts = array(); |
|
| 255 | 7 | throw $e; |
|
| 256 | } |
||
| 257 | } |
||
| 258 | |||
| 259 | /* All collections except for ones using addToSet have already been |
||
| 260 | * saved. We have left these to be handled separately to avoid checking |
||
| 261 | * collection for uniqueness on PHP side. |
||
| 262 | */ |
||
| 263 | 492 | foreach ($this->queuedInserts as $document) { |
|
| 264 | 492 | $this->handleCollections($document, $options); |
|
| 265 | } |
||
| 266 | |||
| 267 | 492 | $this->queuedInserts = array(); |
|
| 268 | 492 | } |
|
| 269 | |||
| 270 | /** |
||
| 271 | * Executes all queued document upserts. |
||
| 272 | * |
||
| 273 | * Queued documents with an ID are upserted individually. |
||
| 274 | * |
||
| 275 | * If no upserts are queued, invoking this method is a NOOP. |
||
| 276 | * |
||
| 277 | * @param array $options Options for batchInsert() and update() driver methods |
||
| 278 | */ |
||
| 279 | 76 | public function executeUpserts(array $options = array()) |
|
| 280 | { |
||
| 281 | 76 | if ( ! $this->queuedUpserts) { |
|
| 282 | return; |
||
| 283 | } |
||
| 284 | |||
| 285 | 76 | foreach ($this->queuedUpserts as $oid => $document) { |
|
| 286 | 76 | $data = $this->pb->prepareUpsertData($document); |
|
| 287 | |||
| 288 | // Set the initial version for each upsert |
||
| 289 | 76 | View Code Duplication | if ($this->class->isVersioned) { |
| 290 | 3 | $versionMapping = $this->class->fieldMappings[$this->class->versionField]; |
|
| 291 | 3 | if ($versionMapping['type'] === 'int') { |
|
| 292 | 2 | $nextVersion = max(1, (int) $this->class->reflFields[$this->class->versionField]->getValue($document)); |
|
| 293 | 2 | $this->class->reflFields[$this->class->versionField]->setValue($document, $nextVersion); |
|
| 294 | 1 | } elseif ($versionMapping['type'] === 'date') { |
|
| 295 | 1 | $nextVersionDateTime = new \DateTime(); |
|
| 296 | 1 | $nextVersion = new \MongoDate($nextVersionDateTime->getTimestamp()); |
|
| 297 | 1 | $this->class->reflFields[$this->class->versionField]->setValue($document, $nextVersionDateTime); |
|
| 298 | } |
||
| 299 | 3 | $data['$set'][$versionMapping['name']] = $nextVersion; |
|
| 300 | } |
||
| 301 | |||
| 302 | try { |
||
| 303 | 76 | $this->executeUpsert($data, $options); |
|
| 304 | 76 | $this->handleCollections($document, $options); |
|
| 305 | 76 | unset($this->queuedUpserts[$oid]); |
|
| 306 | } catch (\MongoException $e) { |
||
| 307 | unset($this->queuedUpserts[$oid]); |
||
| 308 | 76 | throw $e; |
|
| 309 | } |
||
| 310 | } |
||
| 311 | 76 | } |
|
| 312 | |||
| 313 | /** |
||
| 314 | * Executes a single upsert in {@link executeInserts} |
||
| 315 | * |
||
| 316 | * @param array $data |
||
| 317 | * @param array $options |
||
| 318 | */ |
||
| 319 | 76 | private function executeUpsert(array $data, array $options) |
|
| 320 | { |
||
| 321 | 76 | $options['upsert'] = true; |
|
| 322 | 76 | $criteria = array('_id' => $data['$set']['_id']); |
|
| 323 | 76 | unset($data['$set']['_id']); |
|
| 324 | |||
| 325 | // Do not send an empty $set modifier |
||
| 326 | 76 | if (empty($data['$set'])) { |
|
| 327 | 13 | unset($data['$set']); |
|
| 328 | } |
||
| 329 | |||
| 330 | /* If there are no modifiers remaining, we're upserting a document with |
||
| 331 | * an identifier as its only field. Since a document with the identifier |
||
| 332 | * may already exist, the desired behavior is "insert if not exists" and |
||
| 333 | * NOOP otherwise. MongoDB 2.6+ does not allow empty modifiers, so $set |
||
| 334 | * the identifier to the same value in our criteria. |
||
| 335 | * |
||
| 336 | * This will fail for versions before MongoDB 2.6, which require an |
||
| 337 | * empty $set modifier. The best we can do (without attempting to check |
||
| 338 | * server versions in advance) is attempt the 2.6+ behavior and retry |
||
| 339 | * after the relevant exception. |
||
| 340 | * |
||
| 341 | * See: https://jira.mongodb.org/browse/SERVER-12266 |
||
| 342 | */ |
||
| 343 | 76 | if (empty($data)) { |
|
| 344 | 13 | $retry = true; |
|
| 345 | 13 | $data = array('$set' => array('_id' => $criteria['_id'])); |
|
| 346 | } |
||
| 347 | |||
| 348 | try { |
||
| 349 | 76 | $this->collection->update($criteria, $data, $options); |
|
| 350 | 64 | return; |
|
| 351 | 13 | } catch (\MongoCursorException $e) { |
|
| 352 | 13 | if (empty($retry) || strpos($e->getMessage(), 'Mod on _id not allowed') === false) { |
|
| 353 | throw $e; |
||
| 354 | } |
||
| 355 | } |
||
| 356 | |||
| 357 | 13 | $this->collection->update($criteria, array('$set' => new \stdClass), $options); |
|
| 358 | 13 | } |
|
| 359 | |||
| 360 | /** |
||
| 361 | * Updates the already persisted document if it has any new changesets. |
||
| 362 | * |
||
| 363 | * @param object $document |
||
| 364 | * @param array $options Array of options to be used with update() |
||
| 365 | * @throws \Doctrine\ODM\MongoDB\LockException |
||
| 366 | */ |
||
| 367 | 214 | public function update($document, array $options = array()) |
|
| 368 | { |
||
| 369 | 214 | $id = $this->uow->getDocumentIdentifier($document); |
|
| 370 | 214 | $update = $this->pb->prepareUpdateData($document); |
|
| 371 | |||
| 372 | 214 | $id = $this->class->getDatabaseIdentifierValue($id); |
|
| 373 | 214 | $query = array('_id' => $id); |
|
| 374 | |||
| 375 | // Include versioning logic to set the new version value in the database |
||
| 376 | // and to ensure the version has not changed since this document object instance |
||
| 377 | // was fetched from the database |
||
| 378 | 214 | if ($this->class->isVersioned) { |
|
| 379 | 31 | $versionMapping = $this->class->fieldMappings[$this->class->versionField]; |
|
| 380 | 31 | $currentVersion = $this->class->reflFields[$this->class->versionField]->getValue($document); |
|
| 381 | 31 | if ($versionMapping['type'] === 'int') { |
|
| 382 | 28 | $nextVersion = $currentVersion + 1; |
|
| 383 | 28 | $update['$inc'][$versionMapping['name']] = 1; |
|
| 384 | 28 | $query[$versionMapping['name']] = $currentVersion; |
|
| 385 | 28 | $this->class->reflFields[$this->class->versionField]->setValue($document, $nextVersion); |
|
| 386 | 3 | } elseif ($versionMapping['type'] === 'date') { |
|
| 387 | 3 | $nextVersion = new \DateTime(); |
|
| 388 | 3 | $update['$set'][$versionMapping['name']] = new \MongoDate($nextVersion->getTimestamp()); |
|
| 389 | 3 | $query[$versionMapping['name']] = new \MongoDate($currentVersion->getTimestamp()); |
|
| 390 | 3 | $this->class->reflFields[$this->class->versionField]->setValue($document, $nextVersion); |
|
| 391 | } |
||
| 392 | } |
||
| 393 | |||
| 394 | 214 | if ( ! empty($update)) { |
|
| 395 | // Include locking logic so that if the document object in memory is currently |
||
| 396 | // locked then it will remove it, otherwise it ensures the document is not locked. |
||
| 397 | 150 | if ($this->class->isLockable) { |
|
| 398 | 11 | $isLocked = $this->class->reflFields[$this->class->lockField]->getValue($document); |
|
| 399 | 11 | $lockMapping = $this->class->fieldMappings[$this->class->lockField]; |
|
| 400 | 11 | if ($isLocked) { |
|
| 401 | 2 | $update['$unset'] = array($lockMapping['name'] => true); |
|
| 402 | } else { |
||
| 403 | 9 | $query[$lockMapping['name']] = array('$exists' => false); |
|
| 404 | } |
||
| 405 | } |
||
| 406 | |||
| 407 | 150 | $result = $this->collection->update($query, $update, $options); |
|
| 408 | |||
| 409 | 150 | View Code Duplication | if (($this->class->isVersioned || $this->class->isLockable) && ! $result['n']) { |
| 410 | 5 | throw LockException::lockFailed($document); |
|
| 411 | } |
||
| 412 | } |
||
| 413 | |||
| 414 | 210 | $this->handleCollections($document, $options); |
|
| 415 | 210 | } |
|
| 416 | |||
| 417 | /** |
||
| 418 | * Removes document from mongo |
||
| 419 | * |
||
| 420 | * @param mixed $document |
||
| 421 | * @param array $options Array of options to be used with remove() |
||
| 422 | * @throws \Doctrine\ODM\MongoDB\LockException |
||
| 423 | */ |
||
| 424 | 28 | public function delete($document, array $options = array()) |
|
| 425 | { |
||
| 426 | 28 | $id = $this->uow->getDocumentIdentifier($document); |
|
| 427 | 28 | $query = array('_id' => $this->class->getDatabaseIdentifierValue($id)); |
|
| 428 | |||
| 429 | 28 | if ($this->class->isLockable) { |
|
| 430 | 2 | $query[$this->class->lockField] = array('$exists' => false); |
|
| 431 | } |
||
| 432 | |||
| 433 | 28 | $result = $this->collection->remove($query, $options); |
|
| 434 | |||
| 435 | 28 | View Code Duplication | if (($this->class->isVersioned || $this->class->isLockable) && ! $result['n']) { |
| 436 | 2 | throw LockException::lockFailed($document); |
|
| 437 | } |
||
| 438 | 26 | } |
|
| 439 | |||
| 440 | /** |
||
| 441 | * Refreshes a managed document. |
||
| 442 | * |
||
| 443 | * @param array $id The identifier of the document. |
||
| 444 | * @param object $document The document to refresh. |
||
| 445 | */ |
||
| 446 | 20 | public function refresh($id, $document) |
|
| 452 | |||
| 453 | /** |
||
| 454 | * Finds a document by a set of criteria. |
||
| 455 | * |
||
| 456 | * If a scalar or MongoId is provided for $criteria, it will be used to |
||
| 457 | * match an _id value. |
||
| 458 | * |
||
| 459 | * @param mixed $criteria Query criteria |
||
| 460 | * @param object $document Document to load the data into. If not specified, a new document is created. |
||
| 461 | * @param array $hints Hints for document creation |
||
| 462 | * @param integer $lockMode |
||
| 463 | * @param array $sort Sort array for Cursor::sort() |
||
| 464 | * @throws \Doctrine\ODM\MongoDB\LockException |
||
| 465 | * @return object|null The loaded and managed document instance or null if no document was found |
||
| 466 | * @todo Check identity map? loadById method? Try to guess whether $criteria is the id? |
||
| 467 | */ |
||
| 468 | 363 | public function load($criteria, $document = null, array $hints = array(), $lockMode = 0, array $sort = null) |
|
| 469 | { |
||
| 470 | // TODO: remove this |
||
| 471 | 363 | if ($criteria === null || is_scalar($criteria) || $criteria instanceof \MongoId) { |
|
| 472 | $criteria = array('_id' => $criteria); |
||
| 473 | } |
||
| 474 | |||
| 475 | 363 | $criteria = $this->prepareQueryOrNewObj($criteria); |
|
| 476 | 363 | $criteria = $this->addDiscriminatorToPreparedQuery($criteria); |
|
| 477 | 363 | $criteria = $this->addFilterToPreparedQuery($criteria); |
|
| 478 | |||
| 479 | 363 | $cursor = $this->collection->find($criteria); |
|
| 480 | |||
| 481 | 363 | if (null !== $sort) { |
|
| 482 | 101 | $cursor->sort($this->prepareSortOrProjection($sort)); |
|
| 483 | } |
||
| 484 | |||
| 485 | 363 | $result = $cursor->getSingleResult(); |
|
| 486 | |||
| 487 | 363 | if ($this->class->isLockable) { |
|
| 488 | 1 | $lockMapping = $this->class->fieldMappings[$this->class->lockField]; |
|
| 489 | 1 | if (isset($result[$lockMapping['name']]) && $result[$lockMapping['name']] === LockMode::PESSIMISTIC_WRITE) { |
|
| 490 | 1 | throw LockException::lockFailed($result); |
|
| 491 | } |
||
| 492 | } |
||
| 493 | |||
| 494 | 362 | return $this->createDocument($result, $document, $hints); |
|
| 495 | } |
||
| 496 | |||
| 497 | /** |
||
| 498 | * Finds documents by a set of criteria. |
||
| 499 | * |
||
| 500 | * @param array $criteria Query criteria |
||
| 501 | * @param array $sort Sort array for Cursor::sort() |
||
| 502 | * @param integer|null $limit Limit for Cursor::limit() |
||
| 503 | * @param integer|null $skip Skip for Cursor::skip() |
||
| 504 | * @return Cursor |
||
| 505 | */ |
||
| 506 | 22 | public function loadAll(array $criteria = array(), array $sort = null, $limit = null, $skip = null) |
|
| 507 | { |
||
| 508 | 22 | $criteria = $this->prepareQueryOrNewObj($criteria); |
|
| 509 | 22 | $criteria = $this->addDiscriminatorToPreparedQuery($criteria); |
|
| 510 | 22 | $criteria = $this->addFilterToPreparedQuery($criteria); |
|
| 511 | |||
| 512 | 22 | $baseCursor = $this->collection->find($criteria); |
|
| 513 | 22 | $cursor = $this->wrapCursor($baseCursor); |
|
| 514 | |||
| 515 | 22 | if (null !== $sort) { |
|
| 516 | 3 | $cursor->sort($sort); |
|
| 517 | } |
||
| 518 | |||
| 519 | 22 | if (null !== $limit) { |
|
| 520 | 2 | $cursor->limit($limit); |
|
| 521 | } |
||
| 522 | |||
| 523 | 22 | if (null !== $skip) { |
|
| 524 | 2 | $cursor->skip($skip); |
|
| 525 | } |
||
| 526 | |||
| 527 | 22 | return $cursor; |
|
| 528 | } |
||
| 529 | |||
| 530 | /** |
||
| 531 | * Wraps the supplied base cursor in the corresponding ODM class. |
||
| 532 | * |
||
| 533 | * @param CursorInterface $baseCursor |
||
| 534 | * @return Cursor |
||
| 535 | */ |
||
| 536 | 22 | private function wrapCursor(CursorInterface $baseCursor) |
|
| 540 | |||
| 541 | /** |
||
| 542 | * Checks whether the given managed document exists in the database. |
||
| 543 | * |
||
| 544 | * @param object $document |
||
| 545 | * @return boolean TRUE if the document exists in the database, FALSE otherwise. |
||
| 546 | */ |
||
| 547 | 3 | public function exists($document) |
|
| 552 | |||
| 553 | /** |
||
| 554 | * Locks document by storing the lock mode on the mapped lock field. |
||
| 555 | * |
||
| 556 | * @param object $document |
||
| 557 | * @param int $lockMode |
||
| 558 | */ |
||
| 559 | 5 | public function lock($document, $lockMode) |
|
| 567 | |||
| 568 | /** |
||
| 569 | * Releases any lock that exists on this document. |
||
| 570 | * |
||
| 571 | * @param object $document |
||
| 572 | */ |
||
| 573 | 1 | public function unlock($document) |
|
| 581 | |||
| 582 | /** |
||
| 583 | * Creates or fills a single document object from an query result. |
||
| 584 | * |
||
| 585 | * @param object $result The query result. |
||
| 586 | * @param object $document The document object to fill, if any. |
||
| 587 | * @param array $hints Hints for document creation. |
||
| 588 | * @return object The filled and managed document object or NULL, if the query result is empty. |
||
| 589 | */ |
||
| 590 | 362 | private function createDocument($result, $document = null, array $hints = array()) |
|
| 604 | |||
| 605 | /** |
||
| 606 | * Loads a PersistentCollection data. Used in the initialize() method. |
||
| 607 | * |
||
| 608 | * @param PersistentCollectionInterface $collection |
||
| 609 | */ |
||
| 610 | 164 | public function loadCollection(PersistentCollectionInterface $collection) |
|
| 631 | |||
| 632 | 115 | private function loadEmbedManyCollection(PersistentCollectionInterface $collection) |
|
| 661 | |||
| 662 | 52 | private function loadReferenceManyCollectionOwningSide(PersistentCollectionInterface $collection) |
|
| 734 | |||
| 735 | 14 | private function loadReferenceManyCollectionInverseSide(PersistentCollectionInterface $collection) |
|
| 743 | |||
| 744 | /** |
||
| 745 | * @param PersistentCollectionInterface $collection |
||
| 746 | * |
||
| 747 | * @return Query |
||
| 748 | */ |
||
| 749 | 16 | public function createReferenceManyInverseSideQuery(PersistentCollectionInterface $collection) |
|
| 785 | |||
| 786 | 3 | private function loadReferenceManyWithRepositoryMethod(PersistentCollectionInterface $collection) |
|
| 799 | |||
| 800 | /** |
||
| 801 | * @param PersistentCollectionInterface $collection |
||
| 802 | * |
||
| 803 | * @return CursorInterface |
||
| 804 | */ |
||
| 805 | 3 | public function createReferenceManyWithRepositoryMethodCursor(PersistentCollectionInterface $collection) |
|
| 835 | |||
| 836 | /** |
||
| 837 | * Prepare a sort or projection array by converting keys, which are PHP |
||
| 838 | * property names, to MongoDB field names. |
||
| 839 | * |
||
| 840 | * @param array $fields |
||
| 841 | * @return array |
||
| 842 | */ |
||
| 843 | 138 | public function prepareSortOrProjection(array $fields) |
|
| 853 | |||
| 854 | /** |
||
| 855 | * Prepare a mongodb field name and convert the PHP property names to MongoDB field names. |
||
| 856 | * |
||
| 857 | * @param string $fieldName |
||
| 858 | * @return string |
||
| 859 | */ |
||
| 860 | 85 | public function prepareFieldName($fieldName) |
|
| 866 | |||
| 867 | /** |
||
| 868 | * Adds discriminator criteria to an already-prepared query. |
||
| 869 | * |
||
| 870 | * This method should be used once for query criteria and not be used for |
||
| 871 | * nested expressions. It should be called before |
||
| 872 | * {@link DocumentPerister::addFilterToPreparedQuery()}. |
||
| 873 | * |
||
| 874 | * @param array $preparedQuery |
||
| 875 | * @return array |
||
| 876 | */ |
||
| 877 | 488 | public function addDiscriminatorToPreparedQuery(array $preparedQuery) |
|
| 893 | |||
| 894 | /** |
||
| 895 | * Adds filter criteria to an already-prepared query. |
||
| 896 | * |
||
| 897 | * This method should be used once for query criteria and not be used for |
||
| 898 | * nested expressions. It should be called after |
||
| 899 | * {@link DocumentPerister::addDiscriminatorToPreparedQuery()}. |
||
| 900 | * |
||
| 901 | * @param array $preparedQuery |
||
| 902 | * @return array |
||
| 903 | */ |
||
| 904 | 489 | public function addFilterToPreparedQuery(array $preparedQuery) |
|
| 918 | |||
| 919 | /** |
||
| 920 | * Prepares the query criteria or new document object. |
||
| 921 | * |
||
| 922 | * PHP field names and types will be converted to those used by MongoDB. |
||
| 923 | * |
||
| 924 | * @param array $query |
||
| 925 | * @return array |
||
| 926 | */ |
||
| 927 | 522 | public function prepareQueryOrNewObj(array $query) |
|
| 954 | |||
| 955 | /** |
||
| 956 | * Prepares a query value and converts the PHP value to the database value |
||
| 957 | * if it is an identifier. |
||
| 958 | * |
||
| 959 | * It also handles converting $fieldName to the database name if they are different. |
||
| 960 | * |
||
| 961 | * @param string $fieldName |
||
| 962 | * @param mixed $value |
||
| 963 | * @param ClassMetadata $class Defaults to $this->class |
||
| 964 | * @param boolean $prepareValue Whether or not to prepare the value |
||
| 965 | * @return array Prepared field name and value |
||
| 966 | */ |
||
| 967 | 515 | private function prepareQueryElement($fieldName, $value = null, $class = null, $prepareValue = true) |
|
| 1150 | |||
| 1151 | /** |
||
| 1152 | * Prepares a query expression. |
||
| 1153 | * |
||
| 1154 | * @param array|object $expression |
||
| 1155 | * @param ClassMetadata $class |
||
| 1156 | * @return array |
||
| 1157 | */ |
||
| 1158 | 68 | private function prepareQueryExpression($expression, $class) |
|
| 1185 | |||
| 1186 | /** |
||
| 1187 | * Checks whether the value has DBRef fields. |
||
| 1188 | * |
||
| 1189 | * This method doesn't check if the the value is a complete DBRef object, |
||
| 1190 | * although it should return true for a DBRef. Rather, we're checking that |
||
| 1191 | * the value has one or more fields for a DBref. In practice, this could be |
||
| 1192 | * $elemMatch criteria for matching a DBRef. |
||
| 1193 | * |
||
| 1194 | * @param mixed $value |
||
| 1195 | * @return boolean |
||
| 1196 | */ |
||
| 1197 | 69 | private function hasDBRefFields($value) |
|
| 1215 | |||
| 1216 | /** |
||
| 1217 | * Checks whether the value has query operators. |
||
| 1218 | * |
||
| 1219 | * @param mixed $value |
||
| 1220 | * @return boolean |
||
| 1221 | */ |
||
| 1222 | 73 | private function hasQueryOperators($value) |
|
| 1240 | |||
| 1241 | /** |
||
| 1242 | * Gets the array of discriminator values for the given ClassMetadata |
||
| 1243 | * |
||
| 1244 | * @param ClassMetadata $metadata |
||
| 1245 | * @return array |
||
| 1246 | */ |
||
| 1247 | 21 | private function getClassDiscriminatorValues(ClassMetadata $metadata) |
|
| 1263 | |||
| 1264 | 556 | private function handleCollections($document, $options) |
|
| 1283 | } |
||
| 1284 |
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: