Symfony表单(作为独立组件与Doctrine)EntityType不工作

编程入门 行业动态 更新时间:2024-10-27 09:33:23
本文介绍了Symfony表单(作为独立组件与Doctrine)EntityType不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我使用Symfony表单(v3.0),而没有其余的Symfony框架。使用Doctrine v2.5。

I'm using Symfony forms (v3.0) without the rest of the Symfony framework. Using Doctrine v2.5.

我创建了一个表单,这里是表单类型类:

I've created a form, here's the form type class:

class CreateMyEntityForm extends BaseFormType { public function buildForm(FormBuilderInterface $builder, array $options){ $builder->add('myEntity', EntityType::class); } }

加载页面时,我收到以下错误。

When loading the page, I get the following error.

参数1传递给 Symfony\Bridge\Doctrine\Form\Type\DoctrineType :: __ construct( )必须是一个Doctrine\Common\Persistence\ManagerRegistry的实例,none 给定,在$ b $中的/var/www/dev3/Vendor/symfony/form/FormRegistry.php中调用b line 85

Argument 1 passed to Symfony\Bridge\Doctrine\Form\Type\DoctrineType::__construct() must be an instance of Doctrine\Common\Persistence\ManagerRegistry, none given, called in /var/www/dev3/Vendor/symfony/form/FormRegistry.php on line 85

我相信有一些配置需要放在这里,但我不知道如何创建一个类实现ManagerRegistryInterface - 如果这是正确的事情。

I believe there's some configuration that needs putting in place here, but I don't know how to create a class that implements ManagerRegistryInterface - if that is the right thing to do.

任何指针?

编辑 - 这里是我的设置Doctrine的代码

Edit - here is my code for setting up Doctrine

use Doctrine\ORM\EntityManager; use Doctrine\ORM\Tools\Setup; class Bootstrap { //...some other methods, including getCredentials() which returns DB credentials for Doctrine public function getEntityManager($env){ $isDevMode = $env == 'dev'; $paths = [ROOT_DIR . '/src']; $config = Setup::createAnnotationMetadataConfiguration($paths, $isDevMode, null, null, false); $dbParams = $this->getCredentials($env); $em = EntityManager::create($dbParams, $config); return $em; } }

推荐答案

相信我,你要麻烦了!

Believe me, you're asking for trouble!

EntityType :: class 在与Symfony框架紧密结合时起作用(引擎盖下有魔术 - 通过DoctrineBundle)。否则,您需要编写大量代码才能正常工作。 不值得努力!

EntityType::class works when it is seamsly integrated to "Symfony" framework (there's magic under the hoods - via DoctrineBundle). Otherwise, you need to write a lot of code for it to work properly. Not worth the effort!

如果您创建一个实体存储库并将其注入到表单构造函数中,然后在 ChoiceType :: class 字段中使用,这将会更容易。像这样的事情:

It's a lot easier if you to create an entity repository and inject it in form constructor, then use in a ChoiceType::class field. Somethink like this:

<?php # you form class namespace Application\Form\Type; use Symfony\Component\Form\AbstractType; use Symfony\Component\Form\Extension\Core\Type\ChoiceType; use Symfony\Component\Form\FormBuilderInterface; use Symfony\Component\OptionsResolver\OptionsResolver; class InvoiceItemtType extends AbstractType { public function buildForm(FormBuilderInterface $builder, array $options) { $builder->add('product', ChoiceType::class, [ 'choices' => $this->loadProducts($options['products']) ]); } public function configureOptions(OptionsResolver $resolver) { $resolver->setDefaults(['products' => [],]); # custom form option } private function loadProducts($productsCollection) { # custom logic here (if any) } }

应用程序中的某个位置:

And somewhere in application:

$repo = $entityManager->getRepository(Product::class); $formOptions = ['products' => $repo->findAll()]; $formFactory = Forms::createFormFactory(); $formFactory->create(InvoiceItemtType::class, new InvoiceItem, $formOptions);

这是要点!

更多推荐

Symfony表单(作为独立组件与Doctrine)EntityType不工作

本文发布于:2023-10-10 22:53:02,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1479986.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:表单   组件   独立   工作   Symfony

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!