Hi experts: I have a problem building a query to extract details of the document that has been referenced using 'referenceMany'. Here is the example setup of the code
class FoodItem{
/**
* @MongoDb\Id
*/
protected Id;
/**
* @MongoDb\field(type="string")
*/
protected name;
/**
* @MongoDb\field(type="float")
*/
protected calories;
/**
* @MongoDb\ReferenceMany(targetDocument="FoodItem", storeAs="id")
*/
protected $bestEatenWith = array();
}
The idea is 1 FoodItem can be bestEatenWith other FoodItems, therefore 'bestEatenWith' is an array of 'Id's belonging to the same 'FoodItem' document.
The query I used is as follows -
$qb = $dm->createQueryBuilder('AppBundle:FoodItem')
->select("name", "bestEatenWith", "nutrition")
->eagerCursor(true)
->hydrate(false);
$query = $qb->getQuery();
$result = $query->execute();
The result I get is then displayed using Twig template, and I can display in Twig, the 'name' and 'nutrition', but I get 'bestEatenWith' as an array of 'Id's
Question 1: How do I parse through the $result using PHP and access the 'Id's contained in the array of 'bestEatenWith' ?
Question 2: Can I not possibly get something like 'bestEatenWith.name' in a single query?
Thanks Mohamed for your answer, you have answered part of my question. However I have the found the answer myself to the second part so that makes it a full answer.
Here is what I did-
Answer 1
Answer 2