Javascript Symfony2删除集合中的项目
我使用以下链接开发了一个表格集合: 我设法在收藏中添加了新项目,但无法删除它们。这是我的密码:Javascript Symfony2删除集合中的项目,javascript,symfony,Javascript,Symfony,我使用以下链接开发了一个表格集合: 我设法在收藏中添加了新项目,但无法删除它们。这是我的密码: /** * My Controller */ public function editAction($id, Request $request) { $em = $this->getDoctrine()->getManager(); $content = $em->getRepository('MyAppBundle:Content')->find($id
/**
* 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') : '' }}'>
我花了12个多小时,但不明白为什么在添加工作时删除不工作?使用$em->flush;在$em->删除$eposion之后;,删除对象时,实际上是将其添加到要删除的列表中,而不是删除本身。删除发生在刷新时。您忘记将请求映射到具有
$editForm->handleRequest($request);
就在这行前面:如果$editForm->isValid它仍然不能工作。。还有其他线索吗??我正确地遵循了symfony医生,但仍然不明白原因是什么。在我的内容中,我有许多需要删除选项的剧集。嗨,你能展示一下你是如何添加元素的吗
$editForm->handleRequest($request);