Search code examples
symfonydoctrinedoctrine-ormdoctrine-odm

Is there a smarter way to get a referenced documents ID without querying for it and hydrating it?


I have a document which contains references to some other documents. Like most people, I am using a MongoId in an "id" field in URLs in order to view these documents. The situation is that I want to display links to these referenced documents from the main document. In order to get the ID of the referenced document I am using code like this:

$mainDocument->getReferencedDoc()->getId();

The obvious annoyance here is that doctrine will run another query to fetch that referenced document just so I can get the ID. But the ID technically already exists in the main document in the reference field. Shouldn't there be a more efficient way to get only the ID without having to query and hydrate the entire referenced document? Do I have to write custom queries every time I want to do this?

The problem becomes significant when you're generating a large list of the main documents containing links to their referenced documents.


Solution

  • It seems it does behave this way since this commit on Feb 6th. So this issue is resolved by using the latest mongodb-odm.