symfony 在规范化实体时找不到支持规范化器 [英] symfony no supporting normalizer found while normalizing an entity

查看:125
本文介绍了symfony 在规范化实体时找不到支持规范化器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发 symfony 4.1.我在 service.yml 中定义了两个规范化器.

I'm working on symfony 4.1. I defined two normalizer in my service.yml.

api.tone_normalizer:
    class: App\Serializer\Normalizer\JnToneNormalizer
    tags: [serializer.normalizer]

api.wskeytone_normalizer:
    class: App\Serializer\Normalizer\ApiWsKeyToneToneNormalizer
    tags: [serializer.normalizer]

这里是第一个规范化器.了解 JnTone 实体.

Here the first normalizer. Is aware about JnTone entities.

<?php
namespace App\Serializer\Normalizer;


use App\Entity\JnTone;
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
/**
 * JnTone normalizer
 */
class JnToneNormalizer implements NormalizerInterface
{
    /**
     * {@inheritdoc}
     */
    public function normalize($object, $format = null, array $context = array())
    {

        return [
            'id'   => $object->getId(),
            'name' => $object->getName(),
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function supportsNormalization($data, $format = null)
    {
        return $data instanceof JnTone;
    }
}

还有我想调用第一个的归一化器.rootTone 是 JnTone 实体的一个实例,所以我想调用我的 JnTone 规范器.

And the normalizer where I want to call the first one. rootTone is an instance of JnTone entity so I want to call my JnTone normalizer.

<?php
namespace App\Serializer\Normalizer;


use App\Entity\JnWsKey;
use App\Entity\JnTone;
use Symfony\Component\Serializer\Normalizer\NormalizerAwareInterface;
use Symfony\Component\Serializer\Normalizer\NormalizerAwareTrait;
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
use Symfony\Component\Serializer\Normalizer\SerializerAwareNormalizer;

use Symfony\Component\Serializer\SerializerAwareTrait;
use Symfony\Component\Serializer\SerializerAwareInterface;
/**
 * JnWsKey normalizer
 */
class ApiWsKeyNormalizer implements NormalizerInterface, SerializerAwareInterface
{
    use NormalizerAwareTrait;
    use SerializerAwareTrait;

    private $tones;

    /**
     * {@inheritdoc}
     */
    public function normalize($object, $format = null, array $context = array())
    {


        return [
            'id'=>$object->getId(),
            'name'=>$object->getName(),
            'rootTone'=>$this->serializer->normalize($object->getRootTone(),$format,$context)

        ];



    }


    /**
     * {@inheritdoc}
     */
    public function supportsNormalization($data, $format = null)
    {
        return $data instanceof JnWsKey ;
    }



}

我无法正常工作.找不到第一个规范化器

I can't get this working. The first normalizer isn't find

Could not normalize object of type App\Entity\JnTone, no supporting normalizer found.

我做错了什么?

推荐答案

我只是没有意识到我必须在序列化程序定义中声明所有需要的normailizer.我解决了它:

I just did not realize that I have to declare all needed normailizer in the serializer definition. I solved it doing:

$encoder = new JsonEncoder();

$serializer = new Serializer(array(

    new JnToneNormalizer(),
    new JnWsKeyToneNormalizer()

), array($encoder));

这篇关于symfony 在规范化实体时找不到支持规范化器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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