Symfony 2 - 无法使用集合

时间:2015-09-30 13:27:45

标签: symfony doctrine-orm

我正在努力解决一些symfony 2形式问题。在尝试保存表单时,我收到以下消息:

  

通过这种关系找到了一个新的实体   'Org \ CoreBundle \ Entity \ CustomLists#customListElements'不是   配置为级联实体的持久化操作:   组织\ CoreBundle \实体\ CustomListElement @ 000000007db26fee000000007e19f46a。   解决此问题:显式调用EntityManager#persist()on   这个未知的实体或配置级联持久存在此关联   映射例如@ManyToOne(..,cascade = {“persist”})。

我在集合元素上调用了persist()方法,并在我的实体中添加了cascade = {“persist”},在这里的类似线程中建议使用,但它没有帮助。

CustomLists.php:

 /**
     * @var \Doctrine\Common\Collections\Collection
     * @ORM\OneToMany(targetEntity="CustomListElement", mappedBy="customList", cascade={"persist"})
     */
    private $customListElements;

/**
     * Add customListElement
     *
     * @param \Org\CoreBundle\Entity\CustomListElement $customListElement
     *
     * @return CustomLists
     */
    public function addCustomListElement(\Org\CoreBundle\Entity\CustomListElement $customListElement)
    {
        $this->customListElements[] = $customListElement;

        return $this;
    }

    /**
     * Remove customListElement
     *
     * @param \Org\CoreBundle\Entity\CustomListElement $customListElement
     */
    public function removeCustomListElement(\Org\CoreBundle\Entity\CustomListElement $customListElement)
    {
        $this->customListElements->removeElement($customListElement);
    }

CustomListElement.php:

 /**
     * 
     * @ORM\ManyToOne(targetEntity="CustomLists", inversedBy="customListElements", cascade={"persist"})
     * @ORM\JoinColumn(name="custom_list", referencedColumnName="id_custom_list", nullable=false)
     */
    private $customList;

  /**
     * Set customList
     *
     * @param \Org\CoreBundle\Entity\CustomLists $customList
     * 
     * @return CustomListElement
     */

    public function setCustomList(\Org\CoreBundle\Entity\CustomLists $customList = null)
    {
        $this->customList = $customList;

        return $this;
    }

CustomListType.php:

    $builder->add('customListElements', 'collection', array(
            'type' => new CustomListElementType(),
            'label' => false,
            'allow_add' => true,
            'allow_delete' => true,
            'prototype' => true,
            'attr' => array('class' => 'form-control')
    ));

位指示:

   if ($form->isValid()) {
        $data = $form->getData();

        $customListElements = $data->getCustomListElements();
         foreach($customListElements as $element){
            $element->setCustomList($data);
            $em->persist($element);
            $em->flush();
        }       

        $em->persist($data);
        $em->flush();
    }

我真的不知道,我做错了什么。我会感谢任何有用的答案。

编辑: 这是我的表单代码:

{% extends 'OrganizerBundle::layout.html.twig' %}

{% block title %}{{ title }}{% endblock %}

{% block header %}
{{ parent() }}
{% endblock %}

{% block content %}
    <div class="col col-md-8">
        <div class="panel panel-success custom-list-form">
          <div class="panel-heading">
            <h3 class="panel-title">{{ title }}</h3>
          </div>
          <div class="panel-body">
            {{ form_start(form) }}

                {{ form_row(form.listName) }}

                <h3>Elementy</h3>

                <ul class="elements" 
                data-prototype="{% filter escape %}{% include 'OrganizerBundle:CustomLists:elementTemplate.html.twig'  %}{% endfilter %}">
                {% for tag in form.customListElements %}
                    <li>
                    <div class="custom-list-element">
                        <div id="customList_customListElements___name__">
                            <div>
                                 {{ form_row(tag.elementName) }}
                            </div>
                            <div>
                                {{ form_row(tag.elementDescription) }}
                            </div>
                            <input type="hidden" id="customList_customListElements___name___elementOrder"
                                name="customList[customListElements][__name__][elementOrder]" />
                        </div>
                        <div class="element-buttons">
                            <i class="fa fa-times remove-element" title="{{ 'Usuń'|trans }}" data-placement="left" data-toggle="tooltip"></i>
                        </div>

                        </div>
                    </li>
                {% endfor %}

                </ul>
                {% do form.customListElements.setRendered %}
                <div class="form-buttons">{{ form_widget(form.zapisz) }}</div>
            {{ form_end(form) }}
          </div>
        </div>
    </div>
    <script>
    var $collectionHolder;

    var addElemText = '{{ 'Dodaj element'|trans }}';
    var $newElementButton = $('<a href="#" class="add_element_link"><button type="button" class="btn btn-primary btn-sm">'+ 
            addElemText +'</button></a>');
    var $newLinkLi = $('<li></li>');

    jQuery(document).ready(function() {

        $collectionHolder = $('.elements');

        $newElementButton.insertBefore($collectionHolder);
        $collectionHolder.data('index', $collectionHolder.find(':input').length);

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

        function addElementForm($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);
            $collectionHolder.append($newFormLi);
        }

    });
    </script>
{% endblock %}

和我的elementTemplate.html.twig原型模板:

<div class="listBox" data-pk="{{ element.idCustomList }}">
    <input type="checkbox" class="list-checkbox"/>
    <div class="custom-list-name">{{ element.listName }}</div>
    <div class="list-elem-count">{{ 'elementów:'|trans ~ '...' }}</div>
    <div class="list-elem-created">
        <span class="created-text">{{ 'utworzono:'|trans }}</span>
        <div class="date-created">{{ '...'  }}</div>
    </div>
</div>

编辑2: 当我在进行doctrine:schema:validate时,也会出现错误:

  

[Mapping] FAIL - 实体类'Org \ CoreBundle \ Entity \ CustomLists'   映射无效:   *字段Org \ CoreBundle \ Entity \ CustomLists#customListElements位于双向关系的反面,但是指定的   mappedBy在目标实体上的关联   Org \ CoreBundle \ Entity \ CustomListElement#customList不包含   必需的'inversedBy =“customListElements”'属性。

1 个答案:

答案 0 :(得分:1)

您是否在CustomLists.php的构造函数中添加了这一行:

$customListElements = new ArrayCollection();

在OneToMany声明中将级联更改为all。

/**
     * @var \Doctrine\Common\Collections\Collection
     * @ORM\OneToMany(targetEntity="CustomListElement", mappedBy="customList", cascade={"all"})
     */
    private $customListElements;

没有必要读取所有$ customListElements并手动添加它。 Doctrine会自动执行此操作,因为他知道由于OneToMany,CustomLists与CustomListElement有关系。

你可以改变你的控制器:

$entity = new CustomLists();
$form = $this->createForm(new CustomListType(), $entity);
$form->handleRequest($request); 

if ($form->isValid()) {
       $em->persist($entity);
       $em->flush();    
}

希望有所帮助