Silex + Doctrine2 ORM + Dropdown (EntityType)

元气小坏坏 提交于 2019-11-30 22:35:07

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

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.

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...

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!