我使用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不工作
发布评论