将数据从控制器传递到 symfony2 类型 [英] passing data from controller to Type symfony2

查看:17
本文介绍了将数据从控制器传递到 symfony2 类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我在表单中显示一个实体"类型的字段,并且我想根据我从控制器传递的参数过滤这个实体类型,我该怎么做..?

if i show a field of type "entity" in my form, and i want to filter this entity type based on a argument I pass from the controller, how do i do that.. ?

//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',
    );
}
}

在控制器内部,我创建了表单:

and inside the controller, i create the form :

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 实体.

the $plumeOptions is just a class to persist. But it has a one-to-one relationship with another class called PhysicalPlume. Now, when i want to display the 'framePlume' in my code, i want to show a filtered PhysicalPlume entity.

推荐答案

可以通过如下方式向表单类传递参数:

You can pass parameters to the form class as follows:

//PlumeOptionsType.php
protected $profile;

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

然后在你的 buildForm 的 query_builder 中使用它:

Then use it in the query_builder of your buildForm:

$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);

这篇关于将数据从控制器传递到 symfony2 类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆