2
votes

Hey There Im trying to map some documents in a mongodb and everything works perfect like this

/**
 * @MongoDB\Id
 */
protected $id;

/**
 * @MongoDB\Field(type="string",name="text")
 */
protected $text;

but what if i have a field with a refference to another document with an Object id like :

...
other:ObjectId("823789473938ab"),
...

but what do i have to set as type ?

/**
 * @MongoDB\Field(type="?",name="other")
 */
protected $other;

when i use the annotation above@MongoDB\Id it fails

i also tryed to use refferencene like

 * @MongoDB\ReferenceOne(targetDocument="Bundle1:Other")

leaving it argumentless always returns 0 when i do sth like this somewhere else

          $others=$dm
                 ->createQueryBuilder('Bundle1:Thing')
                 ->field('other')
                 ->equals('ObjectId("516c0061975a299edc44b419")')
                 ->getQuery()
                 ->execute()->count();

the query looks like :

doctrine.INFO: MongoDB query: {"find":true,"query":{"other":"ObjectId(\"516c0061975a299edc44b419\")"},"fields":[],"db":"maself","collection":"thing"}

so ofcourse theres a zero count, so part of the question also is how to query for all Things where other is a unique mongo id ?

for any help thanks in advance!

1

1 Answers

2
votes

Edit:

@MongoDB\ReferenceOne(targetDocument="Bundle1:Other", simple=true)

The option "simple" true, should work for you! The default reference format for Doctrine is DBRef()

You can query it by :

$qb->field('other')->references($OtherObject)

or

$qb->field('other')->equals(new \MongoId('01243efe..'))

ObjectId() is the native field type for mongo in PHP its \MongoId

if you want use @MongoDB\Id you have to import :

use Doctrine\ODM\MongoDB\Mapping\Annotations as MongoDB;

Take care with cache when editing annotations !

Tips : If you make getId(), this method will return a string and not a \MongoId