diff --git a/src/Form/ChoiceList/ModelChoiceLoader.php b/src/Form/ChoiceList/ModelChoiceLoader.php index ad61aa41896..24c6f38e3db 100644 --- a/src/Form/ChoiceList/ModelChoiceLoader.php +++ b/src/Form/ChoiceList/ModelChoiceLoader.php @@ -94,9 +94,25 @@ public function __construct( $this->modelManager = $modelManager; $this->class = $class; $this->property = $property; - $this->query = $query; $this->choices = $choices; + if ($query) { + // NEXT_MAJOR: Remove the method_exists check. + if (method_exists($this->modelManager, 'supportsQuery')) { + if (!$this->modelManager->supportsQuery($query)) { + // NEXT_MAJOR: Remove the deprecation and uncomment the exception. + @trigger_error( + 'Passing a query which is not supported by the model manager is deprecated since' + .' sonata-project/admin-bundle 3.x and will throw an exception in version 4.0.', + E_USER_DEPRECATED + ); + // throw new \InvalidArgumentException('The model manager does not support the query.'); + } + } + + $this->query = $query; + } + $this->identifier = $this->modelManager->getIdentifierFieldNames($this->class); // The property option defines, which property (path) is used for diff --git a/src/Model/ModelManagerInterface.php b/src/Model/ModelManagerInterface.php index 572ae90bb28..157b050bcc4 100644 --- a/src/Model/ModelManagerInterface.php +++ b/src/Model/ModelManagerInterface.php @@ -21,6 +21,8 @@ /** * A model manager is a bridge between the model classes and the admin functionality. + * + * @method bool supportsQuery(object $query) */ interface ModelManagerInterface extends DatagridManagerInterface { @@ -293,6 +295,9 @@ public function modelReverseTransform($class, array $array = []); */ public function modelTransform($class, $instance); + // NEXT_MAJOR: Uncomment this. +// public function supportsQuery(object $query): bool; + /** * @param object $query */ diff --git a/tests/App/Model/ModelManager.php b/tests/App/Model/ModelManager.php index 801962c5a81..45f45884939 100644 --- a/tests/App/Model/ModelManager.php +++ b/tests/App/Model/ModelManager.php @@ -185,6 +185,11 @@ public function modelTransform($class, $instance): object throw new \BadMethodCallException('Not implemented.'); } + public function supportsQuery(object $query): bool + { + return true; + } + public function executeQuery($query): void { } diff --git a/tests/Form/ChoiceList/ModelChoiceLoaderTest.php b/tests/Form/ChoiceList/ModelChoiceLoaderTest.php index 7dd62a644da..a4d74d072d2 100644 --- a/tests/Form/ChoiceList/ModelChoiceLoaderTest.php +++ b/tests/Form/ChoiceList/ModelChoiceLoaderTest.php @@ -17,14 +17,17 @@ use Sonata\AdminBundle\Form\ChoiceList\ModelChoiceLoader; use Sonata\AdminBundle\Model\ModelManagerInterface; use Sonata\AdminBundle\Tests\Fixtures\Bundle\Entity\Foo; +use Symfony\Bridge\PhpUnit\ExpectDeprecationTrait; class ModelChoiceLoaderTest extends TestCase { + use ExpectDeprecationTrait; + private $modelManager; protected function setUp(): void { - $this->modelManager = $this->getMockForAbstractClass(ModelManagerInterface::class); + $this->modelManager = $this->createMock(ModelManagerInterface::class); } public function testLoadFromEntityWithSamePropertyValues(): void