如何在silex中使实体字段类型可用?

ooX*_*1sh 5 symfony doctrine-orm silex

我一直在使用Silex进行我的最新项目,并且我试图跟随Symfony食谱中的"如何使用表单事件动态修改表单".我到了使用实体字段类型的部分,并意识到它在Silex中不可用.

看起来symfony/doctrine-bridge可以添加到我的composer.json中,其中包含"EntityType".有没有人成功获得实体类型在Silex工作或遇到此问题并找到了解决方法?

我在想这样的事可能有用:

    $builder
        ->add('myentity', new EntityType($objectManager, $queryBuilder, 'Path\To\Entity'), array(
    ))
    ;
Run Code Online (Sandbox Code Playgroud)

我也发现这个答案看起来可能通过扩展form.factory来解决问题,但还没有尝试过.

小智 6

我使用 Gist在Silex中添加EntityType字段.

但诀窍是DoctrineOrmExtension通过form.extensionsFormServiceProvider doc那样的扩展来注册表单扩展.

DoctrineOrmExtension期望ManagerRegistry在其构造函数中有一个接口,可以实现扩展Doctrine\Common\Persistence\AbstractManagerRegistry如下:

<?php
namespace MyNamespace\Form\Extensions\Doctrine\Bridge;

use Doctrine\Common\Persistence\AbstractManagerRegistry;
use Silex\Application;

/**
 * References Doctrine connections and entity/document managers.
 *
 * @author ???? ??????????? <umpirsky@gmail.com>
 */
class ManagerRegistry extends AbstractManagerRegistry
{

    /**
     * @var Application
     */
    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['orm.ems'];

    }

}
Run Code Online (Sandbox Code Playgroud)

因此,要注册表单扩展我使用:

// Doctrine Brigde for form extension
$app['form.extensions'] = $app->share($app->extend('form.extensions', function ($extensions) use ($app) {
    $manager = new MyNamespace\Form\Extensions\Doctrine\Bridge\ManagerRegistry(
        null, array(), array('default'), null, null, '\Doctrine\ORM\Proxy\Proxy'
    );
    $manager->setContainer($app);
    $extensions[] = new Symfony\Bridge\Doctrine\Form\DoctrineOrmExtension($manager);

    return $extensions;
}));
Run Code Online (Sandbox Code Playgroud)