表单提交后,Symfony使用查询构建器更改下拉列表值

时间:2017-09-22 14:55:37

标签: forms symfony events query-builder event-listener

我想根据我在之前的4个下拉列表中做出的选择来过滤下拉列表。我的表单类型如下:

class DocumentDeactivationType extends AbstractType {

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

    $builder
    ->add('type', 'choice', array('choices' => array(
        'document_types.contract' => 1,
        'document_types.general'=>2,
        'document_types.goodwill_policy'=>3,
        'document_types.pricesheet'=>4,
        'document_types.yq_update'=>5,
        'document_types.contract_addendum'=>6),
        'choices_as_values' => true, 'label' => 'label.types',
        'expanded' => false, 'multiple' => true,
        'label' => 'label.type', 'required' => false,
        'translation_domain' => 'Documents'))

    -> add('status', 'entity', array(
      'class' => 'DocumentBundle:Status', 'property' => 'name',
      'choice_label' => 'translationkey', 'label' => 'Status',
      'expanded' => false, 'multiple' => true, 'required' => false,
      'translation_domain' => 'Documents',
      'choice_translation_domain' => 'Documents',))


    ->add('airlines', 'entity', array(
      'class' => 'AppBundle:Airline', 'property' => 'id',
      'query_builder' => function (EntityRepository $er){
       return $er->createQueryBuilder('a')
       ->addOrderBy('a.id', 'ASC');
       },
      'choice_value' => 'id',
      'choice_label' => 'id', 'label' => 'label.airlines',
      'expanded' => false, 'multiple' => true, 'required' => false,
      'translation_domain' => 'Documents'))

    ->add('markets', 'entity', array(
        'class' => 'AppBundle:Market', 'property' => 'id',
        'query_builder' => function (EntityRepository $er){
         return $er->createQueryBuilder('m')
         ->addOrderBy('m.id', 'ASC');
         },
        'choice_value' => 'id',
        'choice_label' => 'id', 'label' => 'label.markets',
        'expanded' => false, 'multiple' => true, 'required' => false,
        'translation_domain' => 'Documents'))

    ->add('documentlist', EntityType::class, array(
        'class' => 'DocumentBundle:Document',
        'property' => 'name',
        'expanded' => false, 'multiple' => true,
        'label' => 'label.document_list',
        'empty_value' => "Select document",
        'required' => false,
        'mapped' => false,
        'translation_domain' => 'Documents'));

  $builder->addEventListener(FormEvents::PRE_SUBMIT, function (FormEvent $event) use ($builder)
      {
        $form = $event->getForm();
        $data = $event->getData();
        $markets = $data['markets'];
        $status = $data['status'];
        $type = $data['type'];
        $airlines = $data['airlines'];
    $builder
    ->add('documentlist', EntityType::class, array(
        'class' => 'DocumentBundle:Document',
        'property' => 'name',
        'expanded' => false, 'multiple' => true,
        'label' => 'label.document_list',
        'empty_value' => "Select document",
        'required' => false,
        'mapped' => false,
        'translation_domain' => 'Documents',

        'query_builder' => function (EntityRepository $er) use ($markets, $status, $type, $airlines){
        return $er->createQueryBuilder('e')
        ->where('e.markets IN (:markets)')
        ->andWhere('e.status IN (:status)')
        ->andWhere('e.airlines IN (:airlines)')
        ->andWhere('e.products IN (:products)')
        ->setParameter('markets', $markets)
        ->setParameter('status', $status)
        ->setParameter('airlines', $airlines)
        ->setParameter('type', $type);
      },
      ));
  });
}
    public function getName()
    {
        return 'document_deactivation';

    }
  }

我不确定它是否显而易见:'documentlist'下拉列表已经包含了来自文档实体的所有值以及我尝试通过PRE_SUBMIT EventListener实现的更新其值依赖于来自其他4个下拉菜单。 4个下拉菜单有一个“应用”按钮,所以我不需要ajax请求,但我猜是Post / PreSubmit事件? 我的问题:

  1. 从“过滤器”下拉列表中选择某些值时,例如忽略“市场”,我得到错误“未定义的市场指数”,这可能是因为行市场= $ data ['markets'] - >所以我想要的是能够只过滤某些类型,而不是被迫选择所有类型。
  2. 更新文档列表下拉列表根本不起作用。我甚至不认为,查询构建器正在做任何事情。但我不确定这是否与我的FormEvent有关,也可能与我在表单中添加下拉列表的方式有关。
  3. 如何在我的树枝文件中呈现所有内容:

        {% block filterContent %}
      {{ form_start(form) }}
      {{ form_row(form.type) }}
      {{ form_row(form.status) }}
      {{ form_row(form.markets)}}
      {{ form_row(form.airlines)}}
        <input type="submit" class="btn-primary btn btn-xs" value="Apply Filter" />
    
      <br clear="all" />
    {% endblock %}
    
    {% block content %}
    
    {{ form_label(form.documentlist) }}
    {{ form_widget(form.documentlist) }}
    {{ form_end(form) }} </br>
    
    <div class="row">
      {% include 'AppBundle::HelpSubmitButton.html.twig' with { 'buttonName': 'label.submit'|trans } %}
    </div>
    {% endblock content %}
    

1 个答案:

答案 0 :(得分:0)

1)在$ market附近添加if语句

if (isset($data['markets'])) {$markets = $data['markets'];}

'query_builder' => function (EntityRepository $er) use ($markets, $status, $type, $airlines) {
    $query =  $er->createQueryBuilder('e')
    ->where('e.status IN (:status)')
    ->andWhere('e.airlines IN (:airlines)')
    ->andWhere('e.products IN (:products)')
    ->setParameter('markets', $markets)
    ->setParameter('status', $status)
    ->setParameter('airlines', $airlines)
    ->setParameter('type', $type);
  };

   if (isset($data['markets'])) {
     $query->andWhere('e.markets IN (:markets)')
   }

2)我相信您需要通过AJAX更新列表,因为PRE_SUBMIT将在submit操作之前发生,因此它将使用documentlist中的所有值。