使用自定义标准化器修改 class 属性
Use custom normalizer to modify class properties
我有一个具有 2 个属性的实体,name
和 photo
。 name
属性 是从数据库中读取的,但我必须用一些其他信息填充 photo
属性。
我遵循了文档中的 Writing a Custom Nomalizer 教程,并且制作了自定义规范化器:
<?php
namespace App\Serializer;
use App\Entity\Style;
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
use Vich\UploaderBundle\Templating\Helper\UploaderHelper;
final class StyleNormalizer implements NormalizerInterface, DenormalizerInterface
{
private $normalizer;
private $uploaderHelper;
public function __construct(NormalizerInterface $normalizer, UploaderHelper $uploaderHelper)
{
if (!$normalizer instanceof DenormalizerInterface) {
throw new \InvalidArgumentException('The normalizer must implement the DenormalizerInterface');
}
$this->normalizer = $normalizer;
$this->uploaderHelper = $uploaderHelper;
}
public function denormalize($data, $class, $format = null, array $context = [])
{
return $this->normalizer->denormalize($data, $class, $format, $context);
}
public function supportsDenormalization($data, $type, $format = null)
{
return $this->normalizer->supportsDenormalization($data, $type, $format);
}
public function normalize($object, $format = null, array $context = [])
{
if ($object instanceof Style) {
$object->setPhoto('http://api-platform.com');
}
return $this->normalizer->normalize($object, $format, $context);
}
public function supportsNormalization($data, $format = null)
{
return $this->normalizer->supportsNormalization($data, $format);
}
}
但是photo
属性没有填入需要的信息
经过一些调试后,我发现 supportsNormalization
方法执行了两次(针对每个数据库元素)。如果我打印 $data
变量,我第一次得到实体 name
属性,第二次得到 photo
属性 和 null
值.我从来没有得到整个 Style
实体。然后 supportsNormalitzation
方法总是 returns false
.
如何获取完整的 Style
实体并修改其属性?
谢谢!
尝试将此添加到您的 supportsNormalization
方法中:
public function supportsNormalization($data, $format = null)
{
return
$this->normalizer->supportsNormalization($data, $format)
&& is_object($data) && $data instanceof Style::class
;
}
我有一个具有 2 个属性的实体,name
和 photo
。 name
属性 是从数据库中读取的,但我必须用一些其他信息填充 photo
属性。
我遵循了文档中的 Writing a Custom Nomalizer 教程,并且制作了自定义规范化器:
<?php
namespace App\Serializer;
use App\Entity\Style;
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
use Vich\UploaderBundle\Templating\Helper\UploaderHelper;
final class StyleNormalizer implements NormalizerInterface, DenormalizerInterface
{
private $normalizer;
private $uploaderHelper;
public function __construct(NormalizerInterface $normalizer, UploaderHelper $uploaderHelper)
{
if (!$normalizer instanceof DenormalizerInterface) {
throw new \InvalidArgumentException('The normalizer must implement the DenormalizerInterface');
}
$this->normalizer = $normalizer;
$this->uploaderHelper = $uploaderHelper;
}
public function denormalize($data, $class, $format = null, array $context = [])
{
return $this->normalizer->denormalize($data, $class, $format, $context);
}
public function supportsDenormalization($data, $type, $format = null)
{
return $this->normalizer->supportsDenormalization($data, $type, $format);
}
public function normalize($object, $format = null, array $context = [])
{
if ($object instanceof Style) {
$object->setPhoto('http://api-platform.com');
}
return $this->normalizer->normalize($object, $format, $context);
}
public function supportsNormalization($data, $format = null)
{
return $this->normalizer->supportsNormalization($data, $format);
}
}
但是photo
属性没有填入需要的信息
经过一些调试后,我发现 supportsNormalization
方法执行了两次(针对每个数据库元素)。如果我打印 $data
变量,我第一次得到实体 name
属性,第二次得到 photo
属性 和 null
值.我从来没有得到整个 Style
实体。然后 supportsNormalitzation
方法总是 returns false
.
如何获取完整的 Style
实体并修改其属性?
谢谢!
尝试将此添加到您的 supportsNormalization
方法中:
public function supportsNormalization($data, $format = null)
{
return
$this->normalizer->supportsNormalization($data, $format)
&& is_object($data) && $data instanceof Style::class
;
}