我一直在将Silex用于我的最新项目,并尝试遵循Symfony食谱中的“如何使用表单事件动态修改表单”。我接触到使用实体字段类型的部分,并意识到它在Silex中不可用。
看起来symfony / doctrine-bridge可以添加到包含“ EntityType”的composer.json中。有没有人成功让实体类型在Silex中工作或遇到此问题并找到了解决方法?
我在想像这样的事情可能会起作用:
$builder
->add('myentity', new EntityType($objectManager, $queryBuilder, 'Path\To\Entity'), array(
))
;
我还找到了这个答案,看起来它可以通过扩展form.factory来解决问题,但尚未尝试。
我使用此要点在Silex中添加EntityType字段。
但是窍门是DoctrineOrmExtension
通过form.extensions
像FormServiceProvider 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 Саша Стаменковић <[email protected]>
*/
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'];
}
}
因此,要注册表单扩展名,我使用:
// 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;
}));
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句