Forms ¿;如何在细枝中呈现集合或嵌入表单?

Forms ¿;如何在细枝中呈现集合或嵌入表单?,forms,symfony,collections,render,Forms,Symfony,Collections,Render,我希望你能帮我解决以下问题 我想呈现一个嵌入的表单,但没有做到,他们会很乐意告诉我怎么做?。 我感谢你的帮助 问题是:当我保存数据时,这些数据被插入到数据库中,但是category_id字段中的Product表被插入NULL 类别类型 class CategoryType extends AbstractType { /** * @param FormBuilderInterface $builder * @param array $options */ public function b

我希望你能帮我解决以下问题

我想呈现一个嵌入的表单,但没有做到,他们会很乐意告诉我怎么做?。 我感谢你的帮助


问题是:当我保存数据时,这些数据被插入到数据库中,但是category_id字段中的Product表被插入NULL

类别类型

class CategoryType extends AbstractType
{
/**
 * @param FormBuilderInterface $builder
 * @param array $options
 */
public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder
        ->add('name')            
        ->add('products', 'collection', array(
            'type'         => new ProductType(),
            'allow_add' => true,
            'allow_delete' => true,
            'prototype' => true,
            'by_reference' => false,
    ))
    ;
}

/**
 * @param OptionsResolverInterface $resolver
 */
public function configureOptions(OptionsResolver $resolver)
{
    $resolver->setDefaults(array(
        'data_class' => 'DeteccionBundle\Entity\Category'
    ));
}


/**
 * @return string
 */
public function getName()
{
    return 'category';
}
}
class ProductType extends AbstractType
{
/**
 * @param FormBuilderInterface $builder
 * @param array $options
 */
public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder
        ->add('name', 'text')
        ->add('price')
        //->add('category')
    ;
}

/**
 * @param OptionsResolverInterface $resolver
 */
public function configureOptions(OptionsResolver $resolver)
{
    $resolver->setDefaults(array(
        'data_class' => 'DeteccionBundle\Entity\Product'
    ));
}

/**
 * @return string
 */
public function getName()
{
    return 'product';
}
}
产品类型

class CategoryType extends AbstractType
{
/**
 * @param FormBuilderInterface $builder
 * @param array $options
 */
public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder
        ->add('name')            
        ->add('products', 'collection', array(
            'type'         => new ProductType(),
            'allow_add' => true,
            'allow_delete' => true,
            'prototype' => true,
            'by_reference' => false,
    ))
    ;
}

/**
 * @param OptionsResolverInterface $resolver
 */
public function configureOptions(OptionsResolver $resolver)
{
    $resolver->setDefaults(array(
        'data_class' => 'DeteccionBundle\Entity\Category'
    ));
}


/**
 * @return string
 */
public function getName()
{
    return 'category';
}
}
class ProductType extends AbstractType
{
/**
 * @param FormBuilderInterface $builder
 * @param array $options
 */
public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder
        ->add('name', 'text')
        ->add('price')
        //->add('category')
    ;
}

/**
 * @param OptionsResolverInterface $resolver
 */
public function configureOptions(OptionsResolver $resolver)
{
    $resolver->setDefaults(array(
        'data_class' => 'DeteccionBundle\Entity\Product'
    ));
}

/**
 * @return string
 */
public function getName()
{
    return 'product';
}
}
这是我的模板小枝:

{% block body -%}



{{ form_start(form) }}
    {# {{ form(form) }} #}
    {{ form_widget(form) }}
{{ form_end(form) }}


<a href="#" id="add-another-email">Añadir otro producto</a>
</ul>
{% endblock %}

{% block javascripts %}
{{ parent() }}
<script>
// keep track of how many email fields have been rendered
var emailCount = '{{ form.products|length }}';

jQuery(document).ready(function() {
    jQuery('#add-another-email').click(function(e) {
        e.preventDefault();

        var productList = jQuery('#category_products');

        // grab the prototype template
        var newWidget = productList.attr('data-prototype');
        // replace the "__name__" used in the id and name of the prototype
        // with a number that's unique to your products
        // end name attribute looks like name="contact[products][2]"
        newWidget = newWidget.replace(/__name__/g, emailCount);
        emailCount++;

        // create a new list element and add it to the list
        var newLi = jQuery('<li></li>').html(newWidget);
        newLi.appendTo(productList);
    });
})
</script>
{% endblock %}
{%block body-%}
{{form_start(form)}}
{{{form(form)}}}
{{form_widget(form)}
{{form_end(form)}}

{%endblock%}
{%block javascripts%}
{{parent()}}
//跟踪已呈现的电子邮件字段数
var emailCount='{form.products | length}}}';
jQuery(文档).ready(函数(){
jQuery(“#添加另一封电子邮件”)。单击(函数(e){
e、 预防默认值();
var productList=jQuery(“#类别#u产品”);
//抓取原型模板
var newWidget=productList.attr('data-prototype');
//替换原型的id和名称中使用的“_名称”
//具有您产品独有的编号
//“结束名称”属性类似于name=“联系人[产品][2]”
newWidget=newWidget.replace(/\uuuuu name\uuuuu/g,emailCount);
emailCount++;
//创建一个新的列表元素并将其添加到列表中
var newLi=jQuery('
  • ').html(newWidget); 附录(产品列表); }); }) {%endblock%}
    请注意,
    选项ResolvePrinterface
    自2.6版以来已被弃用。您应该改用
    选项Resolver
    。另外,你读过关于表单集合的烹饪书文章吗?是的,认识和改变,但仍然不起作用!您到底遇到了什么错误?当我保存数据时,这些错误会被插入到数据库中,但在category_id字段中的Product表会插入NULL。