Symfony2,集装箱服务

时间:2013-12-24 11:30:08

标签: symfony containers

您好,我在Sonata admin

中的表单的EventSubscriber上有错误
 namespace OneA\AdvertBundle\Form\EventListener;

 use Symfony\Component\Form\FormFactoryInterface;
 use Symfony\Component\EventDispatcher\EventSubscriberInterface;
 use Symfony\Component\DependencyInjection\ContainerAware;

    class CategoryFieldsSubscriber extends ContainerAware implements EventSubscriberInterface
    {
private $factory;
public function __construct(FormFactoryInterface $factory)
{
    $this->factory = $factory;
}

public static function getSubscribedEvents()
{
    return array(FormEvents::PRE_SET_DATA => 'preSetData');
}

public function preSetData(FormEvent $event)
{
    $data = $event->getData();
    $form = $event->getForm();

    //$form->add('advert_fields', 'text');
    $advert_type = $form->get('advert_type')->getData();

    $this->getFields($form, $advert_type);
}

public function getFields($form, $advert_type)
{
    $form->add('advert_fields', 'text', array(
        'label' => 'dfjg',
    ));



    $this->container->get('one_a_advert.admin_motors_field')->getFieldsForm($form);
 }  

}

当我调用容器时,我有这个错误

FatalErrorException: Error: Call to a member function get() on a non-object in 
To the container->get(

1 个答案:

答案 0 :(得分:0)

您需要像这样发送构造函数所需的容器:

services:
kernel.listener.your_listener:
    class: OneA\AdvertBundle\Form\EventListener\CategoryFieldsSubscriber 
    arguments: [@factory, @service_container] 

并使用此

private $container;
private $factory;
public function __construct(FormFactoryInterface $factory, $container)
{
    $this->container = $container;
    $this->factory = $factory;
}