2016-11-04 45 views
1

的jointable條目,我不能讓我的頭圍繞這一點:刪除只在學說多對多

我跟着symfony的菜譜例子,說明如何在symfony的形式處理arraycollections:link

我只改「演出」和「藝術家」的「任務」和「標籤」。 它一切正常,我可以添加藝術家演出,結果連接表看起來不錯。 但是,當涉及到刪除藝術家時,學說不會按照預期刪除連接表條目,但會嘗試將藝術家名稱設置爲null,但無論如何這都不是工作和錯誤。我想保留藝術家,但刪除與演出的關係。 因此,我管理堅持自己,沒有用戶堅持{「級聯」} ..

我做錯了什麼?

我希望,我的問題形成得不錯,我提供了足夠的信息。我試圖尋找這個問題,但找不到解決方案。

謝謝

只是

錯誤:

An exception occurred while executing 'UPDATE artist SET name = ? WHERE id = >?' with params [null, 10]:

SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'name' cannot >be null

gig.php:

/** 
* @var \Doctrine\Common\Collections\ArrayCollection|Artist[] 
* 
* @ORM\ManyToMany(targetEntity="GoetzBundle\Entity\Artist", inversedBy="gigs"), 
* @ORM\JoinTable(name="gigs_artits") 
*/ 
private $artists; 

... 

/** 
* @return ArrayCollection|Artist 
*/ 
public function getArtists() 
{ 
    return $this->artists; 
} 

/** 
* @param Artist $artist 
* @return ArrayCollection|Artist 
*/ 
public function addArtist($artist) 
{ 
    $artist->addGig($this); 
    $this->artists->add($artist); 

    return $this->artists; 
} 

/** 
* @param Artist $artist 
* @return ArrayCollection|Artist 
*/ 
public function removeArtist($artist) 
{ 
    $artist->removeGig($this); 
    $this->artists->removeElement($artist); 

    return $this->artists; 
} 

artist.php

/** 
* @var \Doctrine\Common\Collections\ArrayCollection|Gig[] 
* 
* @ORM\ManyToMany(targetEntity="GoetzBundle\Entity\Gig", mappedBy="artists", fetch="EAGER") 
* 
*/ 
private $gigs; 

... 

/** 
* @param Gig $gig 
* @return ArrayCollection|Gig 
*/ 
public function addGig(Gig $gig) 
{ 
    if (!$this->gigs->contains($gig)) { 
     $this->gigs->add($gig); 
    } 
    return $this->gigs; 
} 

/** 
* @param Gig $gig 
* @return ArrayCollection|Gig 
*/ 
public function removeGig(Gig $gig) 
{ 
    if ($this->gigs->contains($gig)) { 
     $this->gigs->removeElement($gig); 
    } 
    return $this->gigs; 
} 

gigtype.php

... 

$builder 
     ->add('artists', CollectionType::class, array(
      'entry_type' => ArtistType::class, 
      'allow_add' => true, 
      'by_reference' => false, 
      'allow_delete' => true, 
     )) 

... 

gigcontroller.php

public function newAction(Request $request) 
{ 
    $gig = new Gig(); 

    $form = $this->createForm('GoetzBundle\Form\GigType', $gig); 
    $form->handleRequest($request); 

    if ($form->isSubmitted() && $form->isValid()) { 
     $em = $this->getDoctrine()->getManager(); 

     foreach ($gig->getArtists() as $artist){ 
      $em->persist($artist); 
     } 

     $em->persist($gig); 
     $em->flush(); 

     return $this->redirectToRoute('gig_show', array('id' => $gig->getId())); 
    } 

    return $this->render('@Goetz/gig/new.html.twig', array(
     'gig' => $gig, 
     'form' => $form->createView(), 
    )); 
} 

... 

public function editAction(Request $request, Gig $gig) 
    { 
     $deleteForm = $this->createDeleteForm($gig); 

     $em = $this->getDoctrine()->getManager(); 
     $gig = $em->getRepository('GoetzBundle:Gig')->find($gig); 

     $originalArtists = new ArrayCollection(); 

     foreach ($gig->getArtists() as $artist){ 
      $originalArtists->add($artist); 
     } 

     $editForm = $this->createForm('GoetzBundle\Form\GigType', $gig); 
     $editForm->handleRequest($request); 

     if ($editForm->isSubmitted() && $editForm->isValid()) { 

      foreach ($originalArtists as $artist){ 
       if (false === $gig->getArtists()->contains($artist)){ 
        $artist->getGigs()->removeElement($gig); 
       } 
       $em->persist($artist); 
      } 

      $em = $this->getDoctrine()->getManager(); 

      foreach ($gig->getArtists() as $artist){ 
       $em->persist($artist); 
      } 

      $em->persist($gig); 
      $em->flush(); 

      return $this->redirectToRoute('gig_edit', array('id' => $gig->getId())); 
     } 

     return $this->render('Goetz/gig/edit.html.twig', array(
      'gig' => $gig, 
      'edit_form' => $editForm->createView(), 
      'delete_form' => $deleteForm->createView(), 
     )); 
    } 

edit.html.twig

{% block content %} 
    <h1>Gig edit</h1> 

    {{ form_start(edit_form) }} 
    <h3>Artists</h3> 
    <ul class="artists" data-prototype="{{ form_widget(edit_form.artists.vars.prototype)|e('html_attr') }}"> 
     {% for artist in edit_form.artists %} 
      <li>{{ form_row(artist.name) }}</li> 
     {% endfor %} 
    </ul> 
     {{ form_rest(edit_form) }} 
     <input type="submit" value="Edit" /> 
    {{ form_end(edit_form) }} 

    <ul> 
     <li> 
      <a href="{{ path('gig_index') }}">Back to the list</a> 
     </li> 
     <li> 
      {{ form_start(delete_form) }} 
       <input type="submit" value="Delete"> 
      {{ form_end(delete_form) }} 
     </li> 
    </ul> 
{% endblock %} 
{% block javascripts %} 
    {{ parent() }} 
    <script type="text/javascript" src="{{ asset('bundles/goetz/js/artists.js') }}"></script> 
{% endblock %} 

(新的外觀相同)

artists.js

回答

0

您的刪除操作看起來如何?

你能做這樣的事嗎?

public function deleteAction($id) 
{ 
    $em = $this->getDoctrine()->getManager(); 

    $gig = $this->getDoctrine() 
     ->getRepository('GoetzBundle\Entity\Gig') 
     ->findOneById($id); 

    ... 

    foreach ($gig->getArtists() as $artist) { 
     $gig->removeArtist($artist); 
    } 

    $em->persist($gig); 
    $em->flush(); 

    ... 
} 

這應該保留演出和相應的藝術家,並刪除它們之間的連接表中的連接。