Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

getFirstResult nulls Cursor's Limit #1018

Closed
baileylo opened this issue Jan 26, 2015 · 5 comments · Fixed by #1206
Closed

getFirstResult nulls Cursor's Limit #1018

baileylo opened this issue Jan 26, 2015 · 5 comments · Fixed by #1206
Assignees
Labels
Milestone

Comments

@baileylo
Copy link
Contributor

Issue

Running Doctrine\ODM\MongoDB\Cursor::getSingleResult() will clear limit from Cursor.

Example Code

<?php

$objects = $myObjectRepo->createQueryBuilder()
    ->limit(10)
    ->getQuery()
    ->execute();

// Access First Result:
$firstObject = $objects->getSingleResult();

// limit has been removed from $objects.

Reason

This is the implementation of getSingleResult from Doctrine\MongoDB\Cursor.

    public function getSingleResult()
    {
        $originalLimit = $this->limit;
        $this->reset();
        $this->limit(1);
        $result = current($this->toArray(false)) ?: null;
        $this->reset();
        $this->limit($originalLimit);
        return $result;
    }

It's retrieving the cursors value using $this->limit. Since Doctrine\ODM\MongoDB\Cursor extends Doctrine\MongoDB\Cursor, it has a cursor property. Doctrine\ODM\MongoDB\Cursor acts as decorator for Doctrine\MongoDB\Cursor and should return all values from Doctrine\ODM\MongoDB\Cursor::$baseCursor. Accessing the property breaks or ignores this design and returns the null $limit value of Doctrine\ODM\MongoDB\Cursor.

This could be fixed by creating a protected accessor $this->getLimit() on both Cursors, the ODM cursor though would return the result of $this->baseCursor->getLimit().

@baileylo
Copy link
Contributor Author

For now, I've begun using

$firstObject = $objects->getNext();
$objects->reset();

Is there a better way to do this?

@austinh
Copy link

austinh commented Jul 27, 2015

+1 Im running into this as well.

@malarzm malarzm added the Bug label Jul 28, 2015
@malarzm malarzm added this to the 1.0.0 milestone Jul 28, 2015
@malarzm
Copy link
Member

malarzm commented Jul 28, 2015

I'm sorry, I must have overlooked this issue before, tagged it to get back here shortly.

@jmikola
Copy link
Member

jmikola commented Aug 18, 2015

@alcaeus: Can you confirm if #1206 will resolve this?

EDIT: Yes, indeed it does.

@alcaeus
Copy link
Member

alcaeus commented Aug 18, 2015

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants