将数据从控制器传递到类型symfony2

时间:2023-01-28 11:19:41

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

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.

$ plumeOptions只是一个要坚持的类。但它与另一个叫做PhysicalPlume的类有着一对一的关系。现在,当我想在我的代码中显示'framePlume'时,我想显示一个过滤的PhysicalPlume实体。

2 个解决方案

#1


40  

You can pass parameters to the form class as follows:

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

//PlumeOptionsType.php
protected $profile;

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

Then use it in the query_builder of your buildForm:

然后在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)
                            ;
                        },

));

And finally in your controller:

最后在你的控制器中:

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

#2


4  

You can use $plumeOptions to pass everything your argument, but you'll need to add a getDefaultOptions() in PlumeOptionsType to specify the default value for your option. See for instance https://github.com/symfony/symfony/blob/master/src/Symfony/Component/Form/Extension/Core/Type/CheckboxType.php to see what this method should look like.

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

#1


40  

You can pass parameters to the form class as follows:

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

//PlumeOptionsType.php
protected $profile;

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

Then use it in the query_builder of your buildForm:

然后在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)
                            ;
                        },

));

And finally in your controller:

最后在你的控制器中:

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

#2


4  

You can use $plumeOptions to pass everything your argument, but you'll need to add a getDefaultOptions() in PlumeOptionsType to specify the default value for your option. See for instance https://github.com/symfony/symfony/blob/master/src/Symfony/Component/Form/Extension/Core/Type/CheckboxType.php to see what this method should look like.

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