sylius如何在不使用构造函数的情况下在表单类型类中获取容器?

时间:2016-06-26 16:35:07

标签: symfony sylius

我想在表单类型类中使用服务容器,但是$ this->容器总是返回null。

这里是我的表格的类

<?php

namespace St\AppBundle\Form\Type;

use Sylius\Bundle\CoreBundle\Form\Type\ShippingMethodType as BaseShippingMethodType;
use Symfony\Component\Form\FormBuilderInterface;

use Symfony\Component\Form\FormEvent;
use Symfony\Component\Form\FormEvents;

use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\DependencyInjection\ContainerAwareInterface;

class ShippingMethodType extends BaseShippingMethodType implements ContainerAwareInterface
{

    protected $container;

    public function setContainer(ContainerInterface $container = null)
    {
        $this->container = $container;
    }

    /**
     * @param FormBuilderInterface $builder
     * @param array $options
     */
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        parent::buildForm($builder, $options);

        $builder
            ->add('file', 'file', ['required' => false])
        ;

        var_dump($this->container);

        $builder->addEventListener(FormEvents::POST_SUBMIT, function(FormEvent $event)  {

            $data = $event->getData();

            $tools = $this->container->get('a_tools');
            $tools->saveImage($data);
        });
    }

}

这里是这个表单的服务,我使用setter注入方法,但它似乎不起作用,因为类中$ this-&gt;容器的var_dump返回null

a_shipping_method.form.type:
    class: St\AppBundle\Form\Type\ShippingMethodType
    calls:
      - [setContainer, ['@service_container']]
    tags:
      - { name: form.type, alias: sylius_shipping_method }

为什么二传手注射对这种情况不起作用?

0 个答案:

没有答案