Bonjour à tous,

J'ai un soucis au niveau du persist pour une relation bidirectionnelle avec attributs.
J'ai une entité Piste qui est une Personne.
J'ai une relation ManyToMany entre une personne et une fonction, ce qui me donne l'entité PersonneFonction.

Voici mes entités :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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
/**
 * Icme\MainBundle\Entity\Personne
 *
 * @ORM\Table(name="personne")
 * @ORM\Entity(repositoryClass="Icme\MainBundle\Repository\PersonneRepository")
 */
class Personne
{
    /**
     * @var integer $id
     *
     * @ORM\Column(name="id", type="integer", nullable=false)
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="IDENTITY")
     */
    private $id;
 
    /**
     * @var string $nom
     *
     * @ORM\Column(name="nom", type="string", length=50, nullable=false)
     * @Assert\NotBlank(message="Le nom ne peut être vide")
     */
    private $nom;
 
    /**
     * @var string $prenom
     *
     * @ORM\Column(name="prenom", type="string", length=50, nullable=false)
     * @Assert\NotBlank(message="Le prénom ne peut être vide")
     */
    private $prenom;
 
    /**
     * @var string $mail
     *
     * @ORM\Column(name="mail", type="string", length=50, nullable=true)
     * @Assert\NotBlank(message="L'adresse mail ne peut être vide")
     * 
     */
    private $mail;
 
 
    /**
     * @var datetime $datecreation
     *
     * @ORM\Column(name="dateCreation", type="datetime", nullable=false)
     */
    private $datecreation;
 
    /**
     * @var PersonneFonctions
     *
     * @ORM\OneToMany(targetEntity="Icme\MainBundle\Entity\PersonneFonction", mappedBy="personne")
     *
     */
    private $personnefonctions;    
 
 
    public function  __construct()
    {
    	$this->datecreation = new \DateTime('NOW', new \DateTimeZone('Europe/Paris'));
    	$this->personnefonctions = new ArrayCollection();
    }
 
    /**
     * Add personnefonctions
     *
     * @return Icme\MainBundle\Entity\PersonneFonction
     */
    public function addPersonnefonction(\Icme\MainBundle\Entity\PersonneFonction $personnefonctions)
    {
    	$this->personnefonctions[] = $personnefonctions;
    	$personnefonctions->setPersonne($this);
    }
 
    /**
     * Remove personnefonctions
     *
     * @param Icme\MainBundle\Entity\Personnefonction $personnefonction
     */
    public function removePersonnefonction(\Icme\MainBundle\Entity\PersonneFonction $personnefonctions)
    {
    	$this->personnefonctions->removeElement($personnefonctions);
    	$personnefonctions->setPersonne(null);    	 
    }
 
    /**
     * Get personnefonctions
     * @return \Icme\MainBundle\Entity\PersonneFonctions
     */
    public function getPersonnefonctions()
    {
    	return $this->personnefonctions;
    }
 
    public function setPersonnefonctions(array $personnefonctions)
    {
    	$this->personnefonctions = new ArrayCollection($personnefonctions);
  		foreach ($personnefonctions as $personnefonction)
    		$personnefonction->setPersonne($this);
    }
PersonneFonction :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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
/**
 * Icme\MainBundle\Entity\PersonneFonction
 *
 * @ORM\Table(name="personne_fonction")
 * @ORM\Entity
 */
class PersonneFonction
{
	/**
	 * @var Fonction
	 * @ORM\Id
	 * @ORM\ManyToOne(targetEntity="Fonction", inversedBy="personnefonction")
	 * @ORM\JoinColumns({
	 *   @ORM\JoinColumn(name="idFonction", referencedColumnName="id")
	 * })
	 */
	private $fonction;
 
	/**
	 * @var Personne
	 * @ORM\Id
	 * @ORM\ManyToOne(targetEntity="Personne", inversedBy="personnefonction")
	 * @ORM\JoinColumns({
	 * 	@ORM\JoinColumn(name="idPersonne", referencedColumnName="id")
	 * })
	 */
	private $personne;
 
    /**
     * @var datetime $dateaffectation
     *
     * @ORM\Column(name="dateAffectation", type="datetime", nullable=false)
     */
    private $dateaffectation;
 
 
    public function  __construct()
    {
    	$this->dateaffectation = new \DateTime('NOW', new \DateTimeZone('Europe/Paris'));
    }
 
    /**
     * Set dateaffectation
     *
     * @param datetime $dateaffectation
     */
    public function setDateaffectation($dateaffectation)
    {
        $this->dateaffectation = $dateaffectation;
    }
 
    /**
     * Get dateaffectation
     *
     * @return datetime 
     */
    public function getDateaffectation()
    {
        return $this->dateaffectation;
    }
 
    /**
     * Set fonction
     *
     * @param Icme\MainBundle\Entity\Fonction $fonction
     */
    public function setFonction(\Icme\MainBundle\Entity\Fonction $fonction)
    {
        $this->fonction = $fonction;
    }
 
    /**
     * Get fonction
     *
     * @return Icme\MainBundle\Entity\Fonction 
     */
    public function getFonction()
    {
        return $this->fonction;
    }
 
    /**
     * Set personne
     *
     * @param Icme\MainBundle\Entity\Personne $personne
     */
    public function setPersonne(\Icme\MainBundle\Entity\Personne $personne)
    {
        $this->personne = $personne;
    }
 
    /**
     * Get personne
     *
     * @return Icme\MainBundle\Entity\Personne 
     */
    public function getPersonne()
    {
        return $this->personne;
    }
}
Le soucis vient au niveau du persist.

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
 
public function onSuccess(Piste $piste) 
	{
              // on persist les fonctions
		foreach($piste->getPersonne()->getPersonnefonctions() as $pf)
			$this->em->persist($pf->getFonction());
 
		$this->em->flush();
 
		// on persist la personne		
		$this->em->persist($piste->getPersonne());
		$this->em->flush();
 
		// on persist les personnefonctions
		foreach($piste->getPersonne()->getPersonnefonctions() as $pf)
		{
			$this->em->persist($pf);
		}	
		$this->em->flush();
}

J'ai cette erreur :
A new entity was found through the relationship 'Icme\MainBundle\Entity\Personne#personnefonctions' that was not configured to cascade persist operations for entity: Icme\MainBundle\Entity\PersonneFonction@000000003309c69f000000001a6019c6. Explicitly persist the new entity or configure cascading persist operations on the relationship. If you cannot find out which entity causes the problem implement 'Icme\MainBundle\Entity\PersonneFonction#__toString()' to get a clue.


Si je ne flush pas la personne avant le foreach sur les Personnefonctions, j'ai cette erreur :
Entity of type Icme\MainBundle\Entity\PersonneFonction has identity through a foreign entity Icme\MainBundle\Entity\Personne, however this entity has no ientity itself. You have to call EntityManager#persist() on the related entity and make sure it an identifier was generated before trying to persist 'Icme\MainBundle\Entity\PersonneFonction'. In case of Post Insert ID Generation (such as MySQL Auto-Increment or PostgreSQL SERIAL) this means you have to call EntityManager#flush() between both persist operations.


Lorsque j'ajoute le persist cascade sur personnefonctions dans Personne, j'ai cette même erreur également.


Merci d'avance.
Cordialement.