简体   繁体   English

如何删除循环引用处理程序(Symfony 4)中已弃用的消息?

[英]How can I remove deprecated message for circular reference handler (Symfony 4)?

I am trying to remove this error message: 我正在尝试删除此错误消息:

User Deprecated: The "Symfony\\Component\\Serializer\\Normalizer\\AbstractNormalizer::setCircularReferenceHandler()" method is deprecated since Symfony 4.2, use the "circular_reference_handler" key of the context instead. 不推荐使用的用户:自Symfony 4.2起不推荐使用“ Symfony \\ Component \\ Serializer \\ Normalizer \\ AbstractNormalizer :: setCircularReferenceHandler()”方法,请使用上下文的“ circular_reference_handler”键。

Here is my code: 这是我的代码:

 $encoder = new JsonEncoder();
    $normalizer = new ObjectNormalizer();
    $normalizer->setCircularReferenceHandler(function ($object, string $format = null, array $context = []) {
        return $object->getName();
    });

I made a composer update and cache clear. 我做了作曲家的更新和缓存清除。 But nothing helps. 但是没有任何帮助。

The error message tells that you should give it in the defaultContext array you can give that as third parameter in the costructor. 错误消息告诉您应该在defaultContext数组中提供它,您可以在构造函数中将其作为第三个参数给出。

public function __construct(ClassMetadataFactoryInterface 
$classMetadataFactory = null, NameConverterInterface $nameConverter = null, array $defaultContext = array())

in your case it would be: 在您的情况下,它将是:

$encoders = array(new JsonEncoder());
$normalizer = new JsonSerializableNormalizer(null,null,array(JsonSerializableNormalizer::CIRCULAR_REFERENCE_HANDLER=>function ($object) {
    return (string)$object;
}));

EDIT: 编辑:
I was using a JsonSerializableNormalizer and you an ObjectNormalizer then the constructor definition is: 我使用的是JsonSerializableNormalizer,而您使用的是ObjectNormalizer,则构造函数定义为:

public function __construct(ClassMetadataFactoryInterface $classMetadataFactory = null, NameConverterInterface $nameConverter = null, PropertyAccessorInterface $propertyAccessor = null, PropertyTypeExtractorInterface $propertyTypeExtractor = null, ClassDiscriminatorResolverInterface $classDiscriminatorResolver = null, callable $objectClassResolver = null, array $defaultContext = array())

and used in your program it should be: 并在您的程序中使用它应该是:

$normalizer = new ObjectNormalizer(null,null,null,null,null,null,array(ObjectNormalizer::CIRCULAR_REFERENCE_HANDLER=>function ($object) {
    return (string)$object;
}));

you should use it ( circular_reference_handler ) as configuration key. 您应该使用它( circular_reference_handler )作为配置密钥。 For example, 例如,

serializer:
    circular_reference_handler: App\Service\YourHandlerService

I tried it in framework.yaml and it works. 我在framework.yaml尝试过,并且可以正常工作。

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

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