繁体   English   中英

Symfony2删除集合中的项目

[英]Symfony2 remove item in the collection

我使用以下链接开发了一组表单:

http://symfony.com/doc/current/cookbook/form/form_collections.html

我设法在收藏中添加了新商品,但无法将其删除。 这是我的代码:

/**
 * My Controller
 */
public function editAction($id, Request $request)
{
    $em = $this->getDoctrine()->getManager();

    $content = $em->getRepository('MyAppBundle:Content')->find($id);

    if (!$content ) {
        throw $this->createNotFoundException('Unable to find Content entity.');
    }

    $originalEpisodes = new ArrayCollection();

    foreach ($content->getEpisodes() as $episode) {
        $originalEpisodes->add($episode);
    }
    $editForm   = $this->createEditForm($content , $this->generateUrl('content_update', array('id' => $content ->getId())), 'PUT', 'Update');


    if ($editForm->isValid()) {

        // remove the relationship between the tag and the Task
        foreach ($originalEpisodes as $episode) {
            if (false === $content->getEpisodes()->contains($episode)) {
                // remove the Task from the Tag
                $episode->getEpisodes()->removeElement($content);

                // if it was a many-to-one relationship, remove the relationship like this
                $episode->setContent(null);

                //$em->persist($episode);

                // if you wanted to delete the Tag entirely, you can also do that
                 $em->remove($episode);
            }
        }

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


    $deleteForm = $this->createDeleteForm($id);

    return $this->render('BbdBongoAppBundle:Content:edit.html.twig', array(
        'entity'      => $content,
        'edit_form'   => $editForm->createView(),
        'delete_form' => $deleteForm->createView(),
        'tabs'        => $this->tabs,
    ));
}

JavaScript部分

 $(document).ready(function() {
        var items{{ name|capitalize }}Count = {{ form|length }};

        $('#add-{{ name }}-{{ id }}').click(function() {
            {# Get the template to add new value #}
            var valueList       = $('#edit_{{ name }}-{{ id }}');
            var newWidget       = valueList.data('prototype');
            newWidget           = newWidget.replace(/__name__/g, items{{ name|capitalize }}Count);

            items{{ name|capitalize }}Count ++;

            {# Add a new field and a link to delete #}
            var newLi = $("<li></li>").append(newWidget);
            $('#add-{{ name }}-{{ id }}').before(newLi);
            {% if allow_delete %}
                addTagFormDeleteLink($(newLi));
            {% endif %}
            return false;
        });

        {# Add a reference to the removal of existing values #}
        {% if allow_delete %}
            $('#edit_{{ name }}-{{ id }} > li').not('ul.errors li').each(function() {
                addTagFormDeleteLink($(this));
            });
        {% endif %}
    });

    function addTagFormDeleteLink($tagFormLi) {
        var $removeFormA = $('<a href="#">delete this tag</a>');
        $tagFormLi.append($removeFormA);

        $removeFormA.on('click', function(e) {
            // prevent the link from creating a "#" on the URL
            e.preventDefault();

            // remove the li for the tag form
            $tagFormLi.remove();
        });
    }

<ul class="collection" id="edit_{{ name }}-{{ id }}" data-prototype='{{ prototype is defined ? form_widget(prototype)|e('html') : '' }}'>

实体:

 public function addEpisode(Episode $episode)
{
    $this->episodes->add($episode);
    $episode->setContent($this);
    return $this;
}

/**
 * Remove episode
 *
 * @param Episode $episode
 * @return $this
 */
public function removeEpisode(Episode $episode)
{
    $this->episodes->removeElement($episode);
    $episode->getContent($this);
    return $this;
}

/**
 * Get episodes
 *
 * @return Episode[]|\Doctrine\Common\Collections\Collection
 */
public function getEpisodes()
{
    return $this->episodes;
}

我花了超过12个小时的时间,但无法弄清楚为什么添加时删除不起作用?

使用$em->flush(); $em->remove($episode); ,实际上删除对象时是将其添加到要删除的列表中,而不是删除本身。 删除发生在flush

您忘记了使用以下方式将请求映射到表单

$editForm->handleRequest($request);

就在这行之前: if ($editForm->isValid())

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM