[英]symfony mongodb odm referenceMany
嗨,专家:我在构建查询以提取使用'referenceMany'引用的文档详细信息时遇到问题。 这是代码的示例设置
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();
}
这个想法是1 FoodItem可以与其他FoodItems最佳化,因此'bestEatenWith'是属于同一'FoodItem'文档的'Id'的数组。
我使用的查询如下:
$qb = $dm->createQueryBuilder('AppBundle:FoodItem')
->select("name", "bestEatenWith", "nutrition")
->eagerCursor(true)
->hydrate(false);
$query = $qb->getQuery();
$result = $query->execute();
然后使用Twig模板显示我得到的结果,并且可以在Twig中显示“名称”和“营养”,但是我将“ bestEatenWith”作为“ Id”的数组显示
问题1:如何使用PHP解析$ result并访问'bestEatenWith'数组中包含的'Id'?
问题2:我能否在单个查询中获得类似“ bestEatenWith.name”的信息?
问题1
在控制器中使用dump
功能转储结果:
dump($result);
问题2
通常,您可以从$result
存在的bestEatenWith
获取名称
感谢Mohamed的回答,您已经回答了我的部分问题。 但是,我本人已经找到了第二部分的答案,因此可以得出完整的答案。
这是我所做的-
答案1
答案2
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.