Skip to content

Commit

Permalink
Use CursorInterface in DocumentPersister
Browse files Browse the repository at this point in the history
  • Loading branch information
Andreas Braun committed Aug 18, 2015
1 parent 32b74d4 commit 6f13cf4
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 24 deletions.
7 changes: 3 additions & 4 deletions docs/en/reference/complex-references.rst
Original file line number Diff line number Diff line change
Expand Up @@ -127,10 +127,9 @@ The ``Comment`` class will need to have a custom repository class configured:
}
Lastly, the ``CommentRepository`` class will need a ``findSomeComments()``
method which shall return either ``Doctrine\ODM\MongoDB\Cursor`` or
``Doctrine\ODM\MongoDB\EagerCursor``. When this method is called to populate
the reference, Doctrine will provide the Blogpost instance (i.e. owning
document) as the first argument:
method which shall return ``Doctrine\MongoDB\CursorInterface``. When this method
is called to populate the reference, Doctrine will provide the Blogpost instance
(i.e. owning document) as the first argument:

.. code-block:: php
Expand Down
31 changes: 11 additions & 20 deletions lib/Doctrine/ODM/MongoDB/Persisters/DocumentPersister.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,9 @@
namespace Doctrine\ODM\MongoDB\Persisters;

use Doctrine\Common\EventManager;
use Doctrine\MongoDB\Cursor as BaseCursor;
use Doctrine\MongoDB\CursorInterface;
use Doctrine\ODM\MongoDB\Cursor;
use Doctrine\ODM\MongoDB\DocumentManager;
use Doctrine\ODM\MongoDB\EagerCursor;
use Doctrine\ODM\MongoDB\Utility\CollectionHelper;
use Doctrine\ODM\MongoDB\Hydrator\HydratorFactory;
use Doctrine\ODM\MongoDB\LockException;
Expand Down Expand Up @@ -507,22 +506,15 @@ public function loadAll(array $criteria = array(), array $sort = null, $limit =
$baseCursor = $this->collection->find($criteria);
$cursor = $this->wrapCursor($baseCursor);

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

if (null !== $limit) {
$baseCursor->limit($limit);
$cursor->limit($limit);
}

if (null !== $skip) {
$baseCursor->skip($skip);
$cursor->skip($skip);
}

Expand All @@ -532,10 +524,10 @@ public function loadAll(array $criteria = array(), array $sort = null, $limit =
/**
* Wraps the supplied base cursor in the corresponding ODM class.
*
* @param BaseCursor $cursor
* @param CursorInterface $baseCursor
* @return Cursor
*/
private function wrapCursor(BaseCursor $baseCursor)
private function wrapCursor(CursorInterface $baseCursor)
{
return new Cursor($baseCursor, $this->dm->getUnitOfWork(), $this->class);
}
Expand Down Expand Up @@ -795,7 +787,7 @@ private function loadReferenceManyWithRepositoryMethod(PersistentCollection $col
/**
* @param PersistentCollection $collection
*
* @return Cursor|EagerCursor
* @return CursorInterface
*/
public function createReferenceManyWithRepositoryMethodCursor(PersistentCollection $collection)
{
Expand All @@ -804,25 +796,24 @@ public function createReferenceManyWithRepositoryMethodCursor(PersistentCollecti
$cursor = $this->dm->getRepository($mapping['targetDocument'])
->$mapping['repositoryMethod']($collection->getOwner());

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

if (isset($mapping['sort'])) {
$wrappedCursor->sort($mapping['sort']);
$cursor->sort($mapping['sort']);
}
if (isset($mapping['limit'])) {
$wrappedCursor->limit($mapping['limit']);
$cursor->limit($mapping['limit']);
}
if (isset($mapping['skip'])) {
$wrappedCursor->skip($mapping['skip']);
$cursor->skip($mapping['skip']);
}
if ( ! empty($hints[Query::HINT_SLAVE_OKAY])) {
$wrappedCursor->slaveOkay(true);
$cursor->slaveOkay(true);
}
if ( ! empty($hints[Query::HINT_READ_PREFERENCE])) {
$wrappedCursor->setReadPreference($hints[Query::HINT_READ_PREFERENCE], $hints[Query::HINT_READ_PREFERENCE_TAGS]);
$cursor->setReadPreference($hints[Query::HINT_READ_PREFERENCE], $hints[Query::HINT_READ_PREFERENCE_TAGS]);
}

return $cursor;
Expand Down

0 comments on commit 6f13cf4

Please sign in to comment.