Bonjour,
Ayant commencé il ya peu le développement sous symfony2 et doctrine2, je n'arrive pas à comprendre comment afficher une colonne de la table qui est lié à ma table principale. Je m'explique, j'ai deux tables, une table "users" et une table "pays" lié par "pays_id". Quand j'affiche un enregistrement unique (ex : users.id = 2), alors il m'est possible de récupérer le nom du pays comme ceci : $entity->getPaysId()->getPays().
Malheureusement, ceci ne fonctionne pas quand je suis dans une boucle foreach. Une erreur "entity not found m'est retournée".
Voici les différents codes des fichiers utilisés pour ceci :
Les fichiers Users :
Users.orm.yml
Pays.orm.yml
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 Ito\DefaultBundle\Entity\Users: type: entity table: users repositoryClass: Ito\DefaultBundle\Repository\UsersRepository manyToMany: userRoles: targetEntity: Roles joinTable: name: users_roles joinColumns: user_id: referencedColumnName: id inverseJoinColumns: role_id: referencedColumnName: id oneToOne: pays_id: targetEntity: Pays joinColumn: name: pays_id referencedColumnName: id fields: id: type: integer id: true generator: strategy: AUTO username: type: string length: 255 password: type: string length: 255 nom: type: string length: 255 prenom: type: string length: 255 entreprise: type: string length: 255 adresse1: type: string length: 255 adresse2: type: string length: 255 cp: type: string length: 255 ville: type: string length: 255 # pays_id: # type: integer numero_tva_intracommunautaire: type: string length: 255 lifecycleCallbacks: { }
Users.php
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14 Ito\DefaultBundle\Entity\Pays: type: entity table: pays fields: id: type: integer id: true generator: strategy: AUTO pays: type: string length: 255 lifecycleCallbacks: { }
Pays.php
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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410 <?php namespace Ito\DefaultBundle\Entity; use Doctrine\ORM\Mapping as ORM; use \Symfony\Component\Security\Core\User\UserInterface; use Doctrine\Common\Collections\ArrayCollection; /** * Ito\DefaultBundle\Entity\Users */ class Users implements UserInterface, \Serializable { /** * @var integer $id */ private $id; /** * @var string $username */ private $username; /** * @var string $password */ private $password; /** * @var string $nom */ private $nom; /** * @var string $prenom */ private $prenom; /** * @var string $entreprise */ private $entreprise; /** * @var string $adresse1 */ private $adresse1; /** * @var string $adresse2 */ private $adresse2; /** * @var string $cp */ private $cp; /** * @var string $ville */ private $ville; /** * @var integer $pays_id */ private $pays_id; private $pays; public function getPays() { return $this->getPaysId(); } /** * @var string $numero_tva_intracommunautaire */ private $numero_tva_intracommunautaire; /** * @var array $userRoles */ private $userRoles; private $salt; public function __toString() { return $this->username; } /** * Gets an array of roles. * * @return array An array of Role objects */ public function getRoles() { //return array('ROLE_ADMIN'); return $this->getUserRoles()->toArray(); } /** * Gets the user roles. * * @return ArrayCollection A Doctrine ArrayCollection */ public function getUserRoles() { return $this->userRoles; } /** * Compares this user to another to determine if they are the same. * * @param UserInterface $user The user * @return boolean True if equal, false othwerwise. */ public function equals(UserInterface $user) { return md5($this->getUsername()) == md5($user->getUsername()); } /** * Gets the user salt. * * @return string The salt. */ public function getSalt() { return ''; //return '$this->salt'; } /** * Sets the user salt. * * @param string $value The salt. */ public function setSalt($value) { $this->salt = md5($value); } /** * Erases the user credentials. */ public function eraseCredentials() { } /** * Get id * * @return integer */ public function getId() { return $this->id; } /** * Set username * * @param string $username */ public function setUsername($username) { $this->username = $username; } /** * Get username * * @return string */ public function getUsername() { return $this->username; } /** * Set password * * @param string $password */ public function setPassword($password) { $this->password = $password; } /** * Get password * * @return string */ public function getPassword() { return $this->password; } /** * Set nom * * @param string $nom */ public function setNom($nom) { $this->nom = $nom; } /** * Get nom * * @return string */ public function getNom() { return $this->nom; } /** * Set prenom * * @param string $prenom */ public function setPrenom($prenom) { $this->prenom = $prenom; } /** * Get prenom * * @return string */ public function getPrenom() { return $this->prenom; } /** * Set entreprise * * @param string $entreprise */ public function setEntreprise($entreprise) { $this->entreprise = $entreprise; } /** * Get entreprise * * @return string */ public function getEntreprise() { return $this->entreprise; } /** * Set adresse1 * * @param string $adresse1 */ public function setAdresse1($adresse1) { $this->adresse1 = $adresse1; } /** * Get adresse1 * * @return string */ public function getAdresse1() { return $this->adresse1; } /** * Set adresse2 * * @param string $adresse2 */ public function setAdresse2($adresse2) { $this->adresse2 = $adresse2; } /** * Get adresse2 * * @return string */ public function getAdresse2() { return $this->adresse2; } /** * Set cp * * @param string $cp */ public function setCp($cp) { $this->cp = $cp; } /** * Get cp * * @return string */ public function getCp() { return $this->cp; } /** * Set ville * * @param string $ville */ public function setVille($ville) { $this->ville = $ville; } /** * Get ville * * @return string */ public function getVille() { return $this->ville; } /** * Set pays_id * * @param integer $paysId */ public function setPaysId($paysId) { $this->pays_id = $paysId; } /** * Get pays_id * * @return integer */ public function getPaysId() { return $this->pays_id; } /*public function getTest() { return $this->getPaysId()->getPays(); }*/ /** * Set numero_tva_intracommunautaire * * @param string $numeroTvaIntracommunautaire */ public function setNumeroTvaIntracommunautaire($numeroTvaIntracommunautaire) { $this->numero_tva_intracommunautaire = $numeroTvaIntracommunautaire; } /** * Get numero_tva_intracommunautaire * * @return string */ public function getNumeroTvaIntracommunautaire() { return $this->numero_tva_intracommunautaire; } public function serialize() { return implode(',', array( 'username' => $this->getUsername() )); } public function unserialize($strSerialized) { $serialized = explode(',', $strSerialized); $this->setUsername($serialized[0]); } }
Comment faire pour récupérer facilement le nom du pays dans un foreach?
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 <?php namespace Ito\DefaultBundle\Entity; use Doctrine\ORM\Mapping as ORM; /** * Ito\DefaultBundle\Entity\Pays */ class Pays { /** * @var integer $id */ private $id; /** * @var string $pays */ private $pays; public function __toString() { return $this->pays; } /** * Get id * * @return integer */ public function getId() { return $this->id; } /** * Set pays * * @param string $pays */ public function setPays($pays) { $this->pays = $pays; } /** * Get pays * * @return string */ public function getPays() { return $this->pays; } }
Merci
Partager