resolve data Transformer in ActivityType (persons/thirdparties/users)

This commit is contained in:
Mathieu Jaumotte 2021-06-01 13:49:50 +02:00
parent e95d756e71
commit 9ab032c943
2 changed files with 58 additions and 45 deletions

View File

@ -130,13 +130,13 @@ class Activity implements HasCenterInterface, HasScopeInterface
* @ORM\ManyToMany(targetEntity="Chill\PersonBundle\Entity\Person") * @ORM\ManyToMany(targetEntity="Chill\PersonBundle\Entity\Person")
* @Groups({"read"}) * @Groups({"read"})
*/ */
private Collection $persons; private ?Collection $persons = null;
/** /**
* @ORM\ManyToMany(targetEntity="Chill\ThirdPartyBundle\Entity\ThirdParty") * @ORM\ManyToMany(targetEntity="Chill\ThirdPartyBundle\Entity\ThirdParty")
* @Groups({"read"}) * @Groups({"read"})
*/ */
private Collection $thirdParties; private ?Collection $thirdParties = null;
/** /**
* @ORM\ManyToMany(targetEntity="Chill\DocStoreBundle\Entity\StoredObject") * @ORM\ManyToMany(targetEntity="Chill\DocStoreBundle\Entity\StoredObject")
@ -147,7 +147,7 @@ class Activity implements HasCenterInterface, HasScopeInterface
* @ORM\ManyToMany(targetEntity="Chill\MainBundle\Entity\User") * @ORM\ManyToMany(targetEntity="Chill\MainBundle\Entity\User")
* @Groups({"read"}) * @Groups({"read"})
*/ */
private Collection $users; private ?Collection $users = null;
/** /**
* @ORM\Column(type="boolean", options={"default"=false}) * @ORM\Column(type="boolean", options={"default"=false})
@ -351,17 +351,18 @@ class Activity implements HasCenterInterface, HasScopeInterface
return $this->persons; return $this->persons;
} }
public function setPersons(Collection $persons): self public function setPersons(?Collection $persons): self
{ {
$this->persons = $persons; $this->persons = $persons;
return $this; return $this;
} }
public function addThirdParty(ThirdParty $thirdParty): self public function addThirdParty(?ThirdParty $thirdParty): self
{ {
if (null !== $thirdParty) {
$this->thirdParties[] = $thirdParty; $this->thirdParties[] = $thirdParty;
}
return $this; return $this;
} }
@ -375,7 +376,7 @@ class Activity implements HasCenterInterface, HasScopeInterface
return $this->thirdParties; return $this->thirdParties;
} }
public function setThirdParties(Collection $thirdParties): self public function setThirdParties(?Collection $thirdParties): self
{ {
$this->thirdParties = $thirdParties; $this->thirdParties = $thirdParties;
@ -406,10 +407,11 @@ class Activity implements HasCenterInterface, HasScopeInterface
return $this; return $this;
} }
public function addUser(User $user): self public function addUser(?User $user): self
{ {
if (null !== $user) {
$this->users[] = $user; $this->users[] = $user;
}
return $this; return $this;
} }
@ -423,7 +425,7 @@ class Activity implements HasCenterInterface, HasScopeInterface
return $this->users; return $this->users;
} }
public function setUsers(Collection $users): self public function setUsers(?Collection $users): self
{ {
$this->users = $users; $this->users = $users;

View File

@ -164,40 +164,46 @@ class ActivityType extends AbstractType
} }
if ($activityType->isVisible('persons')) { 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') $builder->get('persons')
//->addModelTransformer(new PersonToIdTransformer($this->om));
->addModelTransformer(new CallbackTransformer( ->addModelTransformer(new CallbackTransformer(
function ($personsAsArray) { function (iterable $personsAsIterable): string {
dump('personsAsArray', $personsAsArray); //Doctrine\Common\Collections\ArrayCollection $personIds = [];
// transform the array to a string foreach ($personsAsIterable as $value) {
//return implode(',', $personsAsArray); $personIds[] = $value->getId();
return $personsAsArray; }
return implode(',', $personIds);
}, },
function ($personsAsString) { function (?string $personsAsString): array {
dump('personsAsString', $personsAsString); //"1188,1259,1229,1223" return array_map(
// transform the string back to an array fn(string $id): ?Person => $this->om->getRepository(Person::class)->findOneBy(['id' => (int) $id]),
return explode(',', $personsAsString); explode(',', $personsAsString)
);
} }
)) ))
; ;
} }
if ($activityType->isVisible('thirdParties')) { 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') $builder->get('thirdParties')
->addModelTransformer(new CallbackTransformer( ->addModelTransformer(new CallbackTransformer(
function ($personsAsArray) { function (iterable $thirdpartyAsIterable): string {
// transform the array to a string $thirdpartyIds = [];
//return implode(',', $personsAsArray); foreach ($thirdpartyAsIterable as $value) {
return $personsAsArray; $thirdpartyIds[] = $value->getId();
}
return implode(',', $thirdpartyIds);
}, },
function ($personsAsString) { function (?string $thirdpartyAsString): array {
// transform the string back to an array return array_map(
return explode(',', $personsAsString); 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')) { 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') $builder->get('users')
->addModelTransformer(new CallbackTransformer( ->addModelTransformer(new CallbackTransformer(
function ($personsAsArray) { function (iterable $usersAsIterable): string {
// transform the array to a string $userIds = [];
//return implode(',', $personsAsArray); foreach ($usersAsIterable as $value) {
return $personsAsArray; $userIds[] = $value->getId();
}
return implode(',', $userIds);
}, },
function ($personsAsString) { function (?string $usersAsString): array {
// transform the string back to an array return array_map(
return explode(',', $personsAsString); 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 public function configureOptions(OptionsResolver $resolver): void
{ {
$resolver->setDefaults([ $resolver->setDefaults([
'data_class' => 'Chill\ActivityBundle\Entity\Activity' 'data_class' => Activity::class
]); ]);
$resolver $resolver