Silex + Doctrine2 ORM +下拉(EntityType) [英] Silex + Doctrine2 ORM + Dropdown (EntityType)
问题描述
$ builder = $ this-> get(form.factory );
$ em = $ this-> get('doctrine.entity_manager');
$ form = $ builder-> createBuilder(new ClientUserType($ em),新的ClientUser()) - > getForm();
以下是我的ClientUserType类,其中包含一个构造函数,我通过实体管理器:
<?php
命名空间Application\Form\Type;
使用Symfony\Component\Form\AbstractType;
使用Symfony\Component\Form\FormBuilderInterface;
使用Symfony\Bridge\Doctrine\Form\Type\EntityType;
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'=> $
$添加('name')
- > add('surname')
- > add('contact')
- > add('email');
}
public function getName()
{
return'client_user_form';
}
}
我继续收到下面可能的致命错误,并有不知道我需要做什么,以便从具有教义的数据库中获得标题的下拉列表。
可追踪的致命错误:参数1传递到Symfony\Bridge\Doctrine\Form\Type\DoctrineType :: __ construct()必须是Doctrine\Common\Persistence\ManagerRegistry的一个实例,没有给定,在D:\web\中调用在第90行的playground-solutions \vendor\symfony\form\FormRegistry.php并在D中定义:\web\playground-solutions\vendor\symfony\doctrine-bridge\Form\Type \DoctrineType.php在第111行
从该错误中读取我不知道我需要创建一个新的ManagerRegistry注册表实例看来实体经理似乎没有工作。我也想我可能需要直接从实体经理本身获得ManagerRegistry。
有人可以帮助解释最简单的方法来使其工作吗?我能失踪什么
似乎没有配置教条桥形式组件。
添加类
命名空间Your\Namespace;
使用Doctrine\Common\Persistence\AbstractManagerRegistry;
使用Silex\Application;
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不支持。
}
public function setContainer(Application $ container)
{
$ this-> container = $ container;
}
}
并配置doctrine-bridge表单组件
$ application-> register(new Silex\Provider\FormServiceProvider(),[]);
$ application-> extend('form.extensions',function($ extensions,$ application){
if(isset($ application ['form.doctrine.bridge.included' ]))return $ extensions;
$ application ['form.doctrine.bridge.included'] = 1;
$ mr = new Your\Namespace\ManagerRegistry(
null,array(),array('em'),null,null,'\\Doctrine\\\\\Proxy\\Proxy'
);
$ mr-> setContainer($ application);
$ extensions [] = new \Symfony\Bridge\Doctrine\Form\DoctrineOrmExtension($ mr);
return $ extensions;
});
array('em')
是 $ application
中的实体经理的关键
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();
Below is my ClientUserType class with a constructor that I pass the entity manager on:
<?php
namespace Application\Form\Type;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
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.
Catchable fatal error: Argument 1 passed to Symfony\Bridge\Doctrine\Form\Type\DoctrineType::__construct() must be an instance of Doctrine\Common\Persistence\ManagerRegistry, none given, called in D:\web\playground-solutions\vendor\symfony\form\FormRegistry.php on line 90 and defined in D:\web\playground-solutions\vendor\symfony\doctrine-bridge\Form\Type\DoctrineType.php on line 111
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?
Seems that doctrine-bridge form component is not configured.
Add class
namespace Your\Namespace;
use Doctrine\Common\Persistence\AbstractManagerRegistry;
use Silex\Application;
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;
}
}
and configure doctrine-bridge form component
$application->register(new Silex\Provider\FormServiceProvider(), []);
$application->extend('form.extensions', function($extensions, $application) {
if (isset($application['form.doctrine.bridge.included'])) return $extensions;
$application['form.doctrine.bridge.included'] = 1;
$mr = new Your\Namespace\ManagerRegistry(
null, array(), array('em'), null, null, '\\Doctrine\\ORM\\Proxy\\Proxy'
);
$mr->setContainer($application);
$extensions[] = new \Symfony\Bridge\Doctrine\Form\DoctrineOrmExtension($mr);
return $extensions;
});
array('em')
- em is key for entity manager in $application
这篇关于Silex + Doctrine2 ORM +下拉(EntityType)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!