Код: Выделить всё
class TariffDto
{
#[Context([EntityNormalizer::UNIQUE_ENTITY_FIELD => 'code'])]
public Product $product;
}
Код: Выделить всё
class EntityNormalizer implements DenormalizerInterface
{
public const UNIQUE_ENTITY_FIELD = 'unique_entity_field';
private EntityManager $em;
public function __construct()
{
$this->em = Registry::getInstance()->EM;
}
public function supportsDenormalization(mixed $data, string $type, ?string $format = null, array $context = []): bool
{
return str_starts_with($type, 'App\\Entity\\') && (is_numeric($data) || is_string($data));
}
public function denormalize(mixed $data, string $type, ?string $format = null, array $context = [])
{
dump($context);
return $this->em->getRepository($type)->find($data);
}
}
Код: Выделить всё
^ array:3 [
"_read_attributes" => false
"cache_key" => "45e90c83367464cde5c29c1ac70e95f3-product"
"deserialization_path" => "product"
]
Код: Выделить всё
$phpDocExtractor = new PropertyInfo\Extractor\PhpDocExtractor();
$typeExtractor = new PropertyInfo\PropertyInfoExtractor(
typeExtractors: [$phpDocExtractor, new ReflectionExtractor()]
);
$serializer = new Serializer\Serializer(
normalizers: [
new BackedEnumNormalizer(),
new EntityNormalizer(), // my custom normalizer
new Serializer\Normalizer\ObjectNormalizer(
nameConverter: new Serializer\NameConverter\CamelCaseToSnakeCaseNameConverter(),
propertyTypeExtractor: $typeExtractor
),
new Serializer\Normalizer\ArrayDenormalizer(),
],
);
Я пробовал передавать разные контекстные ключи, в т.ч. существующий (
Код: Выделить всё
#[Context([DateTimeNormalizer::FORMAT_KEY => 'Y-m-d'])]
Я нашел эту статью, и здесь она описана именно так, как я хочу, и я думаю этому парню это сработало.
Итак, где мой контекст?
Подробнее здесь: https://stackoverflow.com/questions/790 ... serializer