Bonjour,
J'utilises doctrine 2 depuis pas très longtemps et son intégration sur ZF2 c'est à peu prêt fait sans douleur(quelques soucis avec des modules comme ZFCUser mais c'est résolu).
J'ai actuellement un soucis tout bête avec les formulaires: je n'arrive pas à renseigner un select multiple avec les valeurs d'une entité qui est liée à l'objet principal du formulaire.
Pour plus de clarté, voici les différents fichiers et données utilisés :
Tables impactées
Entités
StructureUser.php
StructureUserJob
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254 <?php namespace Application\Entity; use Doctrine\ORM\Mapping as ORM; /** * StructureUser * * @ORM\Table(name="structure_user", indexes={@ORM\Index(name="fk_structure_user_structure_idx", columns={"structure_id"}), @ORM\Index(name="fk_structure_user_user1_idx", columns={"user_id"})}) * @ORM\Entity */ class StructureUser { /** * * @var integer @ORM\Column(name="structure_user_id", type="bigint", nullable=false) * @ORM\Id * @ORM\GeneratedValue(strategy="IDENTITY") */ private $structureUserId; /** * * @var string @ORM\Column(name="date_start", type="date", nullable=false) */ private $dateStart; /** * * @var string @ORM\Column(name="date_end", type="date", nullable=true) */ private $dateEnd; /** * * @var string @ORM\Column(name="phone", type="string", length=45, nullable=true) */ private $phone; /** * * @var string @ORM\Column(name="email", type="string", length=255, nullable=true) */ private $email; /** * * @var \Application\Entity\Structure @ORM\ManyToOne(targetEntity="Application\Entity\Structure") * @ORM\JoinColumns({ * @ORM\JoinColumn(name="structure_id", referencedColumnName="structure_id") * }) */ private $structure; /** * * @var \Application\Entity\StructureUserJob * @ORM\OneToMany(targetEntity="Application\Entity\StructureUserJob", mappedBy="structureUser", cascade={"persist"}) */ private $jobs; /** * * @var \Application\Entity\User @ORM\ManyToOne(targetEntity="Application\Entity\User") * @ORM\JoinColumns({ * @ORM\JoinColumn(name="user_id", referencedColumnName="user_id") * }) */ private $user; public function __construct() { $this->jobs = new \Doctrine\Common\Collections\ArrayCollection(); } /** * Get structureUserId * * @return integer */ public function getStructureUserId() { return $this->structureUserId; } /** * Set dateStart * * @param string $dateStart * @return StructureUser */ public function setDateStart($dateStart) { $this->dateStart = $dateStart; return $this; } /** * Get dateStart * * @return string */ public function getDateStart() { return $this->dateStart; } /** * Set dateEnd * * @param string $dateEnd * @return StructureUser */ public function setDateEnd($dateEnd) { $this->dateEnd = $dateEnd; return $this; } /** * Get dateEnd * * @return string */ public function getDateEnd() { return $this->dateEnd; } /** * Set phone * * @param string $phone * @return StructureUser */ public function setPhone($phone) { $this->phone = $phone; return $this; } /** * Get phone * * @return string */ public function getPhone() { return $this->phone; } /** * Set email * * @param string $email * @return StructureUser */ public function setEmail($email) { $this->email = $email; return $this; } /** * Get email * * @return string */ public function getEmail() { return $this->email; } /** * Set structure * * @param \Application\Entity\Structure $structure * @return StructureUser */ public function setStructure(\Application\Entity\Structure $structure = null) { $this->structure = $structure; return $this; } /** * Get structure * * @return \Application\Entity\Structure */ public function getStructure() { return $this->structure; } /** * Set user * * @param \Application\Entity\User $user * @return StructureUser */ public function setUser(\Application\Entity\User $user = null) { $this->user = $user; return $this; } /** * Get user * * @return \Application\Entity\User */ public function getUser() { return $this->user; } /** * @param \Doctrine\Common\Collections\Collection $jobs */ public function addJobs(\Doctrine\Common\Collections\Collection $jobs) { foreach ($jobs as $job) { $job->setStructureUser($this); $this->jobs->add($job); } } /** * @param \Doctrine\Common\Collections\Collection $jobs */ public function removeJobs(\Doctrine\Common\Collections\Collection $jobs) { foreach ($jobs as $job) { $job->setStructureUser($this); $this->jobs->removeElement($job); } } /** * @return \Doctrine\Common\Collections\Collection */ public function getJobs() { return $this->jobs; } }
Job
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 <?php namespace Application\Entity; use Doctrine\ORM\Mapping as ORM; /** * StructureUserJob * * @ORM\Table(name="structure_user_job", indexes={@ORM\Index(name="structure_user_id", columns={"structure_user_id"}), @ORM\Index(name="job_code", columns={"job_code"})}) * @ORM\Entity */ class StructureUserJob { /** * * @var integer @ORM\Column(name="structure_user_job_id", type="bigint", nullable=false) * @ORM\Id * @ORM\GeneratedValue(strategy="IDENTITY") */ private $structureUserJobId; /** * * @var \Application\Entity\Job @ORM\ManyToOne(targetEntity="Application\Entity\Job") * @ORM\JoinColumns({ * @ORM\JoinColumn(name="job_code", referencedColumnName="job_code") * }) */ private $job; /** * * @var \Application\Entity\StructureUser @ORM\ManyToOne(targetEntity="Application\Entity\StructureUser", inversedBy="jobs") * @ORM\JoinColumns({ * @ORM\JoinColumn(name="structure_user_id", referencedColumnName="structure_user_id") * }) */ private $structureUser; /** * Get structureUserJobId * * @return integer */ public function getStructureUserJobId() { return $this->structureUserJobId; } /** * Set job * * @param \Application\Entity\Job $job * @return StructureUserJob */ public function setJob($job) { $this->job = $job; return $this; } /** * Get job * * @return \Application\Entity\Job */ public function getJob() { return $this->job; } /** * Set structureUser * * @param \Application\Entity\StructureUser $structureUser * @return StructureUserSpeciality */ public function setStructureUser(\Application\Entity\StructureUser $structureUser = null) { $this->structureUser = $structureUser; return $this; } /** * Get structureUser * * @return \Application\Entity\StructureUser */ public function getStructureUser() { return $this->structureUser; } }
Formulaire
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 <?php namespace Application\Entity; use Doctrine\ORM\Mapping as ORM; /** * Job * * @ORM\Table(name="job") * @ORM\Entity */ class Job { /** * * @var string @ORM\Column(name="job_code", type="string", length=255, nullable=false) * @ORM\Id */ private $jobCode; /** * Get jobCode * * @return string */ public function getJobCode() { return $this->jobCode; } /** * Set jobCode * * @param string $jobCode * * @return \Application\Entity\Job */ public function setJobCode($jobCode) { $this->jobCode = $jobCode; return $this; } }
Controller
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 <?php namespace Application\Form; class AffectationEditForm extends \Zend\Form\Form { public function __construct($name = null) { // PLEIN DE TRUC AVANT PAS UTILE ET LONG // ..... // job $this->add(array( 'name' => 'jobs', 'type' => 'DoctrineModule\Form\Element\ObjectSelect', 'attributes' => array( 'multiple' => true ), 'options' => array( 'object_manager' => $this->getObjectManager(), 'target_class' => '\Application\Entity\Job', 'property' => 'jobCode', 'is_method' => true, 'label_generator' => function ($job) { return 'Job.' . $job->getJobCode(); } ) )); // D AUTRES CHAMPS INUTILE APRES // .... } }
------------------------------------------------------------------------------------
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 <?php /** * Zend Framework (http://framework.zend.com/) * * @link http://github.com/zendframework/ZendSkeletonApplication for the canonical source repository * @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com) * @license http://framework.zend.com/license/new-bsd New BSD License */ namespace Application\Controller; use Zend\Mvc\Controller\AbstractActionController; use Zend\View\Model\ViewModel; class AffectationController extends AbstractActionController { // D AUTRES FONCTIONS AVANT ... public function editAction() { // redirection si pas ajax if (! $this->getRequest()->isXmlHttpRequest()) { $this->redirect()->toRoute('structure'); } // on recupere l'entity manager $em = $this->getServiceLocator()->get('Doctrine\ORM\EntityManager'); // on recupere le formulaire $editionForm = $this->getServiceLocator()->get('Application\Form\AffectationEditForm'); $editionForm->setInputFilter($this->getServiceLocator() ->get('Application\Form\AffectationEditFormFilter')); // on recupere la structure $structure = $this->getServiceLocator() ->get('Structure\Service\Structure') ->getStructureEnableByAccount($this->params() ->fromRoute('structure')); if (! $structure instanceof \Application\Entity\Structure) { die(); } // on recupere l'affectation if (is_numeric($this->params()->fromRoute('id'))) { $structureUser = $this->getServiceLocator() ->get('Structure\Service\StructureUser') ->get($this->params() ->fromRoute('id')); if (! $structureUser instanceof \Application\Entity\StructureUser) { die(); } } else { $structureUser = new \Application\Entity\StructureUser(); $structureUser->setStructure($structure); } $editionForm->bind($structureUser); // on enregistre if ($this->getRequest()->isPost()) { $editionForm->setData($this->getRequest() ->getPost()); // test if ($editionForm->isValid()) { try { // enregistrement de l'affectation $em->persist($structureUser); $em->flush(); } catch (\Exception $e) { die($e->getMessage()); } die(); } } // assignation $viewModel = new ViewModel(array( 'editionForm' => $editionForm, 'structure' => $structure, 'structureUser' => $structureUser )); $viewModel->setTerminal(true); return $viewModel; } }
Donc mon formulaire affiche bien mes autres champs, pas de soucis; lors de l'enregistrement des valeurs cela fonctionne bien (pour les autres valeurs).
Il ne reste que ce problème du champs Job qui est renseigné par la table Job (la ça fonctionne bien grâce au 'DoctrineModule\Form\Element\ObjectSelect', le select à une liste d'option attendue) mais l'enregistrement doit se faire dans la table StructureUserJob et là, paf, ca fait des chocapics qui ne fonctionnent pas. Evidemment si j'avais des valeurs dans la table StructureUserJob, lors de l'affichage du formulaire, elles ne sont pas affichées.
Pour information, la fonction getJobs fonctionne bien et me retourne bien un tableau de StructureUserJob avec les objets Job de renseignés.
Je pense que j'ai du oublié quelque chose dans la gestion du champ Job qui permet de lui dire d'enregistrer dans la table StructureUserJob mais je ne vois pas quoi.
Si quelqu'un sait comment travailler avec doctrine 2 et ce type de liaison, pourrait-il éclairer ma lanterne ?
Merci d'avance !!
Cordialement
Partager