Skip to content

Commit

Permalink
Add ModelManagerInterface::findMultiple()
Browse files Browse the repository at this point in the history
  • Loading branch information
phansys committed Sep 28, 2020
1 parent f57b683 commit 0b00e04
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 15 deletions.
20 changes: 11 additions & 9 deletions src/Form/DataTransformer/ModelToIdPropertyTransformer.php
Expand Up @@ -89,12 +89,9 @@ public function __construct(
*/
public function reverseTransform($value)
{
/** @phpstan-var ArrayCollection<array-key, T> $collection */
$collection = new ArrayCollection();

if (empty($value)) {
if ($this->multiple) {
return $collection;
return new ArrayCollection();
}

return null;
Expand All @@ -112,12 +109,17 @@ public function reverseTransform($value)
return '_labels' !== $key;
}, ARRAY_FILTER_USE_KEY);

$query = $this->modelManager->createQuery($this->className);
$this->modelManager->addIdentifiersToQuery($this->className, $query, $ids);
$result = $this->modelManager->executeQuery($query);
// NEXT_MAJOR: Remove this check and the `else` condition.
if (method_exists($this->modelManager, 'findMultiple')) {
/** @phpstan-var ArrayCollection<array-key, T> $collection */
$collection = $this->modelManager->findMultiple($ids);
} else {
$query = $this->modelManager->createQuery($this->className);
$this->modelManager->addIdentifiersToQuery($this->className, $query, $ids);
$result = $this->modelManager->executeQuery($query);

foreach ($result as $model) {
$collection->add($model);
/** @phpstan-var ArrayCollection<array-key, T> $collection */
$collection = new ArrayCollection($result);
}

return $collection;
Expand Down
19 changes: 14 additions & 5 deletions src/Form/DataTransformer/ModelsToArrayTransformer.php
Expand Up @@ -168,11 +168,20 @@ public function reverseTransform($value)
throw new UnexpectedTypeException($value, 'array');
}

$query = $this->modelManager->createQuery($this->class);
$this->modelManager->addIdentifiersToQuery($this->class, $query, $value);
$result = $this->modelManager->executeQuery($query);
// NEXT_MAJOR: Remove this check and the `else` condition.
if (method_exists($this->modelManager, 'findMultiple')) {
/** @phpstan-var ArrayCollection<array-key, T> $collection */
$collection = $this->modelManager->findMultiple($value);
} else {
$query = $this->modelManager->createQuery($this->class);
$this->modelManager->addIdentifiersToQuery($this->class, $query, $value);
$result = $this->modelManager->executeQuery($query);

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

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

if (0 !== $diffCount) {
throw new TransformationFailedException(sprintf(
Expand All @@ -182,7 +191,7 @@ public function reverseTransform($value)
));
}

return new ArrayCollection($result);
return $collection;
}

/**
Expand Down
14 changes: 13 additions & 1 deletion src/Model/ModelManagerInterface.php
Expand Up @@ -13,6 +13,7 @@

namespace Sonata\AdminBundle\Model;

use Doctrine\Common\Collections\Collection;
use Sonata\AdminBundle\Admin\FieldDescriptionInterface;
use Sonata\AdminBundle\Datagrid\DatagridInterface;
use Sonata\AdminBundle\Datagrid\ProxyQueryInterface;
Expand All @@ -22,7 +23,8 @@
/**
* A model manager is a bridge between the model classes and the admin functionality.
*
* @method bool supportsQuery(object $query)
* @method bool supportsQuery(object $query)
* @method Collection findMultiple(string $class, array $ids)
*/
interface ModelManagerInterface extends DatagridManagerInterface
{
Expand Down Expand Up @@ -69,6 +71,16 @@ public function delete($object);
*/
public function findBy($class, array $criteria = []);

// NEXT_MAJOR: Uncomment this method.
// /**
// * @return object[]&Collection all objects matching the given identifiers.
// *
// * @phpstan-template T of object
// * @phpstan-param class-string<T> $class
// * @phpstan-return T[]
// */
// public function findMultiple(string $class, array $ids): Collection;

/**
* @param string $class
* @param array<string, mixed> $criteria
Expand Down

0 comments on commit 0b00e04

Please sign in to comment.