2011-10-18 65 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

相关问题