我想使用PHP解析mongo文档并在TWIG中显示该文档的元素。
但我现在被卡住了。
这是我的文档的一部分:
{"_id":
{"$id":"555f91d9fd2897e433000029"},
"users":[{"id":1735706894,
"id_str":"1735706894","name":"Funny Teen Quotes",
"screen_name":"D_Funny_Teens"},
{"id":1239321974,
"id_str":"1239321974",
"name":"Teen Quotes",
"screen_name":"SayingsOnTeens"} ...
这是我的函数PHP:
public function testAction()
{
$m = $this->container->get('doctrine_mongodb.odm.default_connection');
$db = $m->selectDatabase('test');
$collection = $db->createCollection('tweets');
$followcollection = $db->createCollection('followers');
$follow = $followcollection->findOne();
$k = 0;
foreach ($follow->users as $follower) {
$returns[$k] = array(
$follower->id,
$follower->id_str
);
$k = $k+1;
}
$return = json_encode($return);
return $this->render('ChartsBundle:Charts:test.html.twig',
array('returns'=> $returns
)); }
我的模板树枝:
{% for item in return %}
<li>{{ returns.id }}</li>
<li>{{ returns.id_str }}</li>
{% endfor %}
我收到此错误
Notice: Trying to get property of non-object
答案 0 :(得分:1)
请尝试:
(...)
$follow = (object) $followcollection->findOne();
$k = 0;
foreach ($follow->users as $arrFollower) {
$follower = (object)$arrFollower;
$returns[$k] = (object)array(
$follower->id,
$follower->id_str
);
$k = $k+1;
}
(...)
答案 1 :(得分:0)
我会使用DoctrineMongoDBBundle。这样您就可以使用常规PHP对象。