diff --git a/src/Bundle/ChillMainBundle/Entity/UserGroup.php b/src/Bundle/ChillMainBundle/Entity/UserGroup.php index b42609693..b39216b11 100644 --- a/src/Bundle/ChillMainBundle/Entity/UserGroup.php +++ b/src/Bundle/ChillMainBundle/Entity/UserGroup.php @@ -18,9 +18,12 @@ use Doctrine\Common\Collections\Order; use Doctrine\Common\Collections\ReadableCollection; use Doctrine\Common\Collections\Selectable; use Doctrine\ORM\Mapping as ORM; +use Symfony\Component\Serializer\Annotation\DiscriminatorMap; #[ORM\Entity] #[ORM\Table(name: 'chill_main_user_group')] +// this discriminator key is required for automated denormalization +#[DiscriminatorMap('type', mapping: ['user_group' => UserGroup::class])] class UserGroup { #[ORM\Id] @@ -65,7 +68,7 @@ class UserGroup public function __construct() { - $this->adminUsers = new \Doctrine\Common\Collections\ArrayCollection(); + $this->adminUsers = new ArrayCollection(); $this->users = new ArrayCollection(); } diff --git a/src/Bundle/ChillMainBundle/Form/Type/DataTransformer/EntityToJsonTransformer.php b/src/Bundle/ChillMainBundle/Form/Type/DataTransformer/EntityToJsonTransformer.php index 737c4683f..c6839efa8 100644 --- a/src/Bundle/ChillMainBundle/Form/Type/DataTransformer/EntityToJsonTransformer.php +++ b/src/Bundle/ChillMainBundle/Form/Type/DataTransformer/EntityToJsonTransformer.php @@ -13,6 +13,7 @@ namespace Chill\MainBundle\Form\Type\DataTransformer; use Chill\MainBundle\Entity\User; use Chill\MainBundle\Entity\UserGroup; +use Chill\MainBundle\Serializer\Normalizer\DiscriminatedObjectDenormalizer; use Chill\PersonBundle\Entity\Person; use Chill\ThirdPartyBundle\Entity\ThirdParty; use Symfony\Component\Form\DataTransformerInterface; @@ -76,15 +77,22 @@ class EntityToJsonTransformer implements DataTransformerInterface 'person' => Person::class, 'thirdparty' => ThirdParty::class, 'user_group' => UserGroup::class, + 'user_group_or_user' => DiscriminatedObjectDenormalizer::TYPE, default => throw new \UnexpectedValueException('This type is not supported'), }; + $context = [AbstractNormalizer::GROUPS => ['read']]; + + if ('user_group_or_user' === $this->type) { + $context[DiscriminatedObjectDenormalizer::ALLOWED_TYPES] = [UserGroup::class, User::class]; + } + return $this->denormalizer->denormalize( ['type' => $item['type'], 'id' => $item['id']], $class, 'json', - [AbstractNormalizer::GROUPS => ['read']], + $context, ); } } diff --git a/src/Bundle/ChillMainBundle/Form/Type/PickUserGroupOrUserDynamicType.php b/src/Bundle/ChillMainBundle/Form/Type/PickUserGroupOrUserDynamicType.php index 00d783a97..c4e91cce4 100644 --- a/src/Bundle/ChillMainBundle/Form/Type/PickUserGroupOrUserDynamicType.php +++ b/src/Bundle/ChillMainBundle/Form/Type/PickUserGroupOrUserDynamicType.php @@ -30,7 +30,7 @@ final class PickUserGroupOrUserDynamicType extends AbstractType public function buildForm(FormBuilderInterface $builder, array $options) { - $builder->addViewTransformer(new EntityToJsonTransformer($this->denormalizer, $this->serializer, $options['multiple'], 'user_group')); + $builder->addViewTransformer(new EntityToJsonTransformer($this->denormalizer, $this->serializer, $options['multiple'], 'user_group_or_user')); } public function buildView(FormView $view, FormInterface $form, array $options)