【问题标题】:passing data from controller to Type symfony2将数据从控制器传递到 symfony2 类型
【发布时间】:2011-10-18 12:43:57
【问题描述】:

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

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

【问题讨论】:

标签: symfony


【解决方案1】:

您可以将参数传递给表单类,如下所示:

//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
  • pastebin.com/RVLFCxL4 pastebin.com/778ygFgR pastebin.com/q81k8w9A 我认为我的问题与回调函数有关。我可以从 PlumeOptionsType 内部读取配置文件,但不能从 'query_builder' => function(EntityRepository $er)
  • :请用“使用”语句再次发布该答案,以便我们将此问题设置为已回答!
  • 现在不推荐传递表单类型:stackoverflow.com/questions/34027711/…
【解决方案2】:

您可以使用$plumeOptions 来传递您的所有参数,但您需要在PlumeOptionsType 中添加getDefaultOptions() 以指定您的选项的默认值。 例如查看https://github.com/symfony/symfony/blob/master/src/Symfony/Component/Form/Extension/Core/Type/CheckboxType.php,看看这个方法应该是什么样子。

【讨论】:

  • 你能详细说明一下吗..?你能说的更具体点吗??
  • 我编辑了我的消息以在 getDefaultOptions() 方法上添加更多精度
  • 好的,假设我在返回的 PlumeOptionsType 的 getDefaultOptions() 数组中添加了一个默认的 profile_id。我使用了 ->setParameter('profile', $options['profile_id'])... 但是如何首先将 profile_id 传递给 $plumeOptions ?谢谢你的帮助!!
  • ?!?只需这样做:$plumeOptions['profile_id'] = 42... 我想我不明白真正的问题。
  • 我现在得到了这个: public function editPlumeOptionsAction(Request $request, $user_ou) { //获取会话并从会话中获取 profile_id $repository = $this->getDoctrine()->getRepository('DessinProfileBundle :轮廓'); $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);我收到一个错误“不能使用类型的对象...”
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-04-26
  • 1970-01-01
  • 2012-08-01
  • 2015-09-10
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多