2012-07-01 177 views
0

加入收藏讓我先解釋一下什麼是我想要做的事。的Symfony2式事件偵聽

所以我認爲有一個名爲類型字段實體,稱爲屬性(也可以是文本,電子郵件,或multi_option)和被叫的PropertyValue另一個實體,這些實體的財產

的價值所以我發現this tutorial和我問題是在EventListener中,而不是一個簡單的字段,我怎樣才能添加一個選擇或複選框的其他實體的值?

這裏有我的事件監聽的代碼,你可以看到我面臨的問題

<?php 
namespace Comehoy\AdBundle\Form\EventListener; 

use Symfony\Component\Form\Event\DataEvent; 
use Symfony\Component\Form\FormFactoryInterface; 
use Symfony\Component\EventDispatcher\EventSubscriberInterface; 
use Symfony\Component\Form\FormEvents; 

class AddValueFieldSubscriber implements EventSubscriberInterface 
{ 
    private $factory; 

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

    public static function getSubscribedEvents() 
    { 
     // Tells the dispatcher that we want to listen on the form.pre_set_data 
     // event and that the preSetData method should be called. 
     return array(FormEvents::PRE_SET_DATA => 'preSetData'); 
    } 

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

     // During form creation setData() is called with null as an argument 
     // by the FormBuilder constructor. We're only concerned with when 
     // setData is called with an actual Entity object in it (whether new, 
     // or fetched with Doctrine). This if statement let's us skip right 
     // over the null condition. 
     if (null === $data) { 
      return; 
     } 

     // check if the ProprertyValue object is "new" 
     $type = $data->getI18nField()->getProperty()->getType(); 

     if ('multi_option' === substr($type, 0, 12)) { 
      /* 
      * 
      * Here is the problem since I'm kind of sure this is not the way to do this 
      * 
      */ 
      $builder = $this->factory->createNamedBuilder('entity', 'value'); 
      $builder->add('value', 'entity', array(
       'class' => 'ComehoyAdBundle:Translation\AdPropertyOption', 
       'query_builder' => function(EntityRepository $er) { 
        return $er->createQueryBuilder('po') 
         ->orderBy('po.value', 'ASC'); 
       } 
      )); 
     } else { 
      //It's not a multi option field so we use the type directly 
      $form->add($this->factory->createNamed($type, 'value')); 
     } 
    } 
} 

那麼我基本上試圖做的是,我可以在buildForm使用做一個類型的同樣的事情$工具的詳情參數

感謝

回答

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

    $form->add($this->factory->createNamed('type', 'name', $data->getValue(), array(
      'class' => 'ComehoyAdBundle:Translation\AdPropertyOption', 
      'query_builder' => function(EntityRepository $er) { 
       return $er->createQueryBuilder('po') 
        ->orderBy('po.value', 'ASC'); 
      } 
    ))); 
} 

如果您使用的SF 2.1,則:

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

    $form->add($this->factory->createNamed('name', 'type', $data->getValue(), array(
      'class' => 'ComehoyAdBundle:Translation\AdPropertyOption', 
      'query_builder' => function(EntityRepository $er) { 
       return $er->createQueryBuilder('po') 
        ->orderBy('po.value', 'ASC'); 
      } 
    ))); 
} 
+0

所以基本上我錯過了一個參數嗎?感謝您的回答 –

+0

您已經創建了構建器,並且沒有添加由構建器創建的表單,以便讓偵聽器連接到表單。 –