Symfony填充父类别更改的子类别下拉列表

时间:2017-06-05 18:23:21

标签: php symfony

对此有很多疑问,但我无法让它们工作,因为我正在使用Symfony 3.3。

我有一个父类别下拉列表以及当用户选择一个类别然后将子类别从数据库填充到子类别下拉列表时我想要做的事情。 这是好documentation,但我坚持让它发挥作用。

的问题:

  • 我更改父类别并保存到数据库时提交的表单。
  • 我无法在Producttype.php中获取父类别ID,我正在使用查询构建器从数据库中检索子猫

ProductType.php

 $builder->add('parentCat', EntityType::class, array(
        'class' => 'AdminBundle:ProductCategory',
        'query_builder' => function (EntityRepository $er) {
            return $er->createQueryBuilder('c')
                ->where('c.parentId is NULL');
        },
        'choice_label' => 'title',
        'placeholder' => 'Choose parent category',
        'required' => true,
    ));

         $builder->add('subCat', EntityType::class, array(
        'class' => 'AdminBundle:ProductCategory',
        'placeholder' => 'Choose sub category',
        'choices'=>array()
    ));

    $builder->addEventListener(FormEvents::PRE_SET_DATA, function (FormEvent $event) {
        $product = $event->getData();
        $form = $event->getForm();
         if ($product && null !==  $product->getParentCat()) {
            $id = $product->getParentCat();
            $form->add('subCat', EntityType::class, array(
                'class' => 'AdminBundle:ProductCategory',
                'query_builder' => function (EntityRepository $er) {
                    return $er->createQueryBuilder('c')
                    ->where('c.parentId = ?'.$id);
                },
                'choice_label' => 'title',
                'placeholder' => 'Choose sub category'
            ));
        }
    });
...

create.html.twig

    <div class="form-group">
                {{ form_label(form.parentCat, 'Category') }}
                {{ form_errors(form.parentCat) }}
                {{ form_widget(form.parentCat, {'attr': {'class': 'form-control'}}) }}
            </div>
    <div class="form-group">
            {{ form_label(form.subCat, 'Sub Category') }}
            {{ form_errors(form.subCat) }}
            {{ form_widget(form.subCat, {'attr': {'class': 'form-control'}}) }}
        </div>

 ...

 {% block javascripts %}

{{ parent() }}

<script>
    var $parentCat = $('#product_parentCat');
    var $csrfTag = $('#product__token');
    $parentCat.change(function() {
      // ... retrieve the corresponding form.
      var $form = $(this).closest('form');
      // Simulate form data, but only include the selected sport value.
      var data = {};
      data[$parentCat.attr('name')] = $parentCat.val();
      data[$csrfTag.attr('name')] = $csrfTag.val();
      // Submit data via AJAX to the form's action path.
      console.log(data);
      $.ajax({
        url : $form.attr('action'),
        type: $form.attr('method'),
        data : data,
        success: function(html) {
            console.log("res",html);
          // Replace current product_subCat field ...
          $('#product_subCat').replaceWith(
            // ... with the returned one from the AJAX response.
            $(html).find('#product_subCat')
          );
          // product_subCat field now displays the appropriate sub cats.
        }
      });
    });
</script>

{% endblock %}

0 个答案:

没有答案