purekid / mongodm

MongoDB ORM that includes support for references,embed and multilevel inheritance.
MIT License
200 stars 47 forks source link

Find by Reference #57

Closed wmendes closed 10 years ago

wmendes commented 10 years ago

Im trying to find a model based on reference, my code is below:

$user = \Models\User::id($id);
$threads = \Models\Thread::find(array('user' => $user));

And im geting the error "zero-length keys are not allowed, did you use $ with double quotes?"

Someone know what im doing wrong? Thanks in advance and thanks for the great project.

wmendes commented 10 years ago

Sorry guys, solved!

The correct sintax is: $threads = \Models\Thread::find(array('user.$id' => $user->id));

jrschumacher commented 10 years ago

@wmendes yes that will work and is better than the first because you are only querying once.

If you need to specify the collection I often do this:

<?             
ModelA::find(array(
  'aRef' => array(
    '$in' => array(
      \MongoDBRef::create( ModelB::collectionName(), new \MongoId($id) )
    )
  )
));