symfony2 - 从数据库添加选择 [英] symfony2 - adding choices from database

查看:128
本文介绍了symfony2 - 从数据库添加选择的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在symfony2中使用自定义查询中的值填充一个选择框。我试图尽可能简化。



控制器

  class PageController extends Controller 
{

public function indexAction()
{
$ fields = $ this-> get('fields );
$ countries = $ fields-> getCountries(); //返回一个国家数组,例如array('UK','France','etc')
$ routeSetup = new RouteSetup(); //这是实体
$ routeSetup-> setCountries($ countries); //设置国家数组

$ chooseRouteForm = $ this-> createForm(new ChooseRouteForm(),$ routeSetup);


return $ this-> render('ExampleBundle:Page:index.html.twig',array(
'form'=> $ chooseRouteForm-> createView ()
));

}
}

ChooseRouteForm

  class ChooseRouteForm extends AbstractType 
{

public function buildForm(FormBuilderInterface $ builder, array $ options)
{

//错误...理想情况下,我想要从$ routeSetup对象中获取项目
$ builder-> add('countries' ,'choice',array(
'choices'=> $ this-> routeSetup-> getCountries()
));

}

public function getName()
{
return'choose_route';
}
}


解决方案

你可以将选项传递给您的表单。

  $ chooseRouteForm = $ this-> createForm(new ChooseRouteForm($ routeSetup) ,$ routeSetup); 

然后以您的形式..

  private $ countries; 

public function __construct(RouteSetup $ routeSetup)
{
$ this-> countries = $ routeSetup-> getCountries();
}

public function buildForm(FormBuilderInterface $ builder,array $ options)
{
$ builder-> add('countries','choice',array (
'choices'=> $ this-> countries,
));
}

2.8 + / p>

首先,除非要存储在数据库中,否则首先您不需要将路径对象作为一部分传递到国家/地区。



如果将可用国家/地区存储在数据库中,则可以使用事件侦听器。如果没有(或者您不想使用收听者),您可以在选项区域中添加国家/地区。



使用选项



在控制器中。

  $ selectRouteForm = $ this-> createForm(
ChooseRouteForm :: class,
//或使用< php 5.5
$ routeSetup,
数组(' countries'=> $ fields-> getCountries())
);

您的形式..

  public function buildForm(FormBuilderInterface $ builder,array $ options)
{
$ builder-> add('countries','choice',array(
'choices'=> $ options ['countries'],
));
}

public function configureOptions(OptionsResolver $ resolver)
{
$ resolver
- > setDefault('countries',null)
- > setRequired('countries')
- > setAllowedTypes('countries',array('array'))
;
}

使用听众 (如果国家数组在模型中可用)



在控制器中。

  $ chooseRouteForm = $ this-> createForm(
ChooseRouteForm :: class,
//或使用< php 5.5
$ routeSetup $ b的完整类名$ b);

您的形式..

  public function buildForm(FormBuilderInterface $ builder,array $ options)
{
$ builder
- > addEventListener(FormEvents :: PRE_SET_DATA,function(FormEvent $事件){
$ form = $ event-> getForm();
/ ** @var RouteSetup $ routeSetup * /
$ routeSetup = $ event-> getData();

if(null === $ routeSetup){
throw new \Exception('RouteSetup必须注入到表单中);
}

$ form
- > add('countries','choice',array(
'choices'=> $ routeSetup-> getCountries(),
))
;
})
;
}


I am looking to populate a choice box in symfony2 with values from a custom query. I have tried to simplify as much as possible.

Controller

class PageController extends Controller
{

    public function indexAction()
    {
      $fields = $this->get('fields');
      $countries =  $fields->getCountries(); // returns a array of countries e.g. array('UK', 'France', 'etc')
      $routeSetup = new RouteSetup(); // this is the entity
      $routeSetup->setCountries($countries); // sets the array of countries

      $chooseRouteForm = $this->createForm(new ChooseRouteForm(), $routeSetup);


      return $this->render('ExampleBundle:Page:index.html.twig', array(
        'form' => $chooseRouteForm->createView()
      ));

    }
}

ChooseRouteForm

class ChooseRouteForm extends AbstractType
{

  public function buildForm(FormBuilderInterface $builder, array $options)
  {

    // errors... ideally I want this to fetch the items from the $routeSetup object 
    $builder->add('countries', 'choice', array(
      'choices' => $this->routeSetup->getCountries()
    ));

  }

  public function getName()
  {
    return 'choose_route';
  }
}

解决方案

You could pass the choices to your form using..

$chooseRouteForm = $this->createForm(new ChooseRouteForm($routeSetup), $routeSetup);

Then in your form..

private $countries;

public function __construct(RouteSetup $routeSetup)
{
    $this->countries = $routeSetup->getCountries();
}

public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder->add('countries', 'choice', array(
        'choices' => $this->countries,
    ));
}

Updated (and improved) for 2.8+

Firstly you don't really need to pass in the countries as part of the route object unless they are going to be stored in the DB.

If storing the available countries in the DB then you can use an event listener. If not (or if you don't want to use a listener) you can add the countries in the options area.

Using Options

In the controller..

$chooseRouteForm = $this->createForm(
    ChooseRouteForm::class,
    // Or the full class name if using < php 5.5
    $routeSetup,
    array('countries' => $fields->getCountries())
);

And in your form..

public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder->add('countries', 'choice', array(
        'choices' => $options['countries'],
    ));
}

public function configureOptions(OptionsResolver $resolver)
{
    $resolver
        ->setDefault('countries', null)
        ->setRequired('countries')
        ->setAllowedTypes('countries', array('array'))
    ;
}

Using A Listener (If the countries array is available in the model)

In the controller..

$chooseRouteForm = $this->createForm(
    ChooseRouteForm::class,
    // Or the full class name if using < php 5.5
    $routeSetup
);

And in your form..

public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder
        ->addEventListener(FormEvents::PRE_SET_DATA, function(FormEvent $event) {
            $form = $event->getForm();
            /** @var RouteSetup $routeSetup */
            $routeSetup = $event->getData();

            if (null === $routeSetup) {
                throw new \Exception('RouteSetup must be injected into form');
            }

            $form
                ->add('countries', 'choice', array(
                    'choices' => $routeSetup->getCountries(),
                ))
            ;
        })
    ;
}

这篇关于symfony2 - 从数据库添加选择的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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