Skip to content

Commit

Permalink
Leverage ModelManagerInterface::findBy()
Browse files Browse the repository at this point in the history
  • Loading branch information
phansys committed Sep 27, 2020
1 parent 1f35ad5 commit d02a65a
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 29 deletions.
12 changes: 7 additions & 5 deletions src/Form/DataTransformer/ModelToIdPropertyTransformer.php
Original file line number Diff line number Diff line change
Expand Up @@ -108,12 +108,14 @@ public function reverseTransform($value)
throw new \UnexpectedValueException(sprintf('Value should be array, %s given.', \gettype($value)));
}

foreach ($value as $key => $id) {
if ('_labels' === $key) {
continue;
}
$ids = array_filter($value, static function ($key): bool {
return '_labels' !== $key;
}, ARRAY_FILTER_USE_KEY);

$singleIdentifierFieldName = $this->modelManager->getIdentifierFieldNames($this->className)[0];

$collection->add($this->modelManager->find($this->className, $id));
foreach ($this->modelManager->findBy($this->className, [$singleIdentifierFieldName => $ids]) as $model) {
$collection->add($model);
}

return $collection;
Expand Down
25 changes: 11 additions & 14 deletions src/Form/DataTransformer/ModelsToArrayTransformer.php
Original file line number Diff line number Diff line change
Expand Up @@ -169,25 +169,22 @@ public function reverseTransform($value)
}

/** @phpstan-var ArrayCollection<array-key, T> $collection */
$collection = new ArrayCollection();
$notFound = [];

// optimize this into a SELECT WHERE IN query
foreach ($value as $key) {
if ($model = $this->modelManager->find($this->class, $key)) {
$collection->add($model);
} else {
$notFound[] = $key;
}
}
$collection = $this->modelManager->getModelCollectionInstance($this->class);
$singleIdentifierFieldName = $this->modelManager->getIdentifierFieldNames($this->class)[0];
$result = $this->modelManager->findBy($this->class, [$singleIdentifierFieldName => $value]);

$diffCount = \count($value) - \count($result);

if (\count($notFound) > 0) {
if (0 !== $diffCount) {
throw new TransformationFailedException(sprintf(
'The entities with keys "%s" could not be found',
implode('", "', $notFound)
'%u keys could not be found in the provided values: "%s"',
$diffCount,
implode('", "', $value)
));
}

return [];

return $collection;
}

Expand Down
30 changes: 20 additions & 10 deletions tests/Form/DataTransformer/ModelToIdPropertyTransformerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,23 +61,33 @@ public function testReverseTransformMultiple(array $expected, $params, Foo $enti
$transformer = new ModelToIdPropertyTransformer($this->modelManager, Foo::class, 'bar', true);

$this->modelManager
->method('find')
->willReturnCallback(static function (string $className, int $value) use ($entity1, $entity2, $entity3) {
if (Foo::class !== $className) {
return;
->expects($this->exactly($params ? 1 : 0))
->method('getIdentifierFieldNames')
->willReturn(['id']);

$this->modelManager
->expects($this->exactly($params ? 1 : 0))
->method('findBy')
->willReturnCallback(static function (string $className, array $criteria) use ($entity1, $entity2, $entity3): array {
$collection = [];

if (Foo::class !== $className || !isset($criteria['id'])) {
return $collection;
}

if (123 === $value) {
return $entity1;
if (\in_array(123, $criteria['id'], true)) {
$collection[] = $entity1;
}

if (456 === $value) {
return $entity2;
if (\in_array(456, $criteria['id'], true)) {
$collection[] = $entity2;
}

if (789 === $value) {
return $entity3;
if (\in_array(789, $criteria['id'], true)) {
$collection[] = $entity3;
}

return $collection;
});

$collection = new ArrayCollection();
Expand Down

0 comments on commit d02a65a

Please sign in to comment.