From 0b00e045181385cf0fb02ec0dd5f69eb3e911f23 Mon Sep 17 00:00:00 2001 From: Javier Spagnoletti Date: Mon, 28 Sep 2020 18:25:17 -0300 Subject: [PATCH] Add `ModelManagerInterface::findMultiple()` --- .../ModelToIdPropertyTransformer.php | 20 ++++++++++--------- .../ModelsToArrayTransformer.php | 19 +++++++++++++----- src/Model/ModelManagerInterface.php | 14 ++++++++++++- 3 files changed, 38 insertions(+), 15 deletions(-) diff --git a/src/Form/DataTransformer/ModelToIdPropertyTransformer.php b/src/Form/DataTransformer/ModelToIdPropertyTransformer.php index 6d7e841cb0c..8a003a32597 100644 --- a/src/Form/DataTransformer/ModelToIdPropertyTransformer.php +++ b/src/Form/DataTransformer/ModelToIdPropertyTransformer.php @@ -89,12 +89,9 @@ public function __construct( */ public function reverseTransform($value) { - /** @phpstan-var ArrayCollection $collection */ - $collection = new ArrayCollection(); - if (empty($value)) { if ($this->multiple) { - return $collection; + return new ArrayCollection(); } return null; @@ -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 $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 $collection */ + $collection = new ArrayCollection($result); } return $collection; diff --git a/src/Form/DataTransformer/ModelsToArrayTransformer.php b/src/Form/DataTransformer/ModelsToArrayTransformer.php index cb574bc6e99..16f9ce20206 100644 --- a/src/Form/DataTransformer/ModelsToArrayTransformer.php +++ b/src/Form/DataTransformer/ModelsToArrayTransformer.php @@ -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 $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 $collection */ + $collection = new ArrayCollection($result); + } - $diffCount = \count($value) - \count($result); + $diffCount = \count($value) - $collection->count(); if (0 !== $diffCount) { throw new TransformationFailedException(sprintf( @@ -182,7 +191,7 @@ public function reverseTransform($value) )); } - return new ArrayCollection($result); + return $collection; } /** diff --git a/src/Model/ModelManagerInterface.php b/src/Model/ModelManagerInterface.php index 157b050bcc4..67440e0b88c 100644 --- a/src/Model/ModelManagerInterface.php +++ b/src/Model/ModelManagerInterface.php @@ -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; @@ -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 { @@ -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 $class + // * @phpstan-return T[] + // */ + // public function findMultiple(string $class, array $ids): Collection; + /** * @param string $class * @param array $criteria