понедельник, 29 ноября 2021 г.

[JMS serializer] how to set naming strategy

     public function serialize($object, array $serializationGroups)

    {

        $serializer = SerializerBuilder::create();

        $serializer->setPropertyNamingStrategy(new SerializedNameAnnotationStrategy(new IdenticalPropertyNamingStrategy()));

        $serializer->addMetadataDir($this->configDir . "/serializer", 'App\Entity');

        $serializer->setCacheDir($this->cacheDir . "/jms_serializer");

        $serializer = $serializer->build();


        $group = new GroupsExclusionStrategy($serializationGroups);

        $context = SerializationContext::create();

        $context->addExclusionStrategy($group);


        return $serializer->serialize($object, 'json', $context);

    }

It will make JSON string where property field is camelCased instead of Snake_cased. I highly recommend to use 'App\Entity' in addMetadataDir() because by default in Symfony jms_serializer.yaml config is set 'namespace_prefix' in metadata node, and without 'App\Entity' argument YML/XML entity configs won't work. It is also example how to customize Serializer parameters manually with SerializerBuilder without framework config.

вторник, 9 ноября 2021 г.

[Symfony] Cannot read index 'email' from object of type App\Entity\User because it doesn't implement \ArrayAccess

 Its because PropertyAccessor thinks form is an array. To change it, set form data_class. For example in your form Type:

public function configureOptions(OptionsResolver $resolver)

    {

        $resolver

            ->setDefaults([

                'data_class' => User::class,

...