Sonataadminbundle: ModelType class can't be used with the new Symfony way

Created on 28 Jun 2016  路  5Comments  路  Source: sonata-project/SonataAdminBundle

Environment

Sonata packages

$ composer show sonata-project/*
sonata-project/admin-bundle              3.3.2  The missing Symfony Admin Generator
sonata-project/block-bundle              3.0.1  Symfony SonataBlockBundle
sonata-project/cache                     1.0.7  Cache library
sonata-project/core-bundle               3.0.3  Symfony SonataCoreBundle
sonata-project/doctrine-extensions       1.0.2  Doctrine2 behavioral extensions
sonata-project/doctrine-orm-admin-bundle 3.0.4  Symfony Sonata / Integrate Doctrine ORM into the SonataAdminBundle
sonata-project/easy-extends-bundle       2.1.10 Symfony SonataEasyExtendsBundle
sonata-project/exporter                  1.5.0  Lightweight Exporter library
sonata-project/intl-bundle               2.2.4  Symfony SonataIntlBundle

Symfony packages

$ composer show symfony/*
symfony/assetic-bundle     v2.8.0  Integrates Assetic into Symfony2
symfony/monolog-bundle     2.11.1  Symfony MonologBundle
symfony/phpunit-bridge     v3.1.1  Symfony PHPUnit Bridge
symfony/polyfill-apcu      v1.2.0  Symfony polyfill backporting apcu_* functions to lower PHP versions
symfony/polyfill-intl-icu  v1.2.0  Symfony polyfill for intl's ICU-related data and classes
symfony/polyfill-mbstring  v1.2.0  Symfony polyfill for the Mbstring extension
symfony/polyfill-php54     v1.2.0  Symfony polyfill backporting some PHP 5.4+ features to lower PHP versions
symfony/polyfill-php55     v1.2.0  Symfony polyfill backporting some PHP 5.5+ features to lower PHP versions
symfony/polyfill-php56     v1.2.0  Symfony polyfill backporting some PHP 5.6+ features to lower PHP versions
symfony/polyfill-php70     v1.2.0  Symfony polyfill backporting some PHP 7.0+ features to lower PHP versions
symfony/polyfill-util      v1.2.0  Symfony utilities for portability of PHP codes
symfony/security-acl       v3.0.0  Symfony Security Component - ACL (Access Control List)
symfony/swiftmailer-bundle v2.3.11 Symfony SwiftmailerBundle
symfony/symfony            v2.8.7  The Symfony PHP framework

PHP version

$ php -v
PHP 7.0.7 (cli) (built: Jun 10 2016 02:41:22) ( NTS )
Copyright (c) 1997-2016 The PHP Group
Zend Engine v3.0.0, Copyright (c) 1998-2016 Zend Technologies

Subject

The ModelType can't be used with the new Symfony form way because it's need a service argument: https://github.com/sonata-project/SonataAdminBundle/blob/3.3.2/Form/Type/ModelType.php#L42

We have to find another way. Maybe look at EntityType for inspiration.

cc @sonata-project/contributors for help and suggestions.

Steps to reproduce

Try to change the deprecated form type way from:

protected function configureFormFields(FormMapper $formMapper)
{
    $formMapper
        ->add('groups', 'sonata_type_model', ['required' => false, 'expanded' => true, 'multiple' => true])
    ;
}

To:

protected function configureFormFields(FormMapper $formMapper)
{
    $formMapper
        ->add('groups', ModelType::class, ['required' => false, 'expanded' => true, 'multiple' => true])
    ;
}

Expected results

Working form type.

Actual results

Type error: Argument 1 passed to Sonata\AdminBundle\Form\ChoiceList\ModelChoiceLoader::__construct() must implement interface Sonata\AdminBundle\Model\ModelManagerInterface, null given, called in /code/vendor/sonata-project/admin-bundle/Form/Type/ModelType.php on line 111
bug vendor

Most helpful comment

@Soullivaneuh @greg0ire There you have it!

All 5 comments

Can you provide a stack trace? Knowing where the instantiation takes place would be helpful.

@Soullivaneuh It's not related to Symfony.
The bug is caused by FormContractor::getDefaultOptions not checking against FQCNs.
This issue is fixed in #3971, see https://github.com/sonata-project/SonataAdminBundle/pull/3971/files#diff-672268a470459c46dc5898c5f7265f6cR66

I can work on a PR for both doctrine-orm and doctrine-mongodb bundles (the phpcr bundle does things right atm).
Tell me if you're interested !

@Soullivaneuh It's not related to Symfony.

I never said that. :-)

I can work on a PR for both doctrine-orm and doctrine-mongodb bundles (the phpcr bundle does things right atm).
Tell me if you're interested !

It would be great because I don't know yet when 4.0 will be release and if #3971 will be part of this one.

Thanks!

@Soullivaneuh @greg0ire There you have it!

Was this page helpful?
0 / 5 - 0 ratings