Bonsoir, voici mon problème:
J'ai un objet famille:
cet objet a des relations oneToOne avec un autre objet User
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 <?php namespace BalancelleBundle\Entity; use Doctrine\ORM\Mapping as ORM; /** * Famille * * @ORM\Table(name="famille") * @ORM\Entity(repositoryClass="BalancelleBundle\Repository\FamilleRepository") */ class Famille { /** * @ORM\OneToOne(targetEntity="BalancelleBundle\Entity\User", cascade={"persist"}) */ private $parent1; /** * @ORM\OneToOne(targetEntity="BalancelleBundle\Entity\User", cascade={"persist"}) */ private $parent2; /** * @var int * * @ORM\Column(name="id", type="integer") * @ORM\Id * @ORM\GeneratedValue(strategy="AUTO") */ private $id; /** * @var string * * @ORM\Column(name="nom", type="string", length=255) */ private $nom; /** * @var \DateTime * * @ORM\Column(name="dateCreation", type="datetime") */ private $dateCreation; /** * @var \DateTime * * @ORM\Column(name="dateModification", type="datetime") */ private $dateModification; /** * @var bool * * @ORM\Column(name="active", type="boolean") */ private $active; /** * Get id * * @return int */ public function getId() { return $this->id; } /** * Set nom * * @param string $nom * * @return Famille */ public function setNom($nom) { $this->nom = $nom; return $this; } /** * Get nom * * @return string */ public function getNom() { return $this->nom; } /** * Set dateCreation * * @param \DateTime $dateCreation * * @return Famille */ public function setDateCreation($dateCreation) { $this->dateCreation = $dateCreation; return $this; } /** * Get dateCreation * * @return \DateTime */ public function getDateCreation() { return $this->dateCreation; } /** * Set dateModification * * @param \DateTime $dateModification * * @return Famille */ public function setDateModification($dateModification) { $this->dateModification = $dateModification; return $this; } /** * Get dateModification * * @return \DateTime */ public function getDateModification() { return $this->dateModification; } /** * Set active * * @param boolean $active * * @return Famille */ public function setActive($active) { $this->active = $active; return $this; } /** * Get active * * @return bool */ public function getActive() { return $this->active; } /** * Set parent1 * * @param \BalancelleBundle\Entity\User $parent1 * * @return Famille */ public function setParent1(\BalancelleBundle\Entity\User $parent1 = null) { $this->parent1 = $parent1; return $this; } /** * Get parent1 * * @return \BalancelleBundle\Entity\User */ public function getParent1() { return $this->parent1; } /** * Set parent2 * * @param \BalancelleBundle\Entity\User $parent2 * * @return Famille */ public function setParent2(\BalancelleBundle\Entity\User $parent2 = null) { $this->parent2 = $parent2; return $this; } /** * Get parent2 * * @return \BalancelleBundle\Entity\User */ public function getParent2() { return $this->parent2; } }
je suis en train de finaliser mon formulaire pour faire des ajouts (Famille), j'ai un FamilleType:
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 <?php // src/BalancelleBundle/Entity/User.php namespace BalancelleBundle\Entity; use FOS\UserBundle\Model\User as BaseUser; use Doctrine\Common\Collections\ArrayCollection; use Doctrine\ORM\Mapping as ORM; /** * @ORM\Entity * @ORM\Table(name="fos_user") */ class User extends BaseUser { /** * @ORM\Id * @ORM\Column(type="integer") * @ORM\GeneratedValue(strategy="AUTO") */ protected $id; /** * @var string * * @ORM\Column(name="prenom", type="string", length=255) */ private $prenom; /** * @var string * * @ORM\Column(name="nom", type="string", length=255) */ private $nom; /** * * @ORM\Column(name="birthday", type="date", nullable=true) */ protected $birthday=null; /** * Constructeur de l'objet */ public function __construct() { parent::__construct(); } /** * Set prenom * * @param string $prenom * * @return User */ public function setPrenom($prenom) { $this->prenom = $prenom; return $this; } /** * Get prenom * * @return string */ public function getPrenom() { return $this->prenom; } /** * Set nom * * @param string $nom * * @return User */ public function setNom($nom) { $this->nom = $nom; return $this; } /** * Get nom * * @return string */ public function getNom() { return $this->nom; } /** * Set birthday * * @param \DateTime $birthday * * @return User */ public function setBirthday($birthday) { $this->birthday = $birthday; return $this; } /** * Get birthday * * @return \DateTime */ public function getBirthday() { return $this->birthday; } /** * Détermine si l'utilisateur a le rôle administrateur * @return boolean */ public function isroleAdmin() { return $this->hasRole("ROLE_ADMIN"); } /** * Enregistre le role administrateur * @param \Boolean $bool * @return User */ public function setRoleAdmin($bool) { $this->removeRole("ROLE_ADMIN"); if ($bool) { $this->addRole("ROLE_ADMIN"); } return $this; } /** * Récupère le role admin * @return \Boolean */ public function getRoleAdmin() { return $this->hasRole("ROLE_ADMIN"); } /** * Détermine si l'utilisateur a le rôle parent * @return \boolean */ public function isroleParent() { return $this->hasRole("ROLE_PARENT"); } /** * Enregistre le role parent * @param \Boolean $bool * @return User */ public function setRoleParent($bool) { $this->removeRole("ROLE_PARENT"); if ($bool) { $this->addRole("ROLE_PARENT"); } return $this; } /** * Récupère le role parent * @return \Boolean */ public function getRoleParent() { return $this->hasRole("ROLE_PARENT"); } /** * Détermine si l'utilisateur a le rôle professionnel * @return boolean */ public function isrolePro() { return $this->hasRole("ROLE_PRO"); } /** * Enregistre le role professionnel * @param \Boolean $bool * @return User */ public function setRolePro($bool) { $this->removeRole("ROLE_PRO"); if ($bool) { $this->addRole("ROLE_PRO"); } return $this; } /** * Récupère le role Pro * @return \Boolean */ public function getRolePro() { return $this->hasRole("ROLE_PRO"); } }
et un template twig qui récupère tout ça et l'affiche. Jusqu'ici tout fonctionne a ce ci près que les menus déroulant généré ainsi prenne comme label des options le username de ma class user. Je souhaite modifier ça mais je n'y arrive pas.
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 <?php namespace BalancelleBundle\Form; use Symfony\Component\Form\AbstractType; use Symfony\Component\Form\FormBuilderInterface; use Symfony\Component\OptionsResolver\OptionsResolver; use Symfony\Component\Form\Extension\Core\Type\ChoiceType; class FamilleType extends AbstractType { /** * {@inheritdoc} */ public function buildForm(FormBuilderInterface $builder, array $options) {//var_dump($options['parent1']); $builder ->add('nom') ->add('active') ->add('parent1') ->add('parent2'); }/** * {@inheritdoc} */ public function configureOptions(OptionsResolver $resolver) { $resolver->setDefaults(array( 'data_class' => 'BalancelleBundle\Entity\Famille' )); } /** * {@inheritdoc} */ public function getBlockPrefix() { return 'balancellebundle_famille'; } }
j'ai essayé de faire un truc comme ça:
mais la j'obtiens une liste vide...
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 public function buildForm(FormBuilderInterface $builder, array $options) {//var_dump($options['parent1']); $builder ->add('nom') ->add('active') ->add('parent1',ChoiceType::class, [ 'choice_label' => function($parent1, $key, $value) { /** @var User $parent */ return strtoupper($parent1->getPrenom()." ".$parent1->getNom()); }, 'choice_attr' => function($parent, $key, $value) { return ['class' => $parent->getId()]; } ]) ->add('parent2');
Comment faire pour modifier la valeur de mes otions? merci d'avance pour vos pistes...
Partager