简体   繁体   English

Symfony 3序列化器问题

[英]Symfony 3 serializer problems

Hello I am creating an API REST on Symfony 3.1. 您好,我正在Symfony 3.1上创建API REST。

I have got a problem Serializing and object. 我有一个序列化和对象问题。

These is the error it returns to me. 这些是它返回给我的错误。

A circular reference has been detected (configured limit: 1).

Stack overflow link I have read without any result. 我读过的堆栈溢出链接没有任何结果。

These is the documentation which I readed for try the seialize my obtject. 这些是我阅读的文档,可尝试使我的对象受益匪浅。

Here is the code for fill $employees: 以下是填补$ employees的代码:

$em = $this->getDoctrine()->getManager();
$dql = " SELECT e FROM BackendBundle:Employees e 
         INNER JOIN BackendBundle:Companies c 
         WITH e.idCompany = c.idCompany 
         WHERE c.idUser = ?1";                
$query = $em->createQuery($dql);
$query->setParameter(1,$user);
$employees = $query->getResult();

I Tryied these thing: 我尝试了这些东西:

  1. First proof 第一个证明

      use Symfony\\Component\\Serializer\\Serializer; use Symfony\\Component\\Serializer\\Encoder\\XmlEncoder; use Symfony\\Component\\Serializer\\Encoder\\JsonEncoder; use Symfony\\Component\\Serializer\\Normalizer\\ObjectNormalizer; $encoders = array(new XmlEncoder(), new JsonEncoder()); $normalizers = array(new ObjectNormalizer()); $serializer = new Serializer($normalizers,$encoders); $data = $serializer->serialize($employees, 'json'); 
  2. The sencond proof 第二证明

In these proof I readed by default on Symfony 3 Serialzer is deseabe. 在这些证明中,我默认在Symfony 3 Serialzer上读取的是deseabe。 For that I modified these files: 为此,我修改了这些文件:

app/config/config.yml app / config / config.yml

framework:
#esi:             ~
#translator:      { fallbacks: ["%locale%"] }
secret:          "%secret%"
router:
    resource: "%kernel.root_dir%/config/routing.yml"
    strict_requirements: ~
form:            ~
csrf_protection: ~
validation:      { enable_annotations: true }
#serializer:      { enable_annotations: true }
templating:
    engines: ['twig']
default_locale:  "%locale%"
trusted_hosts:   ~
trusted_proxies: ~
session:
    # http://symfony.com/doc/current/reference/configuration/framework.html#handler-id
    handler_id:  session.handler.native_file
    save_path:   "%kernel.root_dir%/../var/sessions/%kernel.environment%"
fragments:       ~
http_method_override: true
assets: ~
serializer:
    enabled: true
    enable_annotations: true

The unic important thing on here are the last lines where I active the serializer. 唯一重要的是我激活串行器的最后几行。

app/config/servces.yml app / config / servces.yml

services:    
get_set_method_normalizer:
    class: Symfony\Component\Serializer\Normalizer\GetSetMethodNormalizer
    public: false
    tags:
        - { name: serializer.normalizer }

src/AppBundle/Controller/DefaultController.php src / AppBundle / Controller / DefaultController.php

<?php
$serializer = $this->get('serializer');
$json = $serializer->serialize($employees,'json');

And little more proof where there is not much difference between you are reading and it. 还有一点证明,您正在阅读的书与书之间没有太大区别。

Please if someone know how serialize an object on Symfony 3. I wat trying it all morning with the same error. 请问是否有人知道如何在Symfony 3上序列化对象。我一整天都在尝试使用同一错误进行尝试。

A circular reference has been detected (configured limit: 1).

Your issue is that serializing Employees is serializing their company which again is having a reference to Employees, a perfect circular reference. 您的问题是序列化员工正在序列化他们的公司,这又是对员工的引用,这是一个完美的循环引用。

You can handle these circular references in Symfony's Serializer eg by catching the CircularReferenceException or by using custom callable in setCircularReferenceHandler and only serialize attributes which are not referencing back to the original entity. 您可以在Symfony的序列化器中处理这些循环引用,例如,通过捕获CircularReferenceException或通过在setCircularReferenceHandler中使用自定义可调用对象,并仅序列化不引用原始实体的属性。

See Symfony's documentation for a detailed description. 有关详细说明,请参见Symfony的文档

$normalizers->setCircularReferenceHandler(function ($object) {
        return $object->getId();
    });

add this bro after you make the instance of your ObjectNormalizer() . 在创建ObjectNormalizer()实例后添加此兄弟。 It work perfectly for me ! 它对我来说很完美!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM