Silex + Doctrine2 ORM + Dropdown (EntityType)

前端 未结 3 1825
南笙
南笙 2021-01-03 06:45

I have a controller that renders a form that is suppose to have a dropdown with titles mapped against a client_user entity. Below is code I use in my controller to create th

相关标签:
3条回答
  • 2021-01-03 07:33

    Seems that doctrine-bridge form component is not configured.
    Add class

    namespace Your\Namespace;
    
    use Doctrine\Common\Persistence\AbstractManagerRegistry;
    use Silex\Application;
    
    class ManagerRegistry extends AbstractManagerRegistry
    {
        protected $container;
    
        protected function getService($name)
        {
            return $this->container[$name];
        }
    
        protected function resetService($name)
        {
            unset($this->container[$name]);
        }
    
        public function getAliasNamespace($alias)
        {
            throw new \BadMethodCallException('Namespace aliases not supported.');
        }
    
        public function setContainer(Application $container)
        {
            $this->container = $container;
        }
    }
    

    and configure doctrine-bridge form component

    $application->register(new Silex\Provider\FormServiceProvider(), []);
    
    $application->extend('form.extensions', function($extensions, $application) {
        if (isset($application['form.doctrine.bridge.included'])) return $extensions;
        $application['form.doctrine.bridge.included'] = 1;
    
        $mr = new Your\Namespace\ManagerRegistry(
            null, array(), array('em'), null, null, '\\Doctrine\\ORM\\Proxy\\Proxy'
        );
        $mr->setContainer($application);
        $extensions[] = new \Symfony\Bridge\Doctrine\Form\DoctrineOrmExtension($mr);
    
        return $extensions;
    });
    

    array('em') - em is key for entity manager in $application

    0 讨论(0)
  • 2021-01-03 07:38

    For others that may find this: If you want to use the EntityType and you're not using a framework at all, you need to add the DoctrineOrmExtension to your FormFactoryBuilder like so:

    $managerRegistry = new myManagerRegistry(
        'myManager',
        array('connection'),
        array('em'),
        'connection',
        'em',
        \Doctrine\ORM\Proxy\Proxy::class
    );
    
    // Setup your Manager Registry or whatever...
    
    $doctrineOrmExtension = new DoctrineOrmExtension($managerRegistry);
    $builder->addExtension($doctrineOrmExtension);
    

    When you use EntityType, myManagerRegistry#getService($name) will be called. $name is the name of the service it needs ('em' or 'connection') and it needs to return the Doctrine entity manager or the Doctrine database connection, respectively.

    0 讨论(0)
  • 2021-01-03 07:38

    In your controller, try to call the service like that:

    $em = $this->get('doctrine.orm.entity_manager');

    Hope it will help you.

    Edit:

    Sorry, I thought you was on Symfony... I have too quickly read...

    0 讨论(0)
提交回复
热议问题