生の値を関連するエンティティに変換(非正規化)する必要があるため、自分のノーマライザを追加しようとしています。これは私がやっていることです:ObjectNormalizerがRelationshipNormalizerをオーバーライドし、コードがクラッシュする原因は何ですか?
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リーチ:Xdebugをと使用
private function getNormalizer($data, $format, array $context)
{
foreach ($this->normalizers as $normalizer) {
if ($normalizer instanceof NormalizerInterface && $normalizer->supportsNormalization($data, $format, $context)) {
return $normalizer;
}
}
}
をIDE $data instanceof \AgreementType
の状態がどのように達成されているかを確認できますが、コードは再びノーマライザをチェックしてからthis functionが実行されます:
public function supportsDenormalization($data, $type, $format = null)
{
return class_exists($type);
}
そして、私は次のエラーの原因となっ間違った正規化を取得する場所を正確にそれがです:
Notice: Uninitialized string offset: 0 in vendor/symfony/symfony/src/Symfony/Component/Inflector/Inflector.php at line 179
UPDATE:
私は、この他の方法及び結果は意味の前とまったく同じです試してみました同じエラーメッセージ:
$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]);
私が紛失しているもの ここに?