gpt4 book ai didi

symfony - Silex + Doctrine2 ORM + 下拉菜单(实体类型)

转载 作者:行者123 更新时间:2023-12-02 16:29:51 26 4
gpt4 key购买 nike

我有一个 Controller ,它呈现一个表单,该表单应该有一个下拉列表,其中标题映射到 client_user 实体。以下是我在 Controller 中用于创建表单的代码:

$builder = $this->get(form.factory);
$em = $this->get('doctrine.entity_manager');

$form = $builder->createBuilder(new ClientUserType($em), new ClientUser())->getForm();

下面是我的 ClientUserType 类,其中包含我传递实体管理器的构造函数:

<?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';
}
}

我不断收到下面这个可捕获的 fatal error ,并且不知道我需要做什么才能从具有学说的数据库中获取包含标题的下拉列表。

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。

有人可以帮忙解释一下让它发挥作用的最简单方法吗?我可能会错过什么?

最佳答案

似乎没有配置doctrine-bridge表单组件。
添加类

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 中实体管理器的关键

关于symfony - Silex + Doctrine2 ORM + 下拉菜单(实体类型),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34589024/

26 4 0