Symfony 如何使实体字段类型在silex中可用?
我一直在使用Silex进行我的最新项目,我试图遵循Symfony食谱中的。我找到了使用实体字段类型的部分,并意识到它在Silex中不可用 看起来可以将symfony/doctrine桥添加到我的composer.json中,其中包含“EntityType”。是否有人成功获得了在Silex中工作的实体类型,或遇到此问题并找到了解决方法 我在想这样的事情可能有用:Symfony 如何使实体字段类型在silex中可用?,symfony,doctrine-orm,silex,Symfony,Doctrine Orm,Silex,我一直在使用Silex进行我的最新项目,我试图遵循Symfony食谱中的。我找到了使用实体字段类型的部分,并意识到它在Silex中不可用 看起来可以将symfony/doctrine桥添加到我的composer.json中,其中包含“EntityType”。是否有人成功获得了在Silex中工作的实体类型,或遇到此问题并找到了解决方法 我在想这样的事情可能有用: $builder ->add('myentity', new EntityType($objectMana
$builder
->add('myentity', new EntityType($objectManager, $queryBuilder, 'Path\To\Entity'), array(
))
;
我还发现它可能通过扩展form.factory来达到目的,但还没有尝试。我使用Gist在Silex中添加EntityType字段
但是技巧是通过扩展form.extensions
来注册DoctrineOrmExtension
表单扩展,就像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'];
}
}
非常感谢!我不得不在composer.json中添加“symfony/dependency injection”:“~2.3”。非常好,谢谢你!您是否在Silex 2.0 ~上使用了它?
// 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;
}));