首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Symfony2,容器服务

Symfony2,容器服务
EN

Stack Overflow用户
提问于 2013-12-24 19:30:08
回答 1查看 295关注 0票数 1

您好,我有一个错误在EventSubscriber上的形式在奏鸣曲管理

代码语言:javascript
复制
 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);
 }  

}

当我调用容器时,我得到了这个错误

代码语言:javascript
复制
FatalErrorException: Error: Call to a member function get() on a non-object in 
To the container->get(
EN

回答 1

Stack Overflow用户

发布于 2013-12-24 19:36:44

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

代码语言:javascript
复制
services:
kernel.listener.your_listener:
    class: OneA\AdvertBundle\Form\EventListener\CategoryFieldsSubscriber 
    arguments: [@factory, @service_container] 

并像这样使用它

代码语言:javascript
复制
private $container;
private $factory;
public function __construct(FormFactoryInterface $factory, $container)
{
    $this->container = $container;
    $this->factory = $factory;
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/20760124

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档