2011-10-18 69 views
24

如果我在窗體中顯示「實體」類型的字段,並且我想根據從控制器傳遞的參數過濾此實體類型,那麼我該怎麼做?將數據從控制器傳遞到類型symfony2

//PlumeOptionsType.php 
public function buildForm(FormBuilder $builder, array $options) 
{ 
    $builder->add('framePlume', 'entity', array(
     'class' => 'DessinPlumeBundle:PhysicalPlume', 
     'query_builder' => function(EntityRepository $er) { 
           return $er->createQueryBuilder('pp') 
            ->where("pp.profile = :profile") 
            ->orderBy('pp.index', 'ASC') 
            ->setParameter('profile', ????) 
           ; 
          }, 

    )); 
} 

public function getName() 
{ 
    return 'plumeOptions'; 
} 

public function getDefaultOptions(array $options) 
{ 
    return array(
      'data_class'  => 'Dessin\PlumeBundle\Entity\PlumeOptions', 
      'csrf_protection' => true, 
      'csrf_field_name' => '_token', 
      // a unique key to help generate the secret token 
      'intention'  => 'plumeOptions_item', 
    ); 
} 
} 

,並在控制器內,我創建表單:

i have that argument that i need to pass in my action code: 
$profile_id = $this->getRequest()->getSession()->get('profile_id'); 
... 
and then i create my form like this 
$form = $this->createForm(new PlumeOptionsType(), $plumeOptions); 

的$ plumeOptions只是堅持一個類。但它與另一個稱爲PhysicalPlume的類有一對一的關係。現在,當我想在我的代碼中顯示'framePlume'時,我想顯示一個過濾的PhysicalPlume實體。

+0

已回答... 檢查http://stackoverflow.com/questions/6716776/symfony-2-how-to-pass-data-to-for mbuilder – xeon

回答

38

您可以將參數傳遞給窗體類如下:

//PlumeOptionsType.php 
protected $profile; 

public function __construct (Profile $profile) 
{ 
    $this->profile = $profile; 
} 

然後在你的buildForm的query_builder使用它:

$profile = $this->profile; 

$builder->add('framePlume', 'entity', array(
    'class' => 'DessinPlumeBundle:PhysicalPlume', 
    'query_builder' => function(EntityRepository $er) use ($profile) { 
          return $er->createQueryBuilder('pp') 
           ->where("pp.profile = :profile") 
           ->orderBy('pp.index', 'ASC') 
           ->setParameter('profile', $profile) 
          ; 
         }, 

)); 

最後在你的控制器:

// fetch $profile from DB 
$form = $this->createForm(new PlumeOptionsType($profile), $plumeOptions); 
+0

thx回答,我認爲你得到了我的意思...... 不過,我得到了一個錯誤,完全按照你的建議。 使用$這個時候不要在PlumeBundle \表格\類型對象上下文 \ PlumeOptionsType.php – xeon

+0

http://pastebin.com/RVLFCxL4 http://pastebin.com/778ygFgR – xeon

+0

http://pastebin.com/RVLFCxL4 http://pastebin.com/778ygFgR http://pastebin.com/q81k8w9A 我覺得我的問題與回調函數有關。我可以從PlumeOptionsType內部讀取配置文件,但不能從 'query_builder'=>函數(EntityRepository $ er) – xeon

4

您可以使用$plumeOptions傳遞所有參數,但您需要在PlumeOptionsType中添加一個getDefaultOptions()以指定選項的默認值。 請參閱https://github.com/symfony/symfony/blob/master/src/Symfony/Component/Form/Extension/Core/Type/CheckboxType.php以查看此方法的外觀。

+1

你能詳細闡述一下..嗎? 你能更具體嗎? – xeon

+0

我編輯了我的消息以在'getDefaultOptions()'方法中添加更多精度 – greg0ire

+0

好吧,比方說,我在返回的PlumeOptionsType的getDefaultOptions()數組中添加了一個默認的profile_id。 和我用 - > setParameter('profile',$ options ['profile_id'])... 但如何將profile_id傳遞給$ plumeOptions在第一個地方? 謝謝你的幫助! – xeon

相關問題