Symonfy:如何在表单订阅者中使用doctrine manager?

时间:2016-06-12 06:19:08

标签: symfony

我有一个表单订阅者,我想在其中使用doctrine manager。但是我收到了错误消息:

"Catchable Fatal Error: Argument 1 passed to ...\ProductEavAttributesSubscriber::__construct() must be an instance of Doctrine\ORM\EntityManager, instance of Symfony\Component\Form\FormFactory given。

有我的订阅者文件代码:

namespace Demo\Bundle\ProductBundle\EventListener;

use Symfony\Bridge\Doctrine\Form\Type\EntityType;
use Symfony\Component\Form\FormEvent;
use Symfony\Component\Form\FormEvents;
use Symfony\Component\Form\FormFactoryInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Doctrine\ORM\EntityRepository;
use Doctrine\ORM\EntityManager;

class ProductEavAttributesSubscriber implements EventSubscriberInterface
{
    private $entityManager;

    public function __construct(EntityManager $entityManager)
    {
        $this->entityManager = $entityManager;
    }
// coding
}

in service.yml

demo.product.eav.attribute:
    class: ...\EventListener\ProductEntityAttributesSubscriber
    arguments: ["@doctrine.orm.entity_manager"]
    tags:
        - { name: form.type, alias: product_eav_attribute }

表单类型:

$builder->add('specifications', ProductEavAttributeType::class);

在ProductEavAttributeType文件中:

class ProductEavAttributeType extends AbstractType
{
    /**
     * @param FormBuilderInterface $builder
     * @param array $options
     */
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $factory = $builder->getFormFactory();
        $productEavAttributes = new ProductEavAttributesSubscriber($factory);
        $builder->addEventSubscriber($productEavAttributes);
    }
}

那么,现在该怎么做?

0 个答案:

没有答案