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 |
||
| 43 | class DocumentPersister |
||
| 44 | { |
||
| 45 | /** |
||
| 46 | * The PersistenceBuilder instance. |
||
| 47 | * |
||
| 48 | * @var PersistenceBuilder |
||
| 49 | */ |
||
| 50 | private $pb; |
||
| 51 | |||
| 52 | /** |
||
| 53 | * The DocumentManager instance. |
||
| 54 | * |
||
| 55 | * @var DocumentManager |
||
| 56 | */ |
||
| 57 | private $dm; |
||
| 58 | |||
| 59 | /** |
||
| 60 | * The EventManager instance |
||
| 61 | * |
||
| 62 | * @var EventManager |
||
| 63 | */ |
||
| 64 | private $evm; |
||
| 65 | |||
| 66 | /** |
||
| 67 | * The UnitOfWork instance. |
||
| 68 | * |
||
| 69 | * @var UnitOfWork |
||
| 70 | */ |
||
| 71 | private $uow; |
||
| 72 | |||
| 73 | /** |
||
| 74 | * The ClassMetadata instance for the document type being persisted. |
||
| 75 | * |
||
| 76 | * @var ClassMetadata |
||
| 77 | */ |
||
| 78 | private $class; |
||
| 79 | |||
| 80 | /** |
||
| 81 | * The MongoCollection instance for this document. |
||
| 82 | * |
||
| 83 | * @var \MongoCollection |
||
| 84 | */ |
||
| 85 | private $collection; |
||
| 86 | |||
| 87 | /** |
||
| 88 | * Array of queued inserts for the persister to insert. |
||
| 89 | * |
||
| 90 | * @var array |
||
| 91 | */ |
||
| 92 | private $queuedInserts = array(); |
||
| 93 | |||
| 94 | /** |
||
| 95 | * Array of queued inserts for the persister to insert. |
||
| 96 | * |
||
| 97 | * @var array |
||
| 98 | */ |
||
| 99 | private $queuedUpserts = array(); |
||
| 100 | |||
| 101 | /** |
||
| 102 | * The CriteriaMerger instance. |
||
| 103 | * |
||
| 104 | * @var CriteriaMerger |
||
| 105 | */ |
||
| 106 | private $cm; |
||
| 107 | |||
| 108 | /** |
||
| 109 | * The CollectionPersister instance. |
||
| 110 | * |
||
| 111 | * @var CollectionPersister |
||
| 112 | */ |
||
| 113 | private $cp; |
||
| 114 | |||
| 115 | /** |
||
| 116 | * Initializes a new DocumentPersister instance. |
||
| 117 | * |
||
| 118 | * @param PersistenceBuilder $pb |
||
| 119 | * @param DocumentManager $dm |
||
| 120 | * @param EventManager $evm |
||
| 121 | * @param UnitOfWork $uow |
||
| 122 | * @param HydratorFactory $hydratorFactory |
||
| 123 | * @param ClassMetadata $class |
||
| 124 | */ |
||
| 125 | 679 | public function __construct(PersistenceBuilder $pb, DocumentManager $dm, EventManager $evm, UnitOfWork $uow, HydratorFactory $hydratorFactory, ClassMetadata $class, CriteriaMerger $cm = null) |
|
| 126 | { |
||
| 127 | 679 | $this->pb = $pb; |
|
| 128 | 679 | $this->dm = $dm; |
|
| 129 | 679 | $this->evm = $evm; |
|
| 130 | 679 | $this->cm = $cm ?: new CriteriaMerger(); |
|
| 131 | 679 | $this->uow = $uow; |
|
| 132 | 679 | $this->hydratorFactory = $hydratorFactory; |
|
|
|
|||
| 133 | 679 | $this->class = $class; |
|
| 134 | 679 | $this->collection = $dm->getDocumentCollection($class->name); |
|
| 135 | 679 | $this->cp = $this->uow->getCollectionPersister(); |
|
| 136 | 679 | } |
|
| 137 | |||
| 138 | /** |
||
| 139 | * @return array |
||
| 140 | */ |
||
| 141 | public function getInserts() |
||
| 145 | |||
| 146 | /** |
||
| 147 | * @param object $document |
||
| 148 | * @return bool |
||
| 149 | */ |
||
| 150 | public function isQueuedForInsert($document) |
||
| 154 | |||
| 155 | /** |
||
| 156 | * Adds a document to the queued insertions. |
||
| 157 | * The document remains queued until {@link executeInserts} is invoked. |
||
| 158 | * |
||
| 159 | * @param object $document The document to queue for insertion. |
||
| 160 | */ |
||
| 161 | 480 | public function addInsert($document) |
|
| 165 | |||
| 166 | /** |
||
| 167 | * @return array |
||
| 168 | */ |
||
| 169 | public function getUpserts() |
||
| 173 | |||
| 174 | /** |
||
| 175 | * @param object $document |
||
| 176 | * @return boolean |
||
| 177 | */ |
||
| 178 | public function isQueuedForUpsert($document) |
||
| 182 | |||
| 183 | /** |
||
| 184 | * Adds a document to the queued upserts. |
||
| 185 | * The document remains queued until {@link executeUpserts} is invoked. |
||
| 186 | * |
||
| 187 | * @param object $document The document to queue for insertion. |
||
| 188 | */ |
||
| 189 | 76 | public function addUpsert($document) |
|
| 193 | |||
| 194 | /** |
||
| 195 | * Gets the ClassMetadata instance of the document class this persister is used for. |
||
| 196 | * |
||
| 197 | * @return ClassMetadata |
||
| 198 | */ |
||
| 199 | public function getClassMetadata() |
||
| 203 | |||
| 204 | /** |
||
| 205 | * Executes all queued document insertions. |
||
| 206 | * |
||
| 207 | * Queued documents without an ID will inserted in a batch and queued |
||
| 208 | * documents with an ID will be upserted individually. |
||
| 209 | * |
||
| 210 | * If no inserts are queued, invoking this method is a NOOP. |
||
| 211 | * |
||
| 212 | * @param array $options Options for batchInsert() and update() driver methods |
||
| 213 | */ |
||
| 214 | 480 | public function executeInserts(array $options = array()) |
|
| 215 | { |
||
| 216 | 480 | if ( ! $this->queuedInserts) { |
|
| 217 | return; |
||
| 218 | } |
||
| 219 | |||
| 220 | 480 | $inserts = array(); |
|
| 221 | 480 | foreach ($this->queuedInserts as $oid => $document) { |
|
| 222 | 480 | $data = $this->pb->prepareInsertData($document); |
|
| 223 | |||
| 224 | // Set the initial version for each insert |
||
| 225 | 479 | View Code Duplication | if ($this->class->isVersioned) { |
| 226 | 39 | $versionMapping = $this->class->fieldMappings[$this->class->versionField]; |
|
| 227 | 39 | if ($versionMapping['type'] === 'int') { |
|
| 228 | 37 | $nextVersion = max(1, (int) $this->class->reflFields[$this->class->versionField]->getValue($document)); |
|
| 229 | 37 | $this->class->reflFields[$this->class->versionField]->setValue($document, $nextVersion); |
|
| 230 | 39 | } elseif ($versionMapping['type'] === 'date') { |
|
| 231 | 2 | $nextVersionDateTime = new \DateTime(); |
|
| 232 | 2 | $nextVersion = new \MongoDate($nextVersionDateTime->getTimestamp()); |
|
| 233 | 2 | $this->class->reflFields[$this->class->versionField]->setValue($document, $nextVersionDateTime); |
|
| 234 | 2 | } |
|
| 235 | 39 | $data[$versionMapping['name']] = $nextVersion; |
|
| 236 | 39 | } |
|
| 237 | |||
| 238 | 479 | $inserts[$oid] = $data; |
|
| 239 | 479 | } |
|
| 240 | |||
| 241 | 479 | if ($inserts) { |
|
| 242 | try { |
||
| 243 | 479 | $this->collection->batchInsert($inserts, $options); |
|
| 244 | 479 | } catch (\MongoException $e) { |
|
| 245 | 7 | $this->queuedInserts = array(); |
|
| 246 | 7 | throw $e; |
|
| 247 | } |
||
| 248 | 479 | } |
|
| 249 | |||
| 250 | /* All collections except for ones using addToSet have already been |
||
| 251 | * saved. We have left these to be handled separately to avoid checking |
||
| 252 | * collection for uniqueness on PHP side. |
||
| 253 | */ |
||
| 254 | 479 | foreach ($this->queuedInserts as $document) { |
|
| 255 | 479 | $this->handleCollections($document, $options); |
|
| 256 | 479 | } |
|
| 257 | |||
| 258 | 479 | $this->queuedInserts = array(); |
|
| 259 | 479 | } |
|
| 260 | |||
| 261 | /** |
||
| 262 | * Executes all queued document upserts. |
||
| 263 | * |
||
| 264 | * Queued documents with an ID are upserted individually. |
||
| 265 | * |
||
| 266 | * If no upserts are queued, invoking this method is a NOOP. |
||
| 267 | * |
||
| 268 | * @param array $options Options for batchInsert() and update() driver methods |
||
| 269 | */ |
||
| 270 | 76 | public function executeUpserts(array $options = array()) |
|
| 271 | { |
||
| 272 | 76 | if ( ! $this->queuedUpserts) { |
|
| 273 | return; |
||
| 274 | } |
||
| 275 | |||
| 276 | 76 | foreach ($this->queuedUpserts as $oid => $document) { |
|
| 277 | 76 | $data = $this->pb->prepareUpsertData($document); |
|
| 278 | |||
| 279 | // Set the initial version for each upsert |
||
| 280 | 76 | View Code Duplication | if ($this->class->isVersioned) { |
| 281 | 3 | $versionMapping = $this->class->fieldMappings[$this->class->versionField]; |
|
| 282 | 3 | if ($versionMapping['type'] === 'int') { |
|
| 283 | 2 | $nextVersion = max(1, (int) $this->class->reflFields[$this->class->versionField]->getValue($document)); |
|
| 284 | 2 | $this->class->reflFields[$this->class->versionField]->setValue($document, $nextVersion); |
|
| 285 | 3 | } elseif ($versionMapping['type'] === 'date') { |
|
| 286 | 1 | $nextVersionDateTime = new \DateTime(); |
|
| 287 | 1 | $nextVersion = new \MongoDate($nextVersionDateTime->getTimestamp()); |
|
| 288 | 1 | $this->class->reflFields[$this->class->versionField]->setValue($document, $nextVersionDateTime); |
|
| 289 | 1 | } |
|
| 290 | 3 | $data['$set'][$versionMapping['name']] = $nextVersion; |
|
| 291 | 3 | } |
|
| 292 | |||
| 293 | try { |
||
| 294 | 76 | $this->executeUpsert($data, $options); |
|
| 295 | 76 | $this->handleCollections($document, $options); |
|
| 296 | 76 | unset($this->queuedUpserts[$oid]); |
|
| 297 | 76 | } catch (\MongoException $e) { |
|
| 298 | unset($this->queuedUpserts[$oid]); |
||
| 299 | throw $e; |
||
| 300 | } |
||
| 301 | 76 | } |
|
| 302 | 76 | } |
|
| 303 | |||
| 304 | /** |
||
| 305 | * Executes a single upsert in {@link executeInserts} |
||
| 306 | * |
||
| 307 | * @param array $data |
||
| 308 | * @param array $options |
||
| 309 | */ |
||
| 310 | 76 | private function executeUpsert(array $data, array $options) |
|
| 311 | { |
||
| 312 | 76 | $options['upsert'] = true; |
|
| 313 | 76 | $criteria = array('_id' => $data['$set']['_id']); |
|
| 314 | 76 | unset($data['$set']['_id']); |
|
| 315 | |||
| 316 | // Do not send an empty $set modifier |
||
| 317 | 76 | if (empty($data['$set'])) { |
|
| 318 | 13 | unset($data['$set']); |
|
| 319 | 13 | } |
|
| 320 | |||
| 321 | /* If there are no modifiers remaining, we're upserting a document with |
||
| 322 | * an identifier as its only field. Since a document with the identifier |
||
| 323 | * may already exist, the desired behavior is "insert if not exists" and |
||
| 324 | * NOOP otherwise. MongoDB 2.6+ does not allow empty modifiers, so $set |
||
| 325 | * the identifier to the same value in our criteria. |
||
| 326 | * |
||
| 327 | * This will fail for versions before MongoDB 2.6, which require an |
||
| 328 | * empty $set modifier. The best we can do (without attempting to check |
||
| 329 | * server versions in advance) is attempt the 2.6+ behavior and retry |
||
| 330 | * after the relevant exception. |
||
| 331 | * |
||
| 332 | * See: https://jira.mongodb.org/browse/SERVER-12266 |
||
| 333 | */ |
||
| 334 | 76 | if (empty($data)) { |
|
| 335 | 13 | $retry = true; |
|
| 336 | 13 | $data = array('$set' => array('_id' => $criteria['_id'])); |
|
| 337 | 13 | } |
|
| 338 | |||
| 339 | try { |
||
| 340 | 76 | $this->collection->update($criteria, $data, $options); |
|
| 341 | 64 | return; |
|
| 342 | 13 | } catch (\MongoCursorException $e) { |
|
| 343 | 14 | if (empty($retry) || strpos($e->getMessage(), 'Mod on _id not allowed') === false) { |
|
| 344 | throw $e; |
||
| 345 | } |
||
| 346 | } |
||
| 347 | |||
| 348 | 13 | $this->collection->update($criteria, array('$set' => new \stdClass), $options); |
|
| 349 | 13 | } |
|
| 350 | |||
| 351 | /** |
||
| 352 | * Updates the already persisted document if it has any new changesets. |
||
| 353 | * |
||
| 354 | * @param object $document |
||
| 355 | * @param array $options Array of options to be used with update() |
||
| 356 | * @throws \Doctrine\ODM\MongoDB\LockException |
||
| 357 | */ |
||
| 358 | 211 | public function update($document, array $options = array()) |
|
| 359 | { |
||
| 360 | 211 | $id = $this->uow->getDocumentIdentifier($document); |
|
| 361 | 211 | $update = $this->pb->prepareUpdateData($document); |
|
| 362 | |||
| 363 | 211 | $id = $this->class->getDatabaseIdentifierValue($id); |
|
| 364 | 211 | $query = array('_id' => $id); |
|
| 365 | |||
| 366 | // Include versioning logic to set the new version value in the database |
||
| 367 | // and to ensure the version has not changed since this document object instance |
||
| 368 | // was fetched from the database |
||
| 369 | 211 | if ($this->class->isVersioned) { |
|
| 370 | 31 | $versionMapping = $this->class->fieldMappings[$this->class->versionField]; |
|
| 371 | 31 | $currentVersion = $this->class->reflFields[$this->class->versionField]->getValue($document); |
|
| 372 | 31 | if ($versionMapping['type'] === 'int') { |
|
| 373 | 28 | $nextVersion = $currentVersion + 1; |
|
| 374 | 28 | $update['$inc'][$versionMapping['name']] = 1; |
|
| 375 | 28 | $query[$versionMapping['name']] = $currentVersion; |
|
| 376 | 28 | $this->class->reflFields[$this->class->versionField]->setValue($document, $nextVersion); |
|
| 377 | 31 | } elseif ($versionMapping['type'] === 'date') { |
|
| 378 | 3 | $nextVersion = new \DateTime(); |
|
| 379 | 3 | $update['$set'][$versionMapping['name']] = new \MongoDate($nextVersion->getTimestamp()); |
|
| 380 | 3 | $query[$versionMapping['name']] = new \MongoDate($currentVersion->getTimestamp()); |
|
| 381 | 3 | $this->class->reflFields[$this->class->versionField]->setValue($document, $nextVersion); |
|
| 382 | 3 | } |
|
| 383 | 31 | } |
|
| 384 | |||
| 385 | 211 | if ( ! empty($update)) { |
|
| 386 | // Include locking logic so that if the document object in memory is currently |
||
| 387 | // locked then it will remove it, otherwise it ensures the document is not locked. |
||
| 388 | 150 | if ($this->class->isLockable) { |
|
| 389 | 11 | $isLocked = $this->class->reflFields[$this->class->lockField]->getValue($document); |
|
| 390 | 11 | $lockMapping = $this->class->fieldMappings[$this->class->lockField]; |
|
| 391 | 11 | if ($isLocked) { |
|
| 392 | 2 | $update['$unset'] = array($lockMapping['name'] => true); |
|
| 393 | 2 | } else { |
|
| 394 | 9 | $query[$lockMapping['name']] = array('$exists' => false); |
|
| 395 | } |
||
| 396 | 11 | } |
|
| 397 | |||
| 398 | 150 | $result = $this->collection->update($query, $update, $options); |
|
| 399 | |||
| 400 | 150 | View Code Duplication | if (($this->class->isVersioned || $this->class->isLockable) && ! $result['n']) { |
| 401 | 5 | throw LockException::lockFailed($document); |
|
| 402 | } |
||
| 403 | 146 | } |
|
| 404 | |||
| 405 | 207 | $this->handleCollections($document, $options); |
|
| 406 | 207 | } |
|
| 407 | |||
| 408 | /** |
||
| 409 | * Removes document from mongo |
||
| 410 | * |
||
| 411 | * @param mixed $document |
||
| 412 | * @param array $options Array of options to be used with remove() |
||
| 413 | * @throws \Doctrine\ODM\MongoDB\LockException |
||
| 414 | */ |
||
| 415 | 28 | public function delete($document, array $options = array()) |
|
| 416 | { |
||
| 417 | 28 | $id = $this->uow->getDocumentIdentifier($document); |
|
| 418 | 28 | $query = array('_id' => $this->class->getDatabaseIdentifierValue($id)); |
|
| 419 | |||
| 420 | 28 | if ($this->class->isLockable) { |
|
| 421 | 2 | $query[$this->class->lockField] = array('$exists' => false); |
|
| 422 | 2 | } |
|
| 423 | |||
| 424 | 28 | $result = $this->collection->remove($query, $options); |
|
| 425 | |||
| 426 | 28 | View Code Duplication | if (($this->class->isVersioned || $this->class->isLockable) && ! $result['n']) { |
| 427 | 2 | throw LockException::lockFailed($document); |
|
| 428 | } |
||
| 429 | 26 | } |
|
| 430 | |||
| 431 | /** |
||
| 432 | * Refreshes a managed document. |
||
| 433 | * |
||
| 434 | * @param array $id The identifier of the document. |
||
| 435 | * @param object $document The document to refresh. |
||
| 436 | */ |
||
| 437 | 20 | public function refresh($id, $document) |
|
| 438 | { |
||
| 439 | 20 | $data = $this->collection->findOne(array('_id' => $id)); |
|
| 440 | 20 | $data = $this->hydratorFactory->hydrate($document, $data); |
|
| 441 | 20 | $this->uow->setOriginalDocumentData($document, $data); |
|
| 442 | 20 | } |
|
| 443 | |||
| 444 | /** |
||
| 445 | * Finds a document by a set of criteria. |
||
| 446 | * |
||
| 447 | * If a scalar or MongoId is provided for $criteria, it will be used to |
||
| 448 | * match an _id value. |
||
| 449 | * |
||
| 450 | * @param mixed $criteria Query criteria |
||
| 451 | * @param object $document Document to load the data into. If not specified, a new document is created. |
||
| 452 | * @param array $hints Hints for document creation |
||
| 453 | * @param integer $lockMode |
||
| 454 | * @param array $sort Sort array for Cursor::sort() |
||
| 455 | * @throws \Doctrine\ODM\MongoDB\LockException |
||
| 456 | * @return object|null The loaded and managed document instance or null if no document was found |
||
| 457 | * @todo Check identity map? loadById method? Try to guess whether $criteria is the id? |
||
| 458 | */ |
||
| 459 | 351 | public function load($criteria, $document = null, array $hints = array(), $lockMode = 0, array $sort = null) |
|
| 460 | 1 | { |
|
| 461 | // TODO: remove this |
||
| 462 | 351 | if ($criteria === null || is_scalar($criteria) || $criteria instanceof \MongoId) { |
|
| 463 | $criteria = array('_id' => $criteria); |
||
| 464 | } |
||
| 465 | |||
| 466 | 351 | $criteria = $this->prepareQueryOrNewObj($criteria); |
|
| 467 | 351 | $criteria = $this->addDiscriminatorToPreparedQuery($criteria); |
|
| 468 | 351 | $criteria = $this->addFilterToPreparedQuery($criteria); |
|
| 469 | |||
| 470 | 351 | $cursor = $this->collection->find($criteria); |
|
| 471 | |||
| 472 | 351 | if (null !== $sort) { |
|
| 473 | 101 | $cursor->sort($this->prepareSortOrProjection($sort)); |
|
| 474 | 101 | } |
|
| 475 | |||
| 476 | 351 | $result = $cursor->getSingleResult(); |
|
| 477 | |||
| 478 | 351 | if ($this->class->isLockable) { |
|
| 479 | 1 | $lockMapping = $this->class->fieldMappings[$this->class->lockField]; |
|
| 480 | 1 | if (isset($result[$lockMapping['name']]) && $result[$lockMapping['name']] === LockMode::PESSIMISTIC_WRITE) { |
|
| 481 | 1 | throw LockException::lockFailed($result); |
|
| 482 | } |
||
| 483 | } |
||
| 484 | |||
| 485 | 350 | return $this->createDocument($result, $document, $hints); |
|
| 486 | } |
||
| 487 | |||
| 488 | /** |
||
| 489 | * Finds documents by a set of criteria. |
||
| 490 | * |
||
| 491 | * @param array $criteria Query criteria |
||
| 492 | * @param array $sort Sort array for Cursor::sort() |
||
| 493 | * @param integer|null $limit Limit for Cursor::limit() |
||
| 494 | * @param integer|null $skip Skip for Cursor::skip() |
||
| 495 | * @return Cursor |
||
| 496 | */ |
||
| 497 | 22 | public function loadAll(array $criteria = array(), array $sort = null, $limit = null, $skip = null) |
|
| 498 | { |
||
| 499 | 22 | $criteria = $this->prepareQueryOrNewObj($criteria); |
|
| 500 | 22 | $criteria = $this->addDiscriminatorToPreparedQuery($criteria); |
|
| 501 | 22 | $criteria = $this->addFilterToPreparedQuery($criteria); |
|
| 502 | |||
| 503 | 22 | $baseCursor = $this->collection->find($criteria); |
|
| 504 | 22 | $cursor = $this->wrapCursor($baseCursor); |
|
| 505 | |||
| 506 | 22 | if (null !== $sort) { |
|
| 507 | 3 | $cursor->sort($sort); |
|
| 508 | 3 | } |
|
| 509 | |||
| 510 | 22 | if (null !== $limit) { |
|
| 511 | 2 | $cursor->limit($limit); |
|
| 512 | 2 | } |
|
| 513 | |||
| 514 | 22 | if (null !== $skip) { |
|
| 515 | 2 | $cursor->skip($skip); |
|
| 516 | 2 | } |
|
| 517 | |||
| 518 | 22 | return $cursor; |
|
| 519 | } |
||
| 520 | |||
| 521 | /** |
||
| 522 | * Wraps the supplied base cursor in the corresponding ODM class. |
||
| 523 | * |
||
| 524 | * @param CursorInterface $baseCursor |
||
| 525 | * @return Cursor |
||
| 526 | */ |
||
| 527 | 22 | private function wrapCursor(CursorInterface $baseCursor) |
|
| 531 | |||
| 532 | /** |
||
| 533 | * Checks whether the given managed document exists in the database. |
||
| 534 | * |
||
| 535 | * @param object $document |
||
| 536 | * @return boolean TRUE if the document exists in the database, FALSE otherwise. |
||
| 537 | */ |
||
| 538 | 3 | public function exists($document) |
|
| 539 | { |
||
| 543 | |||
| 544 | /** |
||
| 545 | * Locks document by storing the lock mode on the mapped lock field. |
||
| 546 | * |
||
| 547 | * @param object $document |
||
| 548 | * @param int $lockMode |
||
| 549 | */ |
||
| 550 | 5 | public function lock($document, $lockMode) |
|
| 558 | |||
| 559 | /** |
||
| 560 | * Releases any lock that exists on this document. |
||
| 561 | * |
||
| 562 | * @param object $document |
||
| 563 | */ |
||
| 564 | 1 | public function unlock($document) |
|
| 572 | |||
| 573 | /** |
||
| 574 | * Creates or fills a single document object from an query result. |
||
| 575 | * |
||
| 576 | * @param object $result The query result. |
||
| 577 | * @param object $document The document object to fill, if any. |
||
| 578 | * @param array $hints Hints for document creation. |
||
| 579 | * @return object The filled and managed document object or NULL, if the query result is empty. |
||
| 580 | */ |
||
| 581 | 350 | private function createDocument($result, $document = null, array $hints = array()) |
|
| 595 | |||
| 596 | /** |
||
| 597 | * Loads a PersistentCollection data. Used in the initialize() method. |
||
| 598 | * |
||
| 599 | * @param PersistentCollection $collection |
||
| 600 | */ |
||
| 601 | 158 | public function loadCollection(PersistentCollection $collection) |
|
| 622 | |||
| 623 | 111 | private function loadEmbedManyCollection(PersistentCollection $collection) |
|
| 650 | |||
| 651 | 50 | private function loadReferenceManyCollectionOwningSide(PersistentCollection $collection) |
|
| 723 | |||
| 724 | 14 | private function loadReferenceManyCollectionInverseSide(PersistentCollection $collection) |
|
| 732 | |||
| 733 | /** |
||
| 734 | * @param PersistentCollection $collection |
||
| 735 | * |
||
| 736 | * @return Query |
||
| 737 | */ |
||
| 738 | 16 | public function createReferenceManyInverseSideQuery(PersistentCollection $collection) |
|
| 774 | |||
| 775 | 3 | private function loadReferenceManyWithRepositoryMethod(PersistentCollection $collection) |
|
| 788 | |||
| 789 | /** |
||
| 790 | * @param PersistentCollection $collection |
||
| 791 | * |
||
| 792 | * @return CursorInterface |
||
| 793 | */ |
||
| 794 | 3 | public function createReferenceManyWithRepositoryMethodCursor(PersistentCollection $collection) |
|
| 824 | |||
| 825 | /** |
||
| 826 | * Prepare a sort or projection array by converting keys, which are PHP |
||
| 827 | * property names, to MongoDB field names. |
||
| 828 | * |
||
| 829 | * @param array $fields |
||
| 830 | * @return array |
||
| 831 | */ |
||
| 832 | 138 | public function prepareSortOrProjection(array $fields) |
|
| 842 | |||
| 843 | /** |
||
| 844 | * Prepare a mongodb field name and convert the PHP property names to MongoDB field names. |
||
| 845 | * |
||
| 846 | * @param string $fieldName |
||
| 847 | * @return string |
||
| 848 | */ |
||
| 849 | 85 | public function prepareFieldName($fieldName) |
|
| 855 | |||
| 856 | /** |
||
| 857 | * Adds discriminator criteria to an already-prepared query. |
||
| 858 | * |
||
| 859 | * This method should be used once for query criteria and not be used for |
||
| 860 | * nested expressions. It should be called before |
||
| 861 | * {@link DocumentPerister::addFilterToPreparedQuery()}. |
||
| 862 | * |
||
| 863 | * @param array $preparedQuery |
||
| 864 | * @return array |
||
| 865 | */ |
||
| 866 | 475 | public function addDiscriminatorToPreparedQuery(array $preparedQuery) |
|
| 882 | |||
| 883 | /** |
||
| 884 | * Adds filter criteria to an already-prepared query. |
||
| 885 | * |
||
| 886 | * This method should be used once for query criteria and not be used for |
||
| 887 | * nested expressions. It should be called after |
||
| 888 | * {@link DocumentPerister::addDiscriminatorToPreparedQuery()}. |
||
| 889 | * |
||
| 890 | * @param array $preparedQuery |
||
| 891 | * @return array |
||
| 892 | */ |
||
| 893 | 476 | public function addFilterToPreparedQuery(array $preparedQuery) |
|
| 907 | |||
| 908 | /** |
||
| 909 | * Prepares the query criteria or new document object. |
||
| 910 | * |
||
| 911 | * PHP field names and types will be converted to those used by MongoDB. |
||
| 912 | * |
||
| 913 | * @param array $query |
||
| 914 | * @return array |
||
| 915 | */ |
||
| 916 | 509 | public function prepareQueryOrNewObj(array $query) |
|
| 943 | |||
| 944 | /** |
||
| 945 | * Prepares a query value and converts the PHP value to the database value |
||
| 946 | * if it is an identifier. |
||
| 947 | * |
||
| 948 | * It also handles converting $fieldName to the database name if they are different. |
||
| 949 | * |
||
| 950 | * @param string $fieldName |
||
| 951 | * @param mixed $value |
||
| 952 | * @param ClassMetadata $class Defaults to $this->class |
||
| 953 | * @param boolean $prepareValue Whether or not to prepare the value |
||
| 954 | * @return array Prepared field name and value |
||
| 955 | */ |
||
| 956 | 502 | private function prepareQueryElement($fieldName, $value = null, $class = null, $prepareValue = true) |
|
| 1131 | |||
| 1132 | /** |
||
| 1133 | * Prepares a query expression. |
||
| 1134 | * |
||
| 1135 | * @param array|object $expression |
||
| 1136 | * @param ClassMetadata $class |
||
| 1137 | * @return array |
||
| 1138 | */ |
||
| 1139 | 66 | private function prepareQueryExpression($expression, $class) |
|
| 1166 | |||
| 1167 | /** |
||
| 1168 | * Checks whether the value has DBRef fields. |
||
| 1169 | * |
||
| 1170 | * This method doesn't check if the the value is a complete DBRef object, |
||
| 1171 | * although it should return true for a DBRef. Rather, we're checking that |
||
| 1172 | * the value has one or more fields for a DBref. In practice, this could be |
||
| 1173 | * $elemMatch criteria for matching a DBRef. |
||
| 1174 | * |
||
| 1175 | * @param mixed $value |
||
| 1176 | * @return boolean |
||
| 1177 | */ |
||
| 1178 | 67 | private function hasDBRefFields($value) |
|
| 1196 | |||
| 1197 | /** |
||
| 1198 | * Checks whether the value has query operators. |
||
| 1199 | * |
||
| 1200 | * @param mixed $value |
||
| 1201 | * @return boolean |
||
| 1202 | */ |
||
| 1203 | 71 | private function hasQueryOperators($value) |
|
| 1221 | |||
| 1222 | /** |
||
| 1223 | * Gets the array of discriminator values for the given ClassMetadata |
||
| 1224 | * |
||
| 1225 | * @param ClassMetadata $metadata |
||
| 1226 | * @return array |
||
| 1227 | */ |
||
| 1228 | 21 | private function getClassDiscriminatorValues(ClassMetadata $metadata) |
|
| 1244 | |||
| 1245 | 543 | private function handleCollections($document, $options) |
|
| 1264 | } |
||
| 1265 |
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: