1

I have a php class with one property that uses DateTimeInterface. the other properties are string, int, etc.

php class

#[ORM\Column(type: Types::DATE_MUTABLE)]
private ?\DateTimeInterface $eff_date = null;

In my Controller I process some CSV data to populate the class (header row followed by multiple data rows). I setup the serializer like so.

   $encoders = [new CsvEncoder()];
    $normalizers = [new ObjectNormalizer(), new DateTimeNormalizer()];
    $serializer = new Serializer($normalizers, $encoders);

Two step process works mostly (single deserialize step does not work)

  1. decode step
  2. denormalize step
    $output = $serializer->decode($data,'csv');
    dump($output);
    
    $an_object_array = [];
    
    foreach($output as $item) { //foreach element in $arr
            $an_object = $serializer->denormalize($item, Rmk::class);
            $an_object_array[] = $an_object;
    }
    
    dd($an_object_array);

If I set the property as a string the serialization works fine.

if not I get the following error.

Failed to denormalize attribute "EFF_DATE" value for class "App\Entity\Rmk": Expected argument of type "?DateTimeInterface", "string" given at property path "EFF_DATE".

1
  • Also tried : $normalizers = array( new DateTimeNormalizer([ DateTimeNormalizer::FORMAT_KEY => "d/m/Y", ]), new ObjectNormalizer( null, null, null, new ReflectionExtractor() ), new GetSetMethodNormalizer(), new ArrayDenormalizer(), ); Commented Nov 29, 2022 at 12:45

1 Answer 1

1

found the solution.

  1. changed the setter method in the entity to accept string as input and create a date time object from the string.

  2. corrected normalizers

    $normalizers = array(
        new ObjectNormalizer(),
        new ArrayDenormalizer(),
    );
  1. switched to single deserialize step also.
    $remarks = $serializer->deserialize($data, Rmk::class.'[]','csv');
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.