Added ability to return documents without the id as the key in Cursor::toArray() #78

Merged
merged 1 commit into from Dec 18, 2012

Conversation

Projects
None yet
3 participants
Contributor

j commented Nov 20, 2012

I didn't pass this along to the eager cursor, but can if it's necessary.

It beats doing

return [
    'data' => array_values($cursor->toArray())
];

every time I want to remove the document ids from the array!

:)

Owner

jmikola commented Dec 2, 2012

@jstout24: There are some additional invocations of Cursor::toArray(). Should they also be updated?

  • Query::toArray() calls toArray() on the inner iterator, which is a cursor.
  • EagerCursor::initialize() calls the inner cursor's toArray() method. If we add $useKeys here, then we'd need it in all the places that call initialize().
  • Cursor::getSingleResult() calls toArray() and grabs the first element. In that case, the key really doesn't matter, so we can likely pass false.

I think supporting Query and Cursor would be straightforward, but I'll defer to @jwage about EagerCursor, since that was his creation.

Owner

jmikola commented Dec 2, 2012

Also, supporting $useKeys is related to an issue brought up in bobthecow/genghis#51, which I filed as PHP-570. If one is using objects as _id values in lieu of scalars or MongoId instances, the object cannot be used as an array key. In that case, you'd definitely want to specify $useKeys as false.

Contributor

j commented Dec 4, 2012

Yep, I've ran into that same issue before. I started rewriting this PR, and made a fluent, useKeys() method in the Cursor and EagerCursor to match, but it feels odd doing it this way because it only pertains to the toArray method in the cursor, which reminded me why I didn't touch EagerCursor when I opened this PR 👅

Anyway, in the eager cursor, suppose I simply do an array_values on the data of the toArray() method since that's what I'm forced to do right now in my app anyway.. either that, or we can go the fluid route.

Contributor

j commented Dec 4, 2012

https://github.com/jstout24/mongodb/commit/1432a58eb0ec29c390adfaaf41ba60f2c2486ae5 is the fluid example

Usage would be:

$users = $dm->createQueryBuilder('User')
    ->getQuery()
    ->execute()
    ->useKeys(false)
    ->toArray();

jmikola referenced this pull request in doctrine/mongodb-odm Dec 13, 2012

Closed

toArray without keys #463

Owner

jmikola commented Dec 17, 2012

@jwage: While you're reviewing the old/new driver PR, please take a look at this as well and chime in.

Owner

jwage commented Dec 18, 2012

👍

@jwage jwage added a commit that referenced this pull request Dec 18, 2012

@jwage jwage Merge pull request #78 from jstout24/use-keys-arg
Added ability to return documents without the id as the key in Cursor::toArray()
eb3a3c3

@jwage jwage merged commit eb3a3c3 into doctrine:master Dec 18, 2012

1 check passed

default The Travis build passed
Details
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment