Silex + Doctrine2 ORM + Dropdown (EntityType) [英] Silex + Doctrine2 ORM + Dropdown (EntityType)
问题描述
我有一个控制器,它呈现一个表单,该表单假设有一个下拉列表,标题映射到 client_user 实体.下面是我在控制器中用来创建表单的代码:
I have a controller that renders a form that is suppose to have a dropdown with titles mapped against a client_user entity. Below is code I use in my controller to create the form:
$builder = $this->get(form.factory);
$em = $this->get('doctrine.entity_manager');
$form = $builder->createBuilder(new ClientUserType($em), new ClientUser())->getForm();
下面是我的 ClientUserType 类,它带有一个我传递实体管理器的构造函数:
Below is my ClientUserType class with a constructor that I pass the entity manager on:
<?php
namespace ApplicationFormType;
use SymfonyComponentFormAbstractType;
use SymfonyComponentFormFormBuilderInterface;
use SymfonyBridgeDoctrineFormTypeEntityType;
class ClientUserType extends AbstractType
{
protected $entityManager;
public function __construct($entityManager)
{
$this->entityManager = $entityManager;
}
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('title', EntityType::class, array(
'class' => '\Application\Model\Entity\Title',
'em' => $this->entityManager
))
->add('name')
->add('surname')
->add('contact')
->add('email');
}
public function getName()
{
return 'client_user_form';
}
}
我一直在下面收到这个可捕获的致命错误,但不知道我需要做什么才能从带有教义的数据库中获取标题的下拉列表.
I keep on getting this catchable fatal error below and have no idea what I need to do in order to get a dropdown with titles from a database with doctrine.
可捕获的致命错误:传递给 SymfonyBridgeDoctrineFormTypeDoctrineType::__construct() 的参数 1 必须是 DoctrineCommonPersistenceManagerRegistry 的实例,没有给出,在 D:web 中调用Playground-solutionsvendorsymfonyformFormRegistry.php 在第 90 行并在 D:webplayground-solutionsvendorsymfonydoctrine-bridgeFormTypeDoctrineType.php 中定义在第 111 行
Catchable fatal error: Argument 1 passed to SymfonyBridgeDoctrineFormTypeDoctrineType::__construct() must be an instance of DoctrineCommonPersistenceManagerRegistry, none given, called in D:webplayground-solutionsvendorsymfonyformFormRegistry.php on line 90 and defined in D:webplayground-solutionsvendorsymfonydoctrine-bridgeFormTypeDoctrineType.php on line 111
从该错误中读取我不知道我需要在哪里创建 ManagerRegistry 注册表的新实例,因为实体管理器似乎不起作用.我也在想也许我需要直接从实体管理器本身获取 ManagerRegistry.
Reading from that error I have no idea where I need to create a new instance of ManagerRegistry registry as it appears that the entity manager does not work. I am also thinking perhaps I need to get the ManagerRegistry straight from the entity manager itself.
有人可以帮忙解释一下让它工作的最简单方法吗?我可能会错过什么?
Can someone please help explain the simplest way to get this to work? What could I be missing?
推荐答案
似乎没有配置doctect-bridge表单组件.
添加班级
Seems that doctrine-bridge form component is not configured.
Add class
namespace YourNamespace;
use DoctrineCommonPersistenceAbstractManagerRegistry;
use SilexApplication;
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;
}
}
并配置doctrine-bridge表单组件
and configure doctrine-bridge form component
$application->register(new SilexProviderFormServiceProvider(), []);
$application->extend('form.extensions', function($extensions, $application) {
if (isset($application['form.doctrine.bridge.included'])) return $extensions;
$application['form.doctrine.bridge.included'] = 1;
$mr = new YourNamespaceManagerRegistry(
null, array(), array('em'), null, null, '\Doctrine\ORM\Proxy\Proxy'
);
$mr->setContainer($application);
$extensions[] = new SymfonyBridgeDoctrineFormDoctrineOrmExtension($mr);
return $extensions;
});
array('em')
- em 是 $application
这篇关于Silex + Doctrine2 ORM + Dropdown (EntityType)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!