Silex + Doctrine2 ORM + 下拉列表(实体类型)
Silex + Doctrine2 ORM + Dropdown (EntityType)
我有一个控制器,它呈现一个表单,该表单应该有一个下拉菜单,其中的标题映射到 client_user 实体。下面是我在控制器中用来创建表单的代码:
$builder = $this->get(form.factory);
$em = $this->get('doctrine.entity_manager');
$form = $builder->createBuilder(new ClientUserType($em), new ClientUser())->getForm();
下面是我的 ClientUserType class 以及我传递给实体管理器的构造函数:
<?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';
}
}
我一直在下面收到这个可捕获的致命错误,并且不知道我需要做什么才能从具有学说的数据库中获得带有标题的下拉列表。
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
从该错误中读取我不知道我需要在哪里创建一个新的 ManagerRegistry 注册表实例,因为实体管理器似乎不起作用。我也在想,也许我需要直接从实体管理器本身获取 ManagerRegistry。
有人可以帮忙解释一下实现此功能的最简单方法吗?我可能会错过什么?
在你的控制器中,尝试像这样调用服务:
$em = $this->get('doctrine.orm.entity_manager');
希望对您有所帮助。
编辑:
抱歉,我以为你在使用 Symfony...我读得太快了...
似乎 doctrine-bridge 表单组件未配置。
添加 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;
}
}
并配置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\ORM\Proxy\Proxy'
);
$mr->setContainer($application);
$extensions[] = new \Symfony\Bridge\Doctrine\Form\DoctrineOrmExtension($mr);
return $extensions;
});
array('em')
- em 是 $application
中实体管理器的关键
对于可能发现此问题的其他人:如果您想使用 EntityType
而您根本没有使用框架,则需要将 DoctrineOrmExtension
添加到您的 FormFactoryBuilder
像这样:
$managerRegistry = new myManagerRegistry(
'myManager',
array('connection'),
array('em'),
'connection',
'em',
\Doctrine\ORM\Proxy\Proxy::class
);
// Setup your Manager Registry or whatever...
$doctrineOrmExtension = new DoctrineOrmExtension($managerRegistry);
$builder->addExtension($doctrineOrmExtension);
当你使用EntityType
时,myManagerRegistry#getService($name)
会被调用。 $name
是它需要的服务名称('em' 或 'connection'),它需要分别 return Doctrine 实体管理器或 Doctrine 数据库连接。
我有一个控制器,它呈现一个表单,该表单应该有一个下拉菜单,其中的标题映射到 client_user 实体。下面是我在控制器中用来创建表单的代码:
$builder = $this->get(form.factory);
$em = $this->get('doctrine.entity_manager');
$form = $builder->createBuilder(new ClientUserType($em), new ClientUser())->getForm();
下面是我的 ClientUserType class 以及我传递给实体管理器的构造函数:
<?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';
}
}
我一直在下面收到这个可捕获的致命错误,并且不知道我需要做什么才能从具有学说的数据库中获得带有标题的下拉列表。
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
从该错误中读取我不知道我需要在哪里创建一个新的 ManagerRegistry 注册表实例,因为实体管理器似乎不起作用。我也在想,也许我需要直接从实体管理器本身获取 ManagerRegistry。
有人可以帮忙解释一下实现此功能的最简单方法吗?我可能会错过什么?
在你的控制器中,尝试像这样调用服务:
$em = $this->get('doctrine.orm.entity_manager');
希望对您有所帮助。
编辑:
抱歉,我以为你在使用 Symfony...我读得太快了...
似乎 doctrine-bridge 表单组件未配置。
添加 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;
}
}
并配置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\ORM\Proxy\Proxy'
);
$mr->setContainer($application);
$extensions[] = new \Symfony\Bridge\Doctrine\Form\DoctrineOrmExtension($mr);
return $extensions;
});
array('em')
- em 是 $application
对于可能发现此问题的其他人:如果您想使用 EntityType
而您根本没有使用框架,则需要将 DoctrineOrmExtension
添加到您的 FormFactoryBuilder
像这样:
$managerRegistry = new myManagerRegistry(
'myManager',
array('connection'),
array('em'),
'connection',
'em',
\Doctrine\ORM\Proxy\Proxy::class
);
// Setup your Manager Registry or whatever...
$doctrineOrmExtension = new DoctrineOrmExtension($managerRegistry);
$builder->addExtension($doctrineOrmExtension);
当你使用EntityType
时,myManagerRegistry#getService($name)
会被调用。 $name
是它需要的服务名称('em' 或 'connection'),它需要分别 return Doctrine 实体管理器或 Doctrine 数据库连接。