[Symfony2] Insertion d'objet et de collections
Bonjour à tous !
J’essaie d'insérer en base dans Symfony, un objet de ma conception, je dispose d'une table de paramétrage, permettant de lié cet objet à un autre objet (issue de la même entité). Cette relation peut-être effectué plusieurs fois et ajoute également une date de création en base, la relation ManyToMany est donc impossible.
Mon soucis est que lors de la validation de mon formulaire, si j'ajoute une collection de paramètres comme je le souhaite, mes paramètres se lient à un l'entité en cours de création qui a pour ID 'Null' résultat, rien ne vas plus !
J'ai tenté de chercher du côté des Pre/PostPersist sans grand succès, quelqu'un aurait-il une idée ?
(Je précise que mon code fonctionne parfaitement dans le cas d'une édition d'un objet déjà créé)
Voici mon entité simplifiée (J'ai retiré la plupart des champs qui ne posent pas de soucis)
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
| /* Mon namespace */;
use Doctrine\ORM\Mapping as ORM;
/**
* Mon fichier
*
* @ORM\Entity(repositoryClass=" Vers mon Repo ")
* @ORM\Table(name="`Ma table `")
*/
class CertificationV2
{
/**
* @var integer
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @var ReplaceCertificationV2 $replace_certificationV2
* @ORM\OneToMany(targetEntity="Chemin correct vers mon entité", mappedBy="certificationV2", cascade={"all"})
*/
protected $replace_certificationV2;
/* CONSTRUCTEUR */
public function __construct()
{
$this->is_delete = false;
$this->updated_at = new \DateTime();
$this->created_at = new \DateTime();
}
public function delete()
{
$this->setDeletedAt(new \DateTime());
$this->setIsDelete(true);
}
public function update()
{
$this->setUpdatedAt(new \DateTime());
}
/**
* Add associated_certificationV2
*
* @param \chemin correct vers mon entité $replaceCertificationV2
* @return CertificationV2
*/
public function addReplaceCertificationV2(\Ella\CertificationsBundle\Entity\ReplaceCertificationV2 $replaceCertificationV2)
{
$replaceCertificationV2->setCertificationV2($this);
$this->replace_certificationV2[] = $replaceCertificationV2;
return $this;
}
/**
* Remove replace_certificationV2
* @param \chemin correct vers mon entité $replaceCertificationV2
*/
public function removeReplaceCertificationV2(\Ella\CertificationsBundle\Entity\ReplaceCertificationV2 $replaceCertificationV2)
{
$this->replace_certificationV2->removeElement($replaceCertificationV2);
}
/**
* Get replace_certificationV2
* @return \Doctrine\Common\Collections\Collection
*/
public function getReplaceCertificationV2()
{
return $this->replace_certificationV2;
}
} |
Viens ensuite ma seconde entité, (utilisée pour ma table de paramétrage)
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 107 108 109 110 111 112 113 114 115 116 117 118 119
| /* Mon namespace */;
use Doctrine\ORM\Mapping as ORM;
/**
* Mon fichier
*
* @ORM\Entity(repositoryClass=" Vers mon Repo ")
* @ORM\Table(name="`Ma table `")
*/
class ReplaceCertificationV2
{
/**
* @ORM\Id
* @ORM\ManyToOne(targetEntity="Chemin correct", inversedBy="ReplaceCertificationV2")
* @ORM\JoinColumn(nullable=false)
*/
private $certificationV2;
/**
* @ORM\Id
* @ORM\ManyToOne(targetEntity="Chemin correct", inversedBy="ReplaceCertificationV2")
* @ORM\JoinColumn(nullable=false)
*/
private $replace_certificationV2;
/**
* @var \DateTime
*
* @ORM\Column(name="created_at", type="datetime")
*/
private $created_at;
/* CONSTRUCTEUR */
public function __construct()
{
$this->created_at = new \DateTime();
}
/* TO STRING */
public function __toString()
{
return $this->getItem();
}
/**
* Set certificationV2
* @param \Chemin correct $certificationV2
* @return ReplaceCertificationV2
*/
public function setCertificationV2(\Chemin correct $certificationV2)
{
$this->certificationV2 = $certificationV2;
return $this;
}
/**
* Get certificationV2
* @return \Chemin correct
*/
public function getCertificationV2()
{
return $this->certificationV2;
}
/**
* Set replace_certificationV2
*
* @param \Chemin correct
* @return ReplaceCertificationV2
*/
public function setReplaceCertificationV2(\Chemin correct $replaceCertificationV2)
{
$this->replace_certificationV2 = $replaceCertificationV2;
return $this;
}
/**
* Get replace_certificationV2
*
* @return \Chemin correct
*/
public function getReplaceCertificationV2()
{
/*if(isset($this))
{
var_dump($this);
exit;
}*/
return $this->replace_certificationV2;
}
/**
* Set created_at
*
* @param \DateTime $createdAt
* @return ReplaceCertificationV2
*/
public function setCreatedAt($createdAt)
{
$this->created_at = $createdAt;
return $this;
}
/**
* Get created_at
*
* @return \DateTime
*/
public function getCreatedAt()
{
return $this->created_at;
}
} |
Et enfin la partie Controller(Simplifiée également)
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
| $em = $this->getDoctrine()->getManager();
$is_edit = false;
$certification = new CertificationV2();
$form = $this->createForm(new CertificationV2Type($is_edit), $certification);
$request = $this->getRequest();
if ($request->getMethod() == 'POST') {
$form->bind($request);
if($form->isValid()) {
$certification->update();
$em->persist($certification);
$em->flush();
return new RedirectResponse($this->getSessionReturn());
}
} |
Voila Voila, je ne sais que dire de plus ! J'espère que quelqu'un connaîtra une solution miracle ^^ Je connais mon problème mais je n'arrive pas à le résoudre ><
Merci d'avance !
Krishnak