简体   繁体   English

可捕获的致命错误:传递给AppBundle \\ Form \\ TagType :: __ construct()的参数1必须是Doctrine \\ ORM \\ EntityRepository的实例,未给出任何实例,

[英]Catchable Fatal Error: Argument 1 passed to AppBundle\Form\TagType::__construct() must be an instance of Doctrine\ORM\EntityRepository, none given,

I am using EntityType field to create a multiple select dropdown. 我正在使用EntityType字段创建多选下拉列表。 Also I want to query the available options based on user input (eg. standard ), and then display the options. 我也想根据用户输入(例如standard )查询可用选项,然后显示这些选项。 For this, I am trying to inject EntityRepository into my TagType form itself, so that I can use it's CreateQueryBuilder . 为此,我试图将EntityRepository注入我的TagType表单本身,以便可以使用它的CreateQueryBuilder But I get this error - 但是我得到这个错误-

在此处输入图片说明

Here is my code, TagType.php : 这是我的代码TagType.php

<?php

namespace AppBundle\Form;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Doctrine\ORM\EntityRepository;
use Symfony\Bridge\Doctrine\Form\Type\EntityType;

class TagType extends AbstractType {

    protected $er;

    public function __construct(EntityRepository $er)
    {
        $this->er = $er;
    }

    private function getTagsByCategoryName($categoryname, $er) {
        return $er->createQueryBuilder('t')
                ->innerJoin('t.categories', 'c', 'WITH', 'c.categoryname = :categoryname')
                ->setParameter('categoryname', $categoryname)
                ->orderBy('t.id', 'ASC');
    }

    public function buildForm(FormBuilderInterface $builder, array $options) {
        $builder->add('tagname', EntityType::class, array(
            'class' => 'AppBundle:Tag',
            'choices' => $this->getTagsByCategoryName('standard'),
            'choice_label' => 'tagname',
            'expanded' => false,
            'multiple' => true,
            'label' => 'Choose Tags',
        ));
    }

    public function configureOptions(OptionsResolver $resolver) {
        $resolver->setDefaults(array(
            'data_class' => 'AppBundle\Entity\Tag',
            'tags' => null,
        ));
    }

}

Any ideas? 有任何想法吗?

2 answers: 2个答案:

  • You don't need to inject EntityRepository into your Form to use query_builder option of EntityType . 您无需将EntityRepository注入到Form中即可使用EntityType query_builder选项。 EntityType will inject the EntityRepository on your behalf, you just need to define a method taking EntityRepository as parameter. EntityType将代表您注入EntityRepository ,您只需要定义一个以EntityRepository作为参数的方法即可。 Knowing this: 知道这一点:

     class TagType extends AbstractType { private function getTagsByCategoryName($categoryname) { return function(EntityRepository $er) { return $er->createQueryBuilder('t') ->innerJoin('t.categories', 'c', 'WITH', 'c.categoryname = :categoryname') ->setParameter('categoryname', $categoryname) ->orderBy('t.id', 'ASC'); } } public function buildForm(FormBuilderInterface $builder, array $options) { $builder->add('tagname', EntityType::class, array( 'class' => 'AppBundle:Tag', 'choices' => $this->getTagsByCategoryName('standard'), 'choice_label' => 'tagname', 'expanded' => false, 'multiple' => true, 'label' => 'Choose Tags', )); } public function configureOptions(OptionsResolver $resolver) { $resolver->setDefaults(array( 'data_class' => 'AppBundle\\Entity\\Tag', 'tags' => null, )); } } 

should be enough. 应该足够了。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 可捕获的致命错误:传递给Controller :: __ construct()的参数1必须是Doctrine \\ ORM \\ EntityManager的实例,未给出任何实例,称为 - Catchable Fatal Error: Argument 1 passed to Controller::__construct() must be an instance of Doctrine\ORM\EntityManager, none given, called 可捕获的致命错误:传递给 Album\Controller\AlbumController::__construct() 的参数 1 必须是 Album\Model\AlbumTable 的实例,没有给出 - Catchable fatal error: Argument 1 passed to Album\Controller\AlbumController::__construct() must be an instance of Album\Model\AlbumTable, none given Symfony2:ContextErrorException:可捕获的致命错误:传递给[…] :: __ construct()的参数1必须实现接口[…]没有给出 - Symfony2: ContextErrorException: Catchable Fatal Error: Argument 1 passed to […]::__construct() must implement interface […] none given 可捕获的致命错误:传递给UserBundle \\ Form \\ UserType :: __ construct()的参数2必须是实例? - Catchable Fatal Error: Argument 2 passed to UserBundle\Form\UserType::__construct() must be an instance ? 可捕获的致命错误:传递给getPrice()的参数1必须是Rectangle的实例,没有给出 - Catchable fatal error: Argument 1 passed to getPrice() must be an instance of Rectangle, none given 主义(Symfony3)可捕获的致命错误:传递给(bundle)的参数1必须是(bundle)的实例,给定数组 - Doctrine (Symfony3) Catchable Fatal Error: Argument 1 passed to (bundle) must be an instance of (bundle), array given 可捕获的致命错误:传递给...的参数1必须是...,给定数组的实例 - Catchable Fatal Error: Argument 1 passed to … must be an instance of …, array given 可捕获的致命错误:传递给Doctrine \\ DBAL \\ Connection :: update()的参数3必须为数组类型,在symfony2 update中没有给出 - Catchable Fatal Error: Argument 3 passed to Doctrine\DBAL\Connection::update() must be of the type array, none given in symfony2 update Action 类型错误:参数1传递Chat :: __ construc t()必须是Doctrine \\ ORM \\ EntityManager的实例,未给出任何实例,在 - Type error: Argument 1 passed Chat::__construc t() must be an instance of Doctrine\ORM\EntityManager, none given, called in 切换表时出现zend db错误可捕获的致命错误:传递给__construct()的参数1必须是一个数组,给定对象,在 - zend db error on switching tables Catchable fatal error: Argument 1 passed to __construct() must be an array, object given, called in
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM