【问题标题】:Resources are not supported in serialized data MongoDB JMSSerializer序列化数据MongoDB JMSSerializer不支持资源
【发布时间】:2013-09-17 14:05:34
【问题描述】:

我正在尝试开发一个 API(基于 DoctrineMongoDBBundle 和 FOSRestBundle),它将所有空洞数据存储在 MongoDB 中,恢复它,并使用 FOSRest 显示它。

当我尝试显示存储在数据库中的对象列表时,这就是我得到的:

"message": "序列化数据不支持资源。路径: Monolog\Handler\StreamHandler -> Symfony\Bridge\Monolog\Logger -> Doctrine\Bundle\MongoDBBundle\Logger\Logger -> Doctrine\Bundle\MongoDBBundle\Logger\AggregateLogger -> 教义\ODM\MongoDB\Configuration -> 教义\MongoDB\Connection -> Doctrine\ODM\MongoDB\LoggableCursor", "class": "JMS\Serializer\Exception\RuntimeException",

我的 FOS config.yml 是这样的:

fos_rest:
    param_fetcher_listener: true
    body_listener: true
    format_listener: true
    view:
        view_response_listener: force

那是我的控制器:

/**
 * @Route("/services.{_format}", defaults={"_format" = "json"})
 * @REST\View()
 */
public function getServicesAction($_format){
    $services = $this->get('doctrine_mongodb')->getManager()
        ->getRepository('RadmasOpen010Bundle:Service')
        ->findAll();

    return $services;
}

我尝试返回一个格式正确的数组,但没有显示任何错误,以及如果我将所有服务对象加入一个数组。

我尝试使用 PECL 安装 Mongo PHP 扩展来解决它,更改了一些配置,但没有任何改变。

有人知道会发生什么吗?

【问题讨论】:

    标签: mongodb doctrine-orm symfony-2.3 fosrestbundle jmsserializerbundle


    【解决方案1】:

    如果您想获取 RadmasOpen010Bundle:Service 文档的集合,您必须在从存储库调用 find 方法或从查询构建器调用 getQuery 方法后调用方法“toArray”

    /**
     * @Route("/services.{_format}", defaults={"_format" = "json"})
     * @REST\View()
     */
    public function getServicesAction($_format){
        $services = $this->get('doctrine_mongodb')->getManager()
            ->getRepository('RadmasOpen010Bundle:Service')
            ->findAll()->toArray();
    
        return $services;
    }
    

    您也可以调用 array_values($services) 来正确序列化排除策略

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-10-17
      • 1970-01-01
      • 1970-01-01
      • 2011-04-03
      • 1970-01-01
      • 1970-01-01
      • 2014-01-23
      相关资源
      最近更新 更多