ObjectNormalizer会覆盖RelationshipNormalizer导致代码崩溃,为什么?

时间:2017-04-17 15:32:41

标签: php symfony symfony-3.2

我正在尝试添加自己的规范化器,因为我需要将一些原始值转换(非规范化)到它的相关实体。这就是我所做的:

namespace MMI\IntegrationBundle\Serializer\Normalizer;

use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;

class RelationshipNormalizer implements NormalizerInterface, DenormalizerInterface
{
    public function normalize($object, $format = null, array $context = [])
    {
        // @TODO implement this method
    }

    public function supportsNormalization($data, $format = null): bool
    {
        return $data instanceof \AgreementType;
    }

    public function denormalize($data, $class, $format = null, array $context = [])
    {
        // @TODO implement this method
    }

    public function supportsDenormalization($data, $type, $format = null): bool
    {
        $supportedTypes = [
            \AgreementType::class   => true
        ];

        return isset($supportedTypes[$type]);
    }
}

这就是我在控制器中使用它的方式:

    $propertyNameConverter = new PropertyNameConverter();
    $encoder               = new JsonEncoder();

    $normalizer = new ObjectNormalizer(
        null,
        $propertyNameConverter,
        null,
        new ReflectionExtractor()
    );

    $serializer = new Serializer([
        new DateTimeNormalizer(),
        new RelationshipNormalizer(),
        $normalizer,
        new ArrayDenormalizer(),
    ], [$encoder]);

当代码到达this method时:

private function getNormalizer($data, $format, array $context)
{
    foreach ($this->normalizers as $normalizer) {
        if ($normalizer instanceof NormalizerInterface && $normalizer->supportsNormalization($data, $format, $context)) {
            return $normalizer;
        }
    }
}

使用Xdebug和IDE,我可以看到条件$data instanceof \AgreementType是如何完成的,然后代码再次尝试检查Normalizer,然后执行this function

public function supportsDenormalization($data, $type, $format = null)
{
    return class_exists($type);
}

这正是我得到错误的规范化器导致以下错误的地方:

  

注意:未初始化的字符串偏移量:0英寸   供应商/ symfony的/ symfony的/ SRC / Symfony的/组件/变形器/ Inflector.php   在第179行

更新

我尝试过另一种方式,结果与之前完全相同,意思是相同的错误信息:

$callback = function ($value) {
    $value = $this->em->getRepository('QuoteBundle:' . $this->table_mapping[$this->entity])->find($value);

    return $value;
};

$entityNormalizer = new GetSetMethodNormalizer();
$entityNormalizer->setCallbacks([
    'agreementType'  => $callback,
]);

$serializer = new Serializer([
    new DateTimeNormalizer(),
    $normalizer,
    $entityNormalizer,
    new ArrayDenormalizer(),
], [$encoder]);

我在这里缺少什么?

1 个答案:

答案 0 :(得分:0)

在Slack上的#symfony-devs频道获得一些帮助后,我确实发现订单很重要。这是我的问题的解决方案(将下面的代码与OP上的代码进行比较,您将看到差异):

$normalizer = new ObjectNormalizer(
    null,
    $propertyNameConverter,
    null,
    new ReflectionExtractor()
);

// Notice how ObjectNormalizer() is the last normalizer
$serializer = new Serializer([
    new ArrayDenormalizer(),
    new DateTimeNormalizer(),
    new RelationshipNormalizer($em),
    $normalizer,
], [$encoder]);