将数据从控制器传递到Symfony2的Type

25

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

//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实体。


已经回答了,请查看:https://dev59.com/uWw15IYBdhLWcg3wSJo3 - xeon
2个回答

40
您可以按照以下方式向表单类传递参数:

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

谢谢您的回答,我认为您完全理解了我的意思... 不过,我按照您建议的方式操作时出现了错误。 在PlumeBundle\Form\Type\PlumeOptionsType.php中使用$this时未处于对象上下文中。 - xeon
我认为我的问题与回调函数有关。我可以从PlumeOptionsType内部读取配置文件,但无法从“query_builder” => function(EntityRepository $er)内部读取。 http://pastebin.com/RVLFCxL4http://pastebin.com/778ygFgRhttp://pastebin.com/q81k8w9A - xeon
请再次发布带有“use”语句的答案,这样我们就可以将此问题标记为已回答!! - xeon
6
传递表单类型的方法已被弃用:https://dev59.com/tFsX5IYBdhLWcg3wRdp_ - Jonathan

4

1
你能详细说明一下吗? 你能更具体一些吗? - xeon
我编辑了我的消息,以更详细地说明getDefaultOptions()方法。 - greg0ire
只需要这样做:$plumeOptions['profile_id'] = 42... 我认为我没有理解真正的问题。 - greg0ire
我现在明白了:public function editPlumeOptionsAction(Request $request, $user_ou) { //获取会话并从会话中获取profile_id $repository = $this->getDoctrine()->getRepository('DessinProfileBundle:Profile'); $profile = $repository->findOneById($profile_id); $plumeOptions = $profile->getPlumeOptions(); if(!$plumeOptions instanceof PlumeOptions) $plumeOptions = new PlumeOptions(); $plumeOptions['profile_id'] = $profile_id; $form = $this->createForm(new PlumeOptionsType(), $plumeOptions);我得到了一个错误:“无法使用类型为...的对象”。 - xeon
我有一个表格Profile与表格PlumeOptions之间的1<->1关系。 并且表格PlumeOptions与表格PhysicalPlume之间也有1<->1关系。 而表格Profile与200个PhysicalPlume之间有1对多的关系。因此,在表格PhysicalPlume中,有一个FK profile id。 这就是我希望我的表单显示的内容: 不是所有的PhysicalPlumes,而只是与该Profile相关的那些。 希望这能够澄清一些情况。 - xeon
显示剩余3条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接