Skip to content

Commit 6f13cf4

Browse files
author
Andreas Braun
committed
Use CursorInterface in DocumentPersister
1 parent 32b74d4 commit 6f13cf4

2 files changed

Lines changed: 14 additions & 24 deletions

File tree

docs/en/reference/complex-references.rst

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -127,10 +127,9 @@ The ``Comment`` class will need to have a custom repository class configured:
127127
}
128128
129129
Lastly, the ``CommentRepository`` class will need a ``findSomeComments()``
130-
method which shall return either ``Doctrine\ODM\MongoDB\Cursor`` or
131-
``Doctrine\ODM\MongoDB\EagerCursor``. When this method is called to populate
132-
the reference, Doctrine will provide the Blogpost instance (i.e. owning
133-
document) as the first argument:
130+
method which shall return ``Doctrine\MongoDB\CursorInterface``. When this method
131+
is called to populate the reference, Doctrine will provide the Blogpost instance
132+
(i.e. owning document) as the first argument:
134133

135134
.. code-block:: php
136135

lib/Doctrine/ODM/MongoDB/Persisters/DocumentPersister.php

Lines changed: 11 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,9 @@
2020
namespace Doctrine\ODM\MongoDB\Persisters;
2121

2222
use Doctrine\Common\EventManager;
23-
use Doctrine\MongoDB\Cursor as BaseCursor;
23+
use Doctrine\MongoDB\CursorInterface;
2424
use Doctrine\ODM\MongoDB\Cursor;
2525
use Doctrine\ODM\MongoDB\DocumentManager;
26-
use Doctrine\ODM\MongoDB\EagerCursor;
2726
use Doctrine\ODM\MongoDB\Utility\CollectionHelper;
2827
use Doctrine\ODM\MongoDB\Hydrator\HydratorFactory;
2928
use Doctrine\ODM\MongoDB\LockException;
@@ -507,22 +506,15 @@ public function loadAll(array $criteria = array(), array $sort = null, $limit =
507506
$baseCursor = $this->collection->find($criteria);
508507
$cursor = $this->wrapCursor($baseCursor);
509508

510-
/* The wrapped cursor may be used if the ODM cursor becomes wrapped with
511-
* an EagerCursor, so we should apply the same sort, limit, and skip
512-
* options to both cursors.
513-
*/
514509
if (null !== $sort) {
515-
$baseCursor->sort($this->prepareSortOrProjection($sort));
516510
$cursor->sort($sort);
517511
}
518512

519513
if (null !== $limit) {
520-
$baseCursor->limit($limit);
521514
$cursor->limit($limit);
522515
}
523516

524517
if (null !== $skip) {
525-
$baseCursor->skip($skip);
526518
$cursor->skip($skip);
527519
}
528520

@@ -532,10 +524,10 @@ public function loadAll(array $criteria = array(), array $sort = null, $limit =
532524
/**
533525
* Wraps the supplied base cursor in the corresponding ODM class.
534526
*
535-
* @param BaseCursor $cursor
527+
* @param CursorInterface $baseCursor
536528
* @return Cursor
537529
*/
538-
private function wrapCursor(BaseCursor $baseCursor)
530+
private function wrapCursor(CursorInterface $baseCursor)
539531
{
540532
return new Cursor($baseCursor, $this->dm->getUnitOfWork(), $this->class);
541533
}
@@ -795,7 +787,7 @@ private function loadReferenceManyWithRepositoryMethod(PersistentCollection $col
795787
/**
796788
* @param PersistentCollection $collection
797789
*
798-
* @return Cursor|EagerCursor
790+
* @return CursorInterface
799791
*/
800792
public function createReferenceManyWithRepositoryMethodCursor(PersistentCollection $collection)
801793
{
@@ -804,25 +796,24 @@ public function createReferenceManyWithRepositoryMethodCursor(PersistentCollecti
804796
$cursor = $this->dm->getRepository($mapping['targetDocument'])
805797
->$mapping['repositoryMethod']($collection->getOwner());
806798

807-
$wrappedCursor = $cursor;
808-
if ($cursor instanceof EagerCursor) {
809-
$wrappedCursor = $cursor->getCursor();
799+
if ( ! $cursor instanceof CursorInterface) {
800+
throw new \BadMethodCallException("Expected repository method {$mapping['repositoryMethod']} to return a CursorInterface");
810801
}
811802

812803
if (isset($mapping['sort'])) {
813-
$wrappedCursor->sort($mapping['sort']);
804+
$cursor->sort($mapping['sort']);
814805
}
815806
if (isset($mapping['limit'])) {
816-
$wrappedCursor->limit($mapping['limit']);
807+
$cursor->limit($mapping['limit']);
817808
}
818809
if (isset($mapping['skip'])) {
819-
$wrappedCursor->skip($mapping['skip']);
810+
$cursor->skip($mapping['skip']);
820811
}
821812
if ( ! empty($hints[Query::HINT_SLAVE_OKAY])) {
822-
$wrappedCursor->slaveOkay(true);
813+
$cursor->slaveOkay(true);
823814
}
824815
if ( ! empty($hints[Query::HINT_READ_PREFERENCE])) {
825-
$wrappedCursor->setReadPreference($hints[Query::HINT_READ_PREFERENCE], $hints[Query::HINT_READ_PREFERENCE_TAGS]);
816+
$cursor->setReadPreference($hints[Query::HINT_READ_PREFERENCE], $hints[Query::HINT_READ_PREFERENCE_TAGS]);
826817
}
827818

828819
return $cursor;

0 commit comments

Comments
 (0)