Bonjour,
SVP j'ai besoin de votre aide,bon je vais vous expliquez un peu le truc.
j'ai 2 entités Produit et Categorie, et je veux faire l'opération d'ajout d'un produit en saisissant les informations nécessaire d'un produit et en sélectionnant une catégorie, sachant que toutes les catégories sont chargé dans une liste depuis la base de données.
Mais le problème c'est qu'au moment de l'insertion d'un Produit je dois insérer la Categorie au même temps, même si j'ai pas fais un CASCADE="persist".
L'exception affichée
voici le codeA new entity was found through the relationship 'ibzar\ProduitBundle\Entity\Produit#categorie' that was not configured to cascade persist operations for entity: ibzar\ProduitBundle\Entity\Categorie@000000000479cb4100000000371825b0. Explicitly persist the new entity or configure cascading persist operations on the relationship. If you cannot find out which entity causes the problem implement 'ibzar\ProduitBundle\Entity\Categorie#__toString()' to get a clue.
500 Internal Server Error - InvalidArgumentException
l'entité Produit
l'entité Categorie
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167 <?php namespace ibzar\ProduitBundle\Entity; use Doctrine\ORM\Mapping as ORM; /** * ibzar\ProduitBundle\Entity\Produit * * @ORM\Table() * @ORM\Entity(repositoryClass="ibzar\ProduitBundle\Entity\ProduitRepository") */ class Produit { /** * @var integer $id * * @ORM\Column(name="id", type="integer") * @ORM\Id * @ORM\GeneratedValue(strategy="AUTO") */ private $id; /** * @var text $designation * * @ORM\Column(name="designation", type="text") */ private $designation; /** * @var float $prix * * @ORM\Column(name="prix", type="float") */ private $prix; /** * @var float $qte_stock * * @ORM\Column(name="qte_stock", type="float") */ private $qte_stock; /** * @var float $qte_comptoir * * @ORM\Column(name="qte_comptoir", type="float") */ private $qte_comptoir; /** * @ORM\ManyToOne(targetEntity="ibzar\ProduitBundle\Entity\Categorie", inversedBy="produits") * @ORM\JoinColumn(nullable=false) */ private $categorie; /** * Get id * * @return integer */ public function getId() { return $this->id; } /** * Set designation * * @param text $designation */ public function setDesignation($designation) { $this->designation = $designation; } /** * Get designation * * @return text */ public function getDesignation() { return $this->designation; } /** * Set prix * * @param float $prix */ public function setPrix($prix) { $this->prix = $prix; } /** * Get prix * * @return float */ public function getPrix() { return $this->prix; } /** * Set qte_stock * * @param float $qteStock */ public function setQteStock($qteStock) { $this->qte_stock = $qteStock; } /** * Get qte_stock * * @return float */ public function getQteStock() { return $this->qte_stock; } /** * Set qte_comptoir * * @param float $qteComptoir */ public function setQteComptoir($qteComptoir) { $this->qte_comptoir = $qteComptoir; } /** * Get qte_comptoir * * @return float */ public function getQteComptoir() { return $this->qte_comptoir; } /** * Set categorie * * @param Categorie $categorie */ public function setCategorie($categorie) { $this->categorie = $categorie; } /** * Get categorie * * @return Categorie */ public function getCategorie() { return $this->categorie; } }
La classe produitHandler
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 <?php namespace ibzar\ProduitBundle\Entity; use Doctrine\ORM\Mapping as ORM; /** * ibzar\ProduitBundle\Entity\Categorie * * @ORM\Table() * @ORM\Entity(repositoryClass="ibzar\ProduitBundle\Entity\CategorieRepository") */ class Categorie { /** * @var integer $id * * @ORM\Column(name="id", type="integer") * @ORM\Id * @ORM\GeneratedValue(strategy="AUTO") */ private $id; /** * @var text $designation * * @ORM\Column(name="designation", type="text") */ private $designation; /** * @ORM\OneToMany(targetEntity="ibzar\ProduitBundle\Entity\Produit", mappedBy="categorie") */ private $produits; /** * Get id * * @return integer */ public function getId() { return $this->id; } /** * Set designation * * @param text $designation */ public function setDesignation($designation) { $this->designation = $designation; } /** * Get designation * * @return text */ public function getDesignation() { return $this->designation; } public function __construct() { $this->produits = new \Doctrine\Common\Collections\ArrayCollection(); } public function addProduit(\ibzar\ProduitBundle\Entity\Produit $produits) { $this->produits[] = $produits; $produits->setCategorie($this); return $this; } public function removeProduit(\ibzar\ProduitBundle\Entity\Produit $produits) { $this->produits->removeElement($produits); $produits->setCategorie(null); } public function getProduits() { return $this->produits; } }
La méthode ajouterAction du contrôleur
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 <?php // src/ibzar/ProduitBundle/Form/ProduitHandler.php namespace ibzar\ProduitBundle\Form; use Symfony\Component\Form\Form; use Symfony\Component\HttpFoundation\Request; use Doctrine\ORM\EntityManager; use ibzar\ProduitBundle\Entity\Produit; use ibzar\ProduitBundle\Entity\Categorie; class ProduitHandler { protected $form; protected $request; protected $em; public function __construct(Form $form, Request $request, EntityManager $em) { $this->form = $form; $this->request = $request; $this->em = $em; } public function process() { if( $this->request->getMethod() == 'POST' ) { $this->form->bindRequest($this->request); if( $this->form->isValid() ) { $this->onSuccess($this->form->getData()); return true; } } return false; } public function onSuccess(Produit $produit) { $this->em->persist($produit); $this->em->flush(); } }
Merci d'avance.
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11 public function ajouterAction() { $produit = new Produit(); $form = $this->createForm(new ProduitType, $produit); $formHandler = new ProduitHandler($form,$this->get('request'),$this->getDoctrine()->getEntityManager()); if( $this->process() ) { return $this->redirect($this->generateUrl('homepage')); } return $this->render('ibzarProduitBundle:Default:ajouter.html.twig',array('form'=>$form->createView())); }
Partager