<i id='AMete'><tr id='AMete'><dt id='AMete'><q id='AMete'><span id='AMete'><b id='AMete'><form id='AMete'><ins id='AMete'></ins><ul id='AMete'></ul><sub id='AMete'></sub></form><legend id='AMete'></legend><bdo id='AMete'><pre id='AMete'><center id='AMete'></center></pre></bdo></b><th id='AMete'></th></span></q></dt></tr></i><div id='AMete'><tfoot id='AMete'></tfoot><dl id='AMete'><fieldset id='AMete'></fieldset></dl></div>
<tfoot id='AMete'></tfoot>

    • <bdo id='AMete'></bdo><ul id='AMete'></ul>
    1. <small id='AMete'></small><noframes id='AMete'>

    2. <legend id='AMete'><style id='AMete'><dir id='AMete'><q id='AMete'></q></dir></style></legend>
    3. symfony2 - 从数据库中添加选择

      时间:2023-10-03
        <tbody id='LVATA'></tbody>
    4. <small id='LVATA'></small><noframes id='LVATA'>

      <i id='LVATA'><tr id='LVATA'><dt id='LVATA'><q id='LVATA'><span id='LVATA'><b id='LVATA'><form id='LVATA'><ins id='LVATA'></ins><ul id='LVATA'></ul><sub id='LVATA'></sub></form><legend id='LVATA'></legend><bdo id='LVATA'><pre id='LVATA'><center id='LVATA'></center></pre></bdo></b><th id='LVATA'></th></span></q></dt></tr></i><div id='LVATA'><tfoot id='LVATA'></tfoot><dl id='LVATA'><fieldset id='LVATA'></fieldset></dl></div>

        <bdo id='LVATA'></bdo><ul id='LVATA'></ul>
        <tfoot id='LVATA'></tfoot>
              <legend id='LVATA'><style id='LVATA'><dir id='LVATA'><q id='LVATA'></q></dir></style></legend>

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

                问题描述

                我希望用来自自定义查询的值填充 symfony2 中的选择框.我尽量简化.

                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.

                控制器

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

                选择路由表单

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

                然后在您的表单中..

                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+ 进行更新(和改进)

                首先,除非将国家/地区存储在数据库中,否则您实际上并不需要将它们作为路线对象的一部分传入.

                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.

                使用选项

                在控制器中..

                $chooseRouteForm = $this->createForm(
                    ChooseRouteForm::class,
                    // Or the full class name if using < php 5.5
                    $routeSetup,
                    array('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'))
                    ;
                }
                

                使用监听器(如果模型中的国家/地区数组可用)

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

                在控制器中..

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

                在你的表格中..

                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 - 从数据库中添加选择的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                上一篇:如何使用 php 预先选择一个 html 下拉列表? 下一篇:php下拉菜单填充

                相关文章

                最新文章

              1. <i id='ngvUh'><tr id='ngvUh'><dt id='ngvUh'><q id='ngvUh'><span id='ngvUh'><b id='ngvUh'><form id='ngvUh'><ins id='ngvUh'></ins><ul id='ngvUh'></ul><sub id='ngvUh'></sub></form><legend id='ngvUh'></legend><bdo id='ngvUh'><pre id='ngvUh'><center id='ngvUh'></center></pre></bdo></b><th id='ngvUh'></th></span></q></dt></tr></i><div id='ngvUh'><tfoot id='ngvUh'></tfoot><dl id='ngvUh'><fieldset id='ngvUh'></fieldset></dl></div>
              2. <legend id='ngvUh'><style id='ngvUh'><dir id='ngvUh'><q id='ngvUh'></q></dir></style></legend>

                  <bdo id='ngvUh'></bdo><ul id='ngvUh'></ul>
              3. <tfoot id='ngvUh'></tfoot>

                <small id='ngvUh'></small><noframes id='ngvUh'>