Symfony2的mongoDB返回一个loggablecursor而不是我的实体 [英] Symfony2 's mongoDB returns a loggablecursor instead of my entities

查看:79
本文介绍了Symfony2的mongoDB返回一个loggablecursor而不是我的实体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前使用DoctrineMongoDbBundle向我的mongodb数据库发出请求.

I currently use the DoctrineMongoDbBundle to make requests the my mongodb database.

这是我的控制器中的电话:

Here's the call in my controller:

    $dm = $this->get('doctrine.odm.mongodb.document_manager');
    $entities = $dm->getRepository('MyBundle:Animal')->findBy(array("prop" => "1")); 

    echo print_r($entities->getQuery());
    echo printf(count($entities));
    echo get_class($entities);

然后我尝试将$ enitities序列化为json并将其发送给客户端,但没有成功.

Then I tried to serialize $enitities to json and send it to the client but it didnt work.

打印出的回声:

Array ( [prop] => 1 ) 
101
Doctrine\ODM\MongoDB\LoggableCursor0

这表示查询正确,但计数应为"2",类型应为Animal数组.

It means that the query is correct but the count should be "2" and the type should be an Animal array.

为什么存储库返回LoggableCursor0而不是Animal数组?

Why is the reposity returning a LoggableCursor0 instead of an Animal array?

它如何返回Animal数组?

[edit] How could it return an Animal array?

[edit]以JSON返回结果的最佳方法是什么?

[edit] What would be the best way to return my result in JSON?

推荐答案

使用方法toArray().像这样:

$dm = $this->get('doctrine.odm.mongodb.document_manager');
$entities = $dm->getRepository('MyBundle:Animal')->findBy(array("prop" => "1"))->toArray(); 

如果需要获取实体数组,请使用array_values()函数.像这样:

If you need to get array of entities, use array_values() function. Like this:

$entities = array_values($entities);

这篇关于Symfony2的mongoDB返回一个loggablecursor而不是我的实体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆