0

There is A LOT of similar quesions, some of them have validated answers, but here I am and none of them worked.

My use case is pretty simple:

  • My users App\Client\common\Entities\User belong to a customer App\Client\common\Entities\Customer.

  • App\Client\common\Entities\User also inherits FOS\UserBundle\Model\User which contains the holy property "email"

I want to serialize all my customers AND their users (including their mail). Jms works pretty well except i can not access properties from the FOS\UserBundle\Model\User class.

following this answer here is what I have now.

jms_serializer.yml

jms_serializer: #blablablaa.... metadata: auto_detection: true directories: App: namespace_prefix: 'App\Client' path: '%kernel.project_dir%/serializer' FOSUB: namespace_prefix: 'FOS\UserBundle' path: '%kernel.project_dir%/serializer' 

serializer/App.Client.common.Entities.User.yml :

App\Client\common\Entities\User: exclusion_policy: ALL properties: surname: expose: true exclude: false groups: [export] 

serializer/Model.User.yml:

FOS\UserBundle\Model\User: exclusion_policy: ALL properties: email: expose: true exclude: false groups: [export] 

src/Command/DeploySyncUsersCommand.php:

protected function execute(InputInterface $input, OutputInterface $output) { $users = $this->customerRepository->findAll(); //this is an array of Customer $context = new SerializationContext(); $context->setGroups(['export']); $serializer = SerializerBuilder::create()->build(); $json = $serializer->serialize($users, 'json', $context); // do something with json } 

Everything works fine except the json does NOT contain email or any FOSUser\User data.

Also something interesting is that I can write anything (even invalid yml) in the App.Client.common.Entities.User.yml and Model.User.yml files, I'm able to clear the cache with no errors. I have errors when I write invalid yml in jms_serializer.yml

1 Answer 1

0

Ok I've been able to solve this using dependency injection instead of building the serializer

/** * @var SerializerInterface */ private SerializerInterface $serializer; /** * @var string */ private string $reportAnalysisUrl; public function __construct(SerializerInterface $serialzer, $reportAnalysis) { $this->serializer = $serialzer; parent::__construct(); } protected function execute(InputInterface $input, OutputInterface $output) { $users = $this->customerRepository->findAll(); $context = new SerializationContext(); $context->setGroups(['export']); $json = $this->serializer->serialize($users, 'json', $context); //do something with json } 

so now my yml files aren't ignored anymore

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.