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")
* @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
{
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
{
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;

View File

@ -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