我一直在我的最新项目中使用 Silex,并且我试图遵循《如何使用表单事件动态修改表单》 http://symfony.com/doc/2.3/cookbook/form/dynamic_form_modification.html#cookbook-form-events-user-data在 Symfony 食谱中。我到达使用实体字段类型的部分,并意识到它在 Silex 中不可用。
看起来 symfony/doctrine-bridge 可以添加到我的包含“EntityType”的composer.json中。有没有人成功地让实体类型在 Silex 中工作或遇到此问题并找到解决方法?
我在想这样的事情可能会起作用:
$builder
->add('myentity', new EntityType($objectManager, $queryBuilder, 'Path\To\Entity'), array(
))
;
我还发现这个答案 https://stackoverflow.com/a/12619255/1814739看起来它可以通过扩展 form.factory 来实现这一点,但尚未尝试。
I use this https://gist.github.com/umpirsky/3939837在 Silex 中添加 EntityType 字段的要点。
但诀窍是注册DoctrineOrmExtension
通过扩展形成扩展form.extensions
like 表单服务提供者 http://silex.sensiolabs.org/doc/providers/form.html医生说。
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] /cdn-cgi/l/email-protection>
*/
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;
}));
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)