diff --git a/UPGRADE-3.x.md b/UPGRADE-3.x.md index 770aad9694..8e0c264c58 100644 --- a/UPGRADE-3.x.md +++ b/UPGRADE-3.x.md @@ -1,6 +1,15 @@ UPGRADE 3.x =========== +## Deprecated `Sonata\AdminBundle\Model\ModelManagerInterface` collection-related methods. + +Use: +- `new \Doctrine\Common\Collections\ArrayCollection()` instead of `getModelCollectionInstance($class)` +- `$collection->removeElement($element)` instead of `collectionRemoveElement($collection, $element)` +- `$collection->add($element)` instead of `collectionAddElement($collection, $element)` +- `$collection->contains($element)` instead of `collectionHasElement($collection, $element)` +- `$collection->clear()` instead of `collectionClear($collection)` + UPGRADE FROM 3.73 to 3.74 ========================= diff --git a/src/Form/DataTransformer/ModelToIdPropertyTransformer.php b/src/Form/DataTransformer/ModelToIdPropertyTransformer.php index 04efa610b6..0c41095e71 100644 --- a/src/Form/DataTransformer/ModelToIdPropertyTransformer.php +++ b/src/Form/DataTransformer/ModelToIdPropertyTransformer.php @@ -13,6 +13,7 @@ namespace Sonata\AdminBundle\Form\DataTransformer; +use Doctrine\Common\Collections\ArrayCollection; use Doctrine\Common\Util\ClassUtils; use Sonata\AdminBundle\Model\ModelManagerInterface; use Symfony\Component\Form\DataTransformerInterface; @@ -73,7 +74,7 @@ public function __construct( public function reverseTransform($value) { - $collection = $this->modelManager->getModelCollectionInstance($this->className); + $collection = new ArrayCollection(); if (empty($value)) { if ($this->multiple) { diff --git a/src/Form/DataTransformer/ModelsToArrayTransformer.php b/src/Form/DataTransformer/ModelsToArrayTransformer.php index c0343789c6..2768a02f4e 100644 --- a/src/Form/DataTransformer/ModelsToArrayTransformer.php +++ b/src/Form/DataTransformer/ModelsToArrayTransformer.php @@ -13,6 +13,7 @@ namespace Sonata\AdminBundle\Form\DataTransformer; +use Doctrine\Common\Collections\ArrayCollection; use Doctrine\Common\Util\ClassUtils; use Sonata\AdminBundle\Form\ChoiceList\ModelChoiceLoader; use Sonata\AdminBundle\Model\ModelManagerInterface; @@ -146,7 +147,7 @@ public function reverseTransform($keys) throw new UnexpectedTypeException($keys, 'array'); } - $collection = $this->modelManager->getModelCollectionInstance($this->class); + $collection = new ArrayCollection(); $notFound = []; // optimize this into a SELECT WHERE IN query diff --git a/src/Form/EventListener/MergeCollectionListener.php b/src/Form/EventListener/MergeCollectionListener.php index 08bb49dcd6..09aa48e65a 100644 --- a/src/Form/EventListener/MergeCollectionListener.php +++ b/src/Form/EventListener/MergeCollectionListener.php @@ -13,6 +13,7 @@ namespace Sonata\AdminBundle\Form\EventListener; +use Doctrine\Common\Collections\Collection; use Sonata\AdminBundle\Model\ModelManagerInterface; use Symfony\Component\EventDispatcher\EventSubscriberInterface; use Symfony\Component\Form\FormEvent; @@ -26,12 +27,23 @@ class MergeCollectionListener implements EventSubscriberInterface { /** - * @var ModelManagerInterface + * @var ModelManagerInterface|null */ protected $modelManager; - public function __construct(ModelManagerInterface $modelManager) + /** + * NEXT_MAJOR: Remove this constructor and the modelManager property. + */ + public function __construct(?ModelManagerInterface $modelManager = null) { + if (null !== $modelManager) { + @trigger_error(sprintf( + 'Passing argument 1 to %s() is deprecated since sonata-project/admin-bundle 3.x' + .' and will be ignored in version 4.0.', + __METHOD__ + ), E_USER_DEPRECATED); + } + $this->modelManager = $modelManager; } @@ -45,7 +57,10 @@ public static function getSubscribedEvents() public function onBind(FormEvent $event) { $collection = $event->getForm()->getData(); + \assert(null === $collection || $collection instanceof Collection); + $data = $event->getData(); + \assert($data instanceof Collection); // looks like there is no way to remove other listeners $event->stopPropagation(); @@ -53,19 +68,19 @@ public function onBind(FormEvent $event) if (!$collection) { $collection = $data; } elseif (0 === \count($data)) { - $this->modelManager->collectionClear($collection); + $collection->clear(); } else { // merge $data into $collection foreach ($collection as $model) { - if (!$this->modelManager->collectionHasElement($data, $model)) { - $this->modelManager->collectionRemoveElement($collection, $model); + if (!$data->contains($model)) { + $collection->removeElement($model); } else { - $this->modelManager->collectionRemoveElement($data, $model); + $data->removeElement($model); } } foreach ($data as $model) { - $this->modelManager->collectionAddElement($collection, $model); + $collection->add($model); } } diff --git a/src/Model/ModelManagerInterface.php b/src/Model/ModelManagerInterface.php index d53573685a..9790cfdb80 100644 --- a/src/Model/ModelManagerInterface.php +++ b/src/Model/ModelManagerInterface.php @@ -169,6 +169,10 @@ public function getUrlSafeIdentifier($model); public function getModelInstance($class); /** + * NEXT_MAJOR: Remove this method. + * + * @deprecated since sonata-project/admin-bundle 3.x. To be removed in 4.0. Use doctrine/collections instead. + * * @param string $class * * @return array|\ArrayAccess @@ -176,6 +180,10 @@ public function getModelInstance($class); public function getModelCollectionInstance($class); /** + * NEXT_MAJOR: Remove this method. + * + * @deprecated since sonata-project/admin-bundle 3.x. To be removed in 4.0. Use doctrine/collections instead. + * * Removes an element from the collection. * * @param array $collection @@ -184,6 +192,10 @@ public function getModelCollectionInstance($class); public function collectionRemoveElement(&$collection, &$element); /** + * NEXT_MAJOR: Remove this method. + * + * @deprecated since sonata-project/admin-bundle 3.x. To be removed in 4.0. Use doctrine/collections instead. + * * Add an element from the collection. * * @param array $collection @@ -192,6 +204,10 @@ public function collectionRemoveElement(&$collection, &$element); public function collectionAddElement(&$collection, &$element); /** + * NEXT_MAJOR: Remove this method. + * + * @deprecated since sonata-project/admin-bundle 3.x. To be removed in 4.0. Use doctrine/collections instead. + * * Check if the element exists in the collection. * * @param array $collection @@ -202,6 +218,10 @@ public function collectionAddElement(&$collection, &$element); public function collectionHasElement(&$collection, &$element); /** + * NEXT_MAJOR: Remove this method. + * + * @deprecated since sonata-project/admin-bundle 3.x. To be removed in 4.0. Use doctrine/collections instead. + * * Clear the collection. * * @param array $collection diff --git a/tests/App/Model/ModelManager.php b/tests/App/Model/ModelManager.php index 99ae2cd6ca..801962c5a8 100644 --- a/tests/App/Model/ModelManager.php +++ b/tests/App/Model/ModelManager.php @@ -124,23 +124,38 @@ public function getModelInstance($class) } } + /** + * NEXT_MAJOR: Remove this method. + */ public function getModelCollectionInstance($class) { return []; } + /** + * NEXT_MAJOR: Remove this method. + */ public function collectionRemoveElement(&$collection, &$element): void { } + /** + * NEXT_MAJOR: Remove this method. + */ public function collectionAddElement(&$collection, &$element): void { } + /** + * NEXT_MAJOR: Remove this method. + */ public function collectionHasElement(&$collection, &$element): void { } + /** + * NEXT_MAJOR: Remove this method. + */ public function collectionClear(&$collection): void { }