$em->persist() ne réagit pas comme prévu
Bonjour à tous!
Je suis en train de finaliser mon aplication sous Symfony2, mais je suis tombé sur un bug.
Si j'ai bien compris, la methode persist() de l'entity manager est censée faire la différence entre une nouvelle entité et une entité déja persistée.
Mon soucis vient de ma zone d'administration. L'orsque j'édite une entrée, il n'édite pas l'entrée en question, mais créé un nouvel enregistrement. Je ne sais pas comment palier à ce problème.
voici mes codes: Controller
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
| <?php
public function editArtAction($id)
{
$rep = $this->getDoctrine()->getEntityManager()->getRepository('AzurCMSBundle:Article');
$article = $rep->find($id);
$form = $this->createForm(new ArticleType, $article);
$request = $this->get('request');
if($request->getMethod() == 'POST')
{
$form->bindRequest($requete);
if($form->isValid())
{
$em = $this->getDoctrine()->getEntityManager();
$rep = $em->getRepository('AzurCMSBundle:Categorie');
$article = $form->getData();
$em->flush();
return $this->redirect($this->generateUrl('AzurCmsBundle_admin'));
}
}
return $this->render('AzurCMSBundle:CMS:addArt.html.twig', array( 'form' => $form->createView()));
}
public function editCatAction($id)
{
$em = $this->getDoctrine()->getEntityManager();
$rep = $em->getRepository('AzurCMSBundle:Categorie');
$categorie = $rep->find($id);
$form = $this->createForm(new CategorieType, $categorie);
$request = $this->get('request');
if($request->getMethod() == 'POST')
{
$form->bindRequest($requete);
if($form->isValid())
{
$categorie = $form->getData();
$em->flush();
return $this->redirect($this->generateUrl('AzurCMSBundle_admin'));
}
}
return $this->render('AzurCMSBundle:CMS:addCat.html.twig', array( 'form' => $form->createView()));
} |
et le code de mes entités.
Article:
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106
| <?php
namespace Azur\CMSBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* Azur\CMSBundle\Entity\Article
*
* @ORM\Table()
* @ORM\Entity(repositoryClass="Azur\CMSBundle\Entity\ArticleRepository")
*/
class Article
{
/**
* @ORM\ManyToOne(targetEntity="Azur\CMSBundle\Entity\Categorie", inversedBy="articles")
* @ORM\JoinColumn(name="categorie", onDelete="set null")
*/
private $categorie;
/**
* @var integer $id
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @var string $titre
*
* @ORM\Column(name="titre", type="string", length=255)
*/
private $titre;
/**
* @var string $texte
*
* @ORM\Column(name="texte", type="string", length=4000)
*/
private $texte;
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set titre
*
* @param string $titre
*/
public function setTitre($titre)
{
$this->titre = $titre;
}
/**
* Get titre
*
* @return string
*/
public function getTitre()
{
return $this->titre;
}
/**
* Set texte
*
* @param string $texte
*/
public function setTexte($texte)
{
$this->texte = $texte;
}
/**
* Get texte
*
* @return string
*/
public function getTexte()
{
return $this->texte;
}
public function setCategorie(\Azur\CMSBundle\Entity\Categorie $categorie)
{
$this->categorie = $categorie;
$categorie->addArticles($this);
}
public function getCategorie()
{
return $this->categorie;
}
} |
Categorie:
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
| <?php
namespace Azur\CMSBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* Azur\CMSBundle\Entity\Categorie
*
* @ORM\Table()
* @ORM\Entity(repositoryClass="Azur\CMSBundle\Entity\CategorieRepository")
*/
class Categorie
{
/**
* @ORM\OneToMany(targetEntity="Azur\CMSBundle\Entity\Article", mappedBy="categorie", cascade={"remove"})
*/
private $articles;
/**
* @var integer $id
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @var string $titre
*
* @ORM\Column(name="titre", type="string", length=255)
*/
private $titre;
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set titre
*
* @param string $titre
*/
public function setTitre($titre)
{
$this->titre = $titre;
}
/**
* Get titre
*
* @return string
*/
public function getTitre()
{
return $this->titre;
}
public function getArticles()
{
return $this->articles;
}
public function addArticles(\Azur\CMSBundle\Entity\Article $article)
{
$this->articles[] = $article;
}
} |
Je ne sais pas si ce soucis a un rapport avec ma relation entre mes entités, c'est pourquoi je vous les donne tout de même.
Quelqu'un a une idée de comment régler ce soucis??
Merci d'avance pour vos réponses
Simcos