<bdo id='iybLY'></bdo><ul id='iybLY'></ul>

  • <legend id='iybLY'><style id='iybLY'><dir id='iybLY'><q id='iybLY'></q></dir></style></legend>

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

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

        Symfony2 同一类中的多个实体

        Symfony2 multiple Entities of same class in one Form(Symfony2 同一类中的多个实体)

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

            1. <small id='lVz5e'></small><noframes id='lVz5e'>

              • <bdo id='lVz5e'></bdo><ul id='lVz5e'></ul>
                  <legend id='lVz5e'><style id='lVz5e'><dir id='lVz5e'><q id='lVz5e'></q></dir></style></legend>
                    <tbody id='lVz5e'></tbody>

                • <tfoot id='lVz5e'></tfoot>
                  本文介绍了Symfony2 同一类中的多个实体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我想呈现一个具有多个相同类实体的表单.我将显示 2 个字段,Price(type=text) 和 Enabled(type=checkbox).

                  我不知道这些实体有多少,因此表单必须动态获取它们.

                  我已尝试执行以下操作

                  public function buildForm(FormBuilderInterface $builder, array $options){$builder->add('价格', '文本', 数组('标签' =>'价格','必需' =>真的))->add('启用','复选框',数组('标签' =>'使用这种货币',));}公共函数 setDefaultOptions(OptionsResolverInterface $resolver){$resolver->setDefaults(array('data_class' =>'奥西里斯实体定价','csrf_protection' =>错误的));}公共函数 getName(){返回定价类型";}

                  在我的控制器中,我创建了这样的表单:

                  $pricingForm = $this->createFormBuilder($prices)->add('items','collection',array('必需' =>错误的,'原型' =>真的,'类型' =>新定价类型(),))->getForm();

                  在我的树枝上:

                  {% for price in form_pricing %}<h2>价格</h2><div class="row">{{ form_widget(price) }}

                  {% 结束为 %}

                  然而,它只带有 h2 价格和带有 class=row 的空 div.我觉得我已经成功了一半,但我不知道如何继续前进.如果有人也知道如何在提交时获取字段,我将不胜感激.

                  解决方案

                  我找到了解决方案,

                  我在 Controller 中创建表单的方式是错误的!我必须执行以下操作:

                  $pricingForm = $this->createFormBuilder(array('prices'=>$prices))->add('价格','集合',数组('必需' =>真的,'allow_add' =>真的,'类型' =>新定价类型(),))->getForm();

                  "allow_add => true" 在使用集合时是必需的,否则它会将任何 PricingType 实体集合添加到表单中.

                  然后,因为表单是在控制器内部构建的 "$this->createFormBuilder(array('prices'=>$prices))" , $prices 数组必须作为一个数组传递,其数组键名与 "->add('prices','collection',array(...)" 中使用的相同,即 'prices' 这样 Symfony 就会知道在哪里绑定什么.$prices 是一个 Pricing 对象数组 array(0 => new Pricing()).

                  在我的 PricingType 中有:

                  class PricingType 扩展 AbstractType {公共函数 buildForm(FormBuilderInterface $builder, array $options){$builder->add('价格', '文本', 数组('标签' =>错误的,'必需' =>真的))->add('启用','复选框',数组('标签' =>'使用这种货币',));}公共函数 setDefaultOptions(OptionsResolverInterface $resolver){$resolver->setDefaults(array('data_class' =>'XXXXXX实体定价','csrf_protection' =>错误的));}公共函数 getName(){返回定价类型";}}

                  这里我需要控制标签属性.我找不到方法(如果有人知道,请发布方法).我按如下方式覆盖我的树枝模板:

                  在顶部我们需要下一行代码:

                   {% form_theme form_pricing _self %}

                  然后按如下方式覆盖行和小部件(调试是一场噩梦):

                  {% block _form_prices_entry_row %}{% 无空间 %}{{ form_widget(form) }}{% endspaceless %}{% 结束块 %}{% 块 _form_prices_entry_widget %}{% 无空间 %}{{ form_row(form.price, { 'label' : form.vars.value.getCurrency().getTitle() } ) }}{{ form_row(form.enabled) }}{% endspaceless %}{% 结束块 %}

                  然后在正文中,按如下方式渲染表单元素:

                  {% for price in form_pricing.prices %}<div class="price-row">{{ form_row(price) }}</div>{% 结束为 %}

                  我真的希望这对你们有帮助.调试尤其是 twig 文件真是一场噩梦,多亏了我聪明的同事,我才做到了.

                  I want to render a form which has multiple Entities of same Class. I will display 2 fields, Price(type=text) and Enabled(type=checkbox).

                  I don't know how many I will have of those entities, so form will have to get them dynamically.

                  I have tried to do the following

                  public function buildForm(FormBuilderInterface $builder, array $options)
                  {
                  
                          $builder
                              ->add('price', 'text', array(
                                  'label' => 'Price',
                                  'required' => true
                              ))
                              ->add('enabled','checkbox',array(
                                  'label'     => 'Use this currency',
                  
                              ))
                          ;    
                  }
                  
                  public function setDefaultOptions(OptionsResolverInterface $resolver)
                  {
                      $resolver->setDefaults(array(
                          'data_class'        => 'OsirisEntityPricing',
                          'csrf_protection'   => false
                      ));
                  }
                  
                  public function getName()
                  {
                      return 'pricingtype';
                  }
                  

                  And in my Controller I have created my form like this:

                  $pricingForm = $this->createFormBuilder($prices)
                                 ->add('items','collection',array(
                                     'required' => false,
                                     'prototype' => true,
                                     'type'    => new PricingType(),
                                 ))
                                  ->getForm()
                              ;
                  

                  In my twig I do:

                  {% for price in form_pricing %}
                      <h2>Price</h2>
                      <div class="row">{{ form_widget(price) }}</div>
                  {% endfor %}
                  

                  However it comes only with h2 Prices and empty div with class=row. I feel like I am half way there, but I've no idea how to move on. If someone knows how to get fields on submit as well, I will really appreciate it.

                  解决方案

                  I found the solution,

                  the way I was creating the form in Controller was wrong! I had to do the following:

                  $pricingForm = $this->createFormBuilder(array('prices'=>$prices))
                                  ->add('prices','collection',array(
                                      'required'       => true,
                                      'allow_add'      => true,
                                      'type'           => new PricingType(),
                                 ))
                                  ->getForm()
                              ;
                  

                  "allow_add => true" is necessary when working with collection, otherwise it will NOT add any of PricingType collection of entities to the form.

                  Then, because the form is built inside the controller "$this->createFormBuilder(array('prices'=>$prices))" , $prices array must be passed as an array with array keyname same as the one used in "->add('prices','collection',array(...)" , which is 'prices' so Symfony will know what to bind where. $prices is an array of Pricing objects array(0 => new Pricing()).

                  In my PricingType I have:

                  class PricingType extends AbstractType {
                  
                      public function buildForm(FormBuilderInterface $builder, array $options)
                      {
                  
                          $builder
                              ->add('price', 'text', array(
                                  'label' => false,
                                  'required' => true
                              ))
                              ->add('enabled','checkbox',array(
                                  'label'     => 'Use this currency',
                  
                              ))
                          ;
                  
                      }
                  
                      public function setDefaultOptions(OptionsResolverInterface $resolver)
                      {
                          $resolver->setDefaults(array(
                              'data_class'        =>  'XXXXXXEntityPricing',
                              'csrf_protection'   => false
                          ));
                      }
                  
                      public function getName()
                      {
                          return 'pricingtype';
                      }
                  }
                  

                  Here I need to have control over label attribute. I could not find the way for it( if anyone knows please post how to). I override my twig template as follows:

                  On the top we need next line of code:

                      {% form_theme form_pricing _self %}
                  

                  Then override row and widget as follows (it was a nightmare to debug):

                  {% block _form_prices_entry_row %}
                      {% spaceless %}
                          {{ form_widget(form) }}
                      {% endspaceless %}
                  {% endblock %}
                  
                  {% block _form_prices_entry_widget %}
                      {% spaceless %}
                  
                          {{ form_row(form.price, { 'label' : form.vars.value.getCurrency().getTitle() } ) }}
                          {{ form_row(form.enabled) }}
                  
                      {% endspaceless %}
                  {% endblock %}
                  

                  In the body then, render form elements as follows:

                  {% for price in form_pricing.prices %}
                                      <div class="price-row">{{ form_row(price) }}</div>
                                  {% endfor %}
                  

                  I really hope this will help you guyz. It was a real nightmare to debug especially the twig file, I did it thanks to my clever colleague.

                  这篇关于Symfony2 同一类中的多个实体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Is PHP or PHP based web framework stateful or stateless?(PHP 或基于 PHP 的 Web 框架是有状态的还是无状态的?)
                  How to parse django style template tags(如何解析 django 样式模板标签)
                  What is a good setup for editing PHP in Emacs?(在 Emacs 中编辑 PHP 的好设置是什么?)
                  How to check whether specified PID is currently running without invoking ps from PHP?(如何在不从 PHP 调用 ps 的情况下检查指定的 PID 当前是否正在运行?)
                  What#39;s the difference between escapeshellarg and escapeshellcmd?(escapeshellarg 和escapeshellcmd 有什么区别?)
                  php in background exec() function(php 后台 exec() 函数)

                    <tfoot id='QsS99'></tfoot><legend id='QsS99'><style id='QsS99'><dir id='QsS99'><q id='QsS99'></q></dir></style></legend>

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

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