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

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

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