我使用以下链接开发了一个表单集合:
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个小时,但是无法弄清楚为什么在添加工作时删除无法正常工作?
答案 0 :(得分:0)
在$em->flush();
之后使用$em->remove($episode);
,当您删除对象时,实际上您要将其添加到要删除的列表中而不是删除本身。删除发生在flush
。
答案 1 :(得分:0)
您忘记使用
将请求映射到表单$editForm->handleRequest($request);
就在这一行之前:if ($editForm->isValid())