diff --git a/src/Bundle/ChillActivityBundle/Entity/Activity.php b/src/Bundle/ChillActivityBundle/Entity/Activity.php index 733a6a1bc..e76bf338a 100644 --- a/src/Bundle/ChillActivityBundle/Entity/Activity.php +++ b/src/Bundle/ChillActivityBundle/Entity/Activity.php @@ -130,13 +130,13 @@ class Activity implements HasCenterInterface, HasScopeInterface * @ORM\ManyToMany(targetEntity="Chill\PersonBundle\Entity\Person") * @Groups({"read"}) */ - private Collection $persons; + private ?Collection $persons = null; /** * @ORM\ManyToMany(targetEntity="Chill\ThirdPartyBundle\Entity\ThirdParty") * @Groups({"read"}) */ - private Collection $thirdParties; + private ?Collection $thirdParties = null; /** * @ORM\ManyToMany(targetEntity="Chill\DocStoreBundle\Entity\StoredObject") @@ -147,7 +147,7 @@ class Activity implements HasCenterInterface, HasScopeInterface * @ORM\ManyToMany(targetEntity="Chill\MainBundle\Entity\User") * @Groups({"read"}) */ - private Collection $users; + private ?Collection $users = null; /** * @ORM\Column(type="boolean", options={"default"=false}) @@ -351,17 +351,18 @@ class Activity implements HasCenterInterface, HasScopeInterface return $this->persons; } - public function setPersons(Collection $persons): self + public function setPersons(?Collection $persons): self { $this->persons = $persons; return $this; } - public function addThirdParty(ThirdParty $thirdParty): self + public function addThirdParty(?ThirdParty $thirdParty): self { - $this->thirdParties[] = $thirdParty; - + if (null !== $thirdParty) { + $this->thirdParties[] = $thirdParty; + } return $this; } @@ -375,7 +376,7 @@ class Activity implements HasCenterInterface, HasScopeInterface return $this->thirdParties; } - public function setThirdParties(Collection $thirdParties): self + public function setThirdParties(?Collection $thirdParties): self { $this->thirdParties = $thirdParties; @@ -406,10 +407,11 @@ class Activity implements HasCenterInterface, HasScopeInterface return $this; } - public function addUser(User $user): self + public function addUser(?User $user): self { - $this->users[] = $user; - + if (null !== $user) { + $this->users[] = $user; + } return $this; } @@ -423,7 +425,7 @@ class Activity implements HasCenterInterface, HasScopeInterface return $this->users; } - public function setUsers(Collection $users): self + public function setUsers(?Collection $users): self { $this->users = $users; diff --git a/src/Bundle/ChillActivityBundle/Form/ActivityType.php b/src/Bundle/ChillActivityBundle/Form/ActivityType.php index a04a4a0a9..b8616eb53 100644 --- a/src/Bundle/ChillActivityBundle/Form/ActivityType.php +++ b/src/Bundle/ChillActivityBundle/Form/ActivityType.php @@ -164,40 +164,46 @@ class ActivityType extends AbstractType } if ($activityType->isVisible('persons')) { - // TODO Faire évoluer le selecteur et la query <= - $builder->add('persons', HiddenType::class); + $builder->add('persons', HiddenType::class, [ + //'data_class' => Person::class, + ]); $builder->get('persons') - //->addModelTransformer(new PersonToIdTransformer($this->om)); - ->addModelTransformer(new CallbackTransformer( - function ($personsAsArray) { - dump('personsAsArray', $personsAsArray); //Doctrine\Common\Collections\ArrayCollection - // transform the array to a string - //return implode(',', $personsAsArray); - return $personsAsArray; + function (iterable $personsAsIterable): string { + $personIds = []; + foreach ($personsAsIterable as $value) { + $personIds[] = $value->getId(); + } + return implode(',', $personIds); }, - function ($personsAsString) { - dump('personsAsString', $personsAsString); //"1188,1259,1229,1223" - // transform the string back to an array - return explode(',', $personsAsString); + function (?string $personsAsString): array { + return array_map( + fn(string $id): ?Person => $this->om->getRepository(Person::class)->findOneBy(['id' => (int) $id]), + explode(',', $personsAsString) + ); } )) - ; + ; } if ($activityType->isVisible('thirdParties')) { - // TODO Faire évoluer le selecteur et la query <= - $builder->add('thirdParties', HiddenType::class, []); + $builder->add('thirdParties', HiddenType::class, [ + //'data_class' => ThirdParty::class, + ]); $builder->get('thirdParties') ->addModelTransformer(new CallbackTransformer( - function ($personsAsArray) { - // transform the array to a string - //return implode(',', $personsAsArray); - return $personsAsArray; + function (iterable $thirdpartyAsIterable): string { + $thirdpartyIds = []; + foreach ($thirdpartyAsIterable as $value) { + $thirdpartyIds[] = $value->getId(); + } + return implode(',', $thirdpartyIds); }, - function ($personsAsString) { - // transform the string back to an array - return explode(',', $personsAsString); + function (?string $thirdpartyAsString): array { + return array_map( + fn(string $id): ?ThirdParty => $this->om->getRepository(ThirdParty::class)->findOneBy(['id' => (int) $id]), + explode(',', $thirdpartyAsString) + ); } )) ; @@ -213,18 +219,23 @@ class ActivityType extends AbstractType } if ($activityType->isVisible('users')) { - // TODO Faire évoluer le selecteur et la query <= - $builder->add('users', HiddenType::class, []); + $builder->add('users', HiddenType::class, [ + //'data_class' => User::class, + ]); $builder->get('users') ->addModelTransformer(new CallbackTransformer( - function ($personsAsArray) { - // transform the array to a string - //return implode(',', $personsAsArray); - return $personsAsArray; + function (iterable $usersAsIterable): string { + $userIds = []; + foreach ($usersAsIterable as $value) { + $userIds[] = $value->getId(); + } + return implode(',', $userIds); }, - function ($personsAsString) { - // transform the string back to an array - return explode(',', $personsAsString); + function (?string $usersAsString): array { + return array_map( + fn(string $id): ?User => $this->om->getRepository(User::class)->findOneBy(['id' => (int) $id]), + explode(',', $usersAsString) + ); } )) ; @@ -298,7 +309,7 @@ class ActivityType extends AbstractType public function configureOptions(OptionsResolver $resolver): void { $resolver->setDefaults([ - 'data_class' => 'Chill\ActivityBundle\Entity\Activity' + 'data_class' => Activity::class ]); $resolver