Fix Create a PickUserGroupOrUserDynamicType

This commit is contained in:
Julien Fastré 2024-10-01 17:16:49 +02:00
parent d8ad8c3605
commit c4c7280b52
Signed by: julienfastre
GPG Key ID: BDE2190974723FCB
3 changed files with 14 additions and 3 deletions

View File

@ -18,9 +18,12 @@ use Doctrine\Common\Collections\Order;
use Doctrine\Common\Collections\ReadableCollection; use Doctrine\Common\Collections\ReadableCollection;
use Doctrine\Common\Collections\Selectable; use Doctrine\Common\Collections\Selectable;
use Doctrine\ORM\Mapping as ORM; use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Serializer\Annotation\DiscriminatorMap;
#[ORM\Entity] #[ORM\Entity]
#[ORM\Table(name: 'chill_main_user_group')] #[ORM\Table(name: 'chill_main_user_group')]
// this discriminator key is required for automated denormalization
#[DiscriminatorMap('type', mapping: ['user_group' => UserGroup::class])]
class UserGroup class UserGroup
{ {
#[ORM\Id] #[ORM\Id]
@ -65,7 +68,7 @@ class UserGroup
public function __construct() public function __construct()
{ {
$this->adminUsers = new \Doctrine\Common\Collections\ArrayCollection(); $this->adminUsers = new ArrayCollection();
$this->users = new ArrayCollection(); $this->users = new ArrayCollection();
} }

View File

@ -13,6 +13,7 @@ namespace Chill\MainBundle\Form\Type\DataTransformer;
use Chill\MainBundle\Entity\User; use Chill\MainBundle\Entity\User;
use Chill\MainBundle\Entity\UserGroup; use Chill\MainBundle\Entity\UserGroup;
use Chill\MainBundle\Serializer\Normalizer\DiscriminatedObjectDenormalizer;
use Chill\PersonBundle\Entity\Person; use Chill\PersonBundle\Entity\Person;
use Chill\ThirdPartyBundle\Entity\ThirdParty; use Chill\ThirdPartyBundle\Entity\ThirdParty;
use Symfony\Component\Form\DataTransformerInterface; use Symfony\Component\Form\DataTransformerInterface;
@ -76,15 +77,22 @@ class EntityToJsonTransformer implements DataTransformerInterface
'person' => Person::class, 'person' => Person::class,
'thirdparty' => ThirdParty::class, 'thirdparty' => ThirdParty::class,
'user_group' => UserGroup::class, 'user_group' => UserGroup::class,
'user_group_or_user' => DiscriminatedObjectDenormalizer::TYPE,
default => throw new \UnexpectedValueException('This type is not supported'), 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 return
$this->denormalizer->denormalize( $this->denormalizer->denormalize(
['type' => $item['type'], 'id' => $item['id']], ['type' => $item['type'], 'id' => $item['id']],
$class, $class,
'json', 'json',
[AbstractNormalizer::GROUPS => ['read']], $context,
); );
} }
} }

View File

@ -30,7 +30,7 @@ final class PickUserGroupOrUserDynamicType extends AbstractType
public function buildForm(FormBuilderInterface $builder, array $options) 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) public function buildView(FormView $view, FormInterface $form, array $options)