Symfony为ManyToOne关系形成组合

时间:2017-01-03 14:36:55

标签: php jquery forms symfony

我有字段customers - 下拉现有客户。并且有客户,比如CollectionType,'entry_type' => CustomerForm::class,但是我使用了ManyToMany关系的示例,但我的关系ManyToOne和我需要为我的表单更改逻辑,需要在CustomerForm::class中只创建一个客户   我阅读了烹饪书form_collections当我添加新的客户表单时,如CollectionType::class,提交表单时出错

Expected argument of type "AppBundle\Entity\Customer", "array" given

请求中的

economy_bundle_outbound_invoice = {array} [4]
companyReference = "wwwww"
customerReference = "wwww"
customer = {array} [2]
 0 = {array} [1]
  name = "wwww"
 1 = {array} [1]
  name = ""
submit = ""

我理解这是ManyToMany的例子。我的问题是如何创建新发票的表单和按钮,以便在需要时添加新客户

现在我的表单(需要添加来自DB的客户以及下拉列表)

        $builder
        ->add('companyReference')
        ->add('customerReference')

        ->add('customer', CollectionType::class, array(
            'entry_type' => CustomerForm::class,
            'allow_add'    => true,
        ))

        ->add('customers', 'entity', array(
            'class' => Customer::class,
            'attr' => array('class' => 'form-control select2 all_customers'),
            'property' => 'name',
            'empty_value' => 'Choice Customer',
            'query_builder' => function ($repository) {
                /** @var CustomerRepository  $repository */
                return $repository->getAllQuery();
            },
            'required' => false,
            'mapped' => false,
            )
        );

CustomerForm

    class CustomerForm extends AbstractType
   {
        $builder
        ->add('name');

我的模板

{% extends "ERP/economy/economy.html.twig" %}
{% trans_default_domain "invoicing" %}

{% set active_tab = "inbound_invoices" %}
{% block title %}HouseOptima ERP - Outbound Invoices{% endblock %}


{% block tab_content %}


{{ form_start(form) }}
{{ form_row(form.companyReference) }}
{{ form_row(form.customerReference) }}
{{ form_row(form.customers) }}

<h3>Customer</h3>
<ul class="tags" data-prototype="{{ form_widget(form.customer.vars.prototype)|e('html_attr') }}">
    {% for customer in form.customer %}
        <li>{{ form_row(customer.name) }}</li>
    {% endfor %}
</ul>

{{ form_end(form) }}
<script>

    var $collectionHolder;
    var $addTagLink = $('<a href="#" class="add_customer_link">Add a customer</a>');
    var $newLinkLi = $('<li></li>').append($addTagLink);

    jQuery(document).ready(function() {
        $collectionHolder = $('ul.tags');
        $collectionHolder.append($newLinkLi);
        $collectionHolder.data('index', $collectionHolder.find(':input').length);

       $addTagLink.on('click', function(e) {
            e.preventDefault();
            addTagForm($collectionHolder, $newLinkLi);
        });
    });



    $('.all_customers').on('change', function () {
        var dd = $('#economy_bundle_outbound_invoice_customers option:selected').val();

        console.log(dd);
        if (dd == '') {
            $('ul.tags').find('li').show();
        } else {
            $('ul.tags').find('li').hide();
        }
    });

    function addTagForm($collectionHolder, $newLinkLi) {
        var prototype = $collectionHolder.data('prototype');
        var index = $collectionHolder.data('index');
        var newForm = prototype.replace(/__name__/g, index);
        $collectionHolder.data('index', index + 1);
        var $newFormLi = $('<li></li>').append(newForm);
        $newLinkLi.before($newFormLi);
    }
</script>

{% endblock %}

0 个答案:

没有答案