Bonjour à tous,
J'ai un soucis sur un de mes formulaires.
Mon modèle est assez simple, j'ai une entité entreprise avec des salariés, chaque salarié peut avoir un poste et chaque poste correspond à un type de poste.
Dans mon formulaire, j'aimerais passer l'entité salarié et faire apparaître deux champs, un pour le type de poste et en fonction de celui là, une liste de poste dans un second champ.
Voici le code de mes entités poste et type de poste :
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 KG\CompanyManagementBundle\Entity; use Doctrine\ORM\Mapping as ORM; use Symfony\Component\Validator\Constraints as Assert; /** * poste * * @ORM\Table() * @ORM\Entity(repositoryClass="KG\CompanyManagementBundle\Entity\PosteRepository") */ class Poste { /** * @var integer * * @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 TypePoste * * @ORM\ManyToOne(targetEntity="KG\CompanyManagementBundle\Entity\TypePoste") * @ORM\JoinColumn(nullable=false) * @Assert\Valid() * @Assert\NotBlank(message="Veuillez sélectionner le type de poste") */ private $type; /** * @var Entreprise * * @ORM\ManyToOne(targetEntity="KG\CompanyManagementBundle\Entity\Entreprise", inversedBy="postes") * @ORM\JoinColumn(nullable=false) */ private $entreprise; /** * @ORM\OneToOne(targetEntity="KG\CompanyManagementBundle\Entity\Image", cascade={"persist"}) * @Assert\Valid() */ private $image; /** * @ORM\OneToOne(targetEntity="KG\CompanyManagementBundle\Entity\Salarie", inversedBy="poste") * @Assert\Valid() */ private $salarie; /** * @var boolean * @ORM\Column(name="supprime", type="boolean") */ private $supprime = false; /** * Get id * * @return integer */ public function getId() { return $this->id; } /** * Set nom * * @param string $nom * @return Poste */ public function setNom($nom) { $this->nom = $nom; return $this; } /** * Get nom * * @return string */ public function getNom() { return $this->nom; } /** * Set type * * @param string $type * @return Poste */ public function setType($type) { $this->type = $type; return $this; } /** * Get type * * @return string */ public function getType() { return $this->type; } /** * Set image * * @param Image $image * @return Poste */ public function setImage(Image $image) { $this->image = $image; return $this; } /** * Get image * * @return Image */ public function getImage() { return $this->image; } /** * Set Salarie * * @param \KG\CompanyManagementBundle\Entity\Salarie $salarie * @return Poste */ public function setSalarie(\KG\CompanyManagementBundle\Entity\Salarie $salarie = null) { $this->salarie = $salarie; return $this; } /** * Get Salarie * * @return \KG\CompanyManagementBundle\Entity\Salarie */ public function getSalarie() { return $this->salarie; } /** * Set entreprise * * @param \KG\CompanyBundle\Entity\Entreprise $entreprise * @return Poste */ public function setExploitation(\KG\CompanyManagementBundle\Entity\Entreprise $entreprise) { $this->entreprise = $entreprise; return $this; } /** * Get entreprise * * @return \KG\CompanyManagementBundle\Entity\Entreprise */ public function getEntreprise() { return $this->entreprise; } /** * Set supprime * * @param string $supprime * @return Poste */ public function setSupprime($supprime) { $this->supprime = $supprime; return $this; } /** * Get supprime * * @return boolean */ public function getSupprime() { return $this->supprime; } }Voici mon 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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64 <?php namespace KG\CompanyManagementBundle\Entity; use Doctrine\ORM\Mapping as ORM; /** * TypePoste * * @ORM\Table() * @ORM\Entity(repositoryClass="KG\CompanyManagementBundle\Entity\TypePosteRepository") */ class TypePoste { /** * @var integer * * @ORM\Column(name="id", type="integer") * @ORM\Id * @ORM\GeneratedValue(strategy="AUTO") */ private $id; /** * @var string * * @ORM\Column(name="libelle", type="string", length=20) */ private $libelle; /** * Get id * * @return integer */ public function getId() { return $this->id; } /** * Set libelle * * @param string $libelle * @return TypePoste */ public function setLibelle($libelle) { $this->libelle = $libelle; return $this; } /** * Get libelle * * @return string */ public function getLibelle() { return $this->libelle; } }
Voici la classe AddPosteFieldSubscriber :
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 <?php namespace KG\CompanyManagementBundle\Form\Type; use Symfony\Component\Form\AbstractType; use Symfony\Component\Form\FormBuilderInterface; use Symfony\Component\OptionsResolver\OptionsResolverInterface; use KG\CompanyManagementBundle\Form\EventListener\AddTypePosteFieldSubscriber; use KG\CompanyManagementBundle\Form\EventListener\AddPosteFieldSubscriber; class AttribuerType extends AbstractType { /** * @param FormBuilderInterface $builder * @param array $options */ public function buildForm(FormBuilderInterface $builder, array $options) { $propertyPathToPoste = 'poste'; $builder ->addEventSubscriber(new AddPosteFieldSubscriber($propertyPathToPoste)) ->addEventSubscriber(new AddTypePosteFieldSubscriber($propertyPathToPoste)); } /** * @param OptionsResolverInterface $resolver */ public function setDefaultOptions(OptionsResolverInterface $resolver) { } /** * @return string */ public function getName() { return 'kg_companymanagementbundle_attribuer'; } }
Voici la classe AddTypePosteFieldSubscriber :
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 <?php namespace KG\CompanyManagementBundle\Form\EventListener; use Symfony\Component\Form\FormEvent; use Symfony\Component\Form\FormEvents; use Symfony\Component\EventDispatcher\EventSubscriberInterface; use Symfony\Component\PropertyAccess\PropertyAccess; use Doctrine\ORM\EntityRepository; class AddPosteFieldSubscriber implements EventSubscriberInterface { private $propertyPathToPoste; public function __construct($propertyPathToPoste) { $this->propertyPathToPoste = $propertyPathToPoste; } public static function getSubscribedEvents() { return array( FormEvents::PRE_SET_DATA => 'preSetData', FormEvents::PRE_SUBMIT => 'preSubmit' ); } private function addPosteForm($form, $type) { $formOptions = array( 'class' => 'KGCompanyManagementBundle:Poste', 'empty_value' => '', 'attr' => array( 'class' => 'poste_selector', ), 'query_builder' => function (EntityRepository $repository) use ($type) { $qb = $repository->createQueryBuilder('poste') ->innerJoin('poste.type', 'type') ->where('type.id = :type') ->setParameter('type', $type) ; return $qb; } ); $form->add($this->propertyPathToPoste, 'entity', $formOptions); } public function preSetData(FormEvent $event) { $data = $event->getData(); $form = $event->getForm(); if (null === $data) { return; } $accessor = PropertyAccess::createPropertyAccessor(); $poste = $accessor->getValue($data, $this->propertyPathToPoste); $type = ($poste) ? $poste->getType()->getId() : null; $this->addPosteForm($form, $type); } public function preSubmit(FormEvent $event) { $data = $event->getData(); $form = $event->getForm(); $type = array_key_exists('type', $data) ? $data['type'] : null; $this->addPosteForm($form, $type); } }
J'appelle mon formulaire depuis mon contrôleur avec ce code :
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 <?php namespace KG\CompanyManagementBundle\Form\EventListener; use Symfony\Component\Form\FormEvent; use Symfony\Component\Form\FormEvents; use Symfony\Component\PropertyAccess\PropertyAccess; use Symfony\Component\EventDispatcher\EventSubscriberInterface; class AddTypePosteFieldSubscriber implements EventSubscriberInterface { private $propertyPathToPoste; public function __construct($propertyPathToPoste) { $this->propertyPathToPoste = $propertyPathToPoste; } public static function getSubscribedEvents() { return array( FormEvents::PRE_SET_DATA => 'preSetData', FormEvents::PRE_SUBMIT => 'preSubmit' ); } private function addTypeForm($form, $type = null) { $formOptions = array( 'class' => 'KGCompanyManagementBundle:TypePoste', 'property' => 'libelle', 'mapped' => false, 'empty_value' => '', 'attr' => array( 'class' => 'type_selector', ), ); if ($type) { $formOptions['data'] = $type; } $form->add('type', 'entity', $formOptions); } public function preSetData(FormEvent $event) { $data = $event->getData(); $form = $event->getForm(); if (null === $data) { return; } $accessor = PropertyAccess::getPropertyAccessor(); $poste = $accessor->getValue($data, $this->propertyPathToPoste); $type = ($poste) ? $poste->getType() : null; $this->addTypeForm($form, $type); } public function preSubmit(FormEvent $event) { $form = $event->getForm(); $this->addTypeForm($form); } }
Voici mon erreur :
Code : Sélectionner tout - Visualiser dans une fenêtre à part $form = $this->createForm(new AttribuerType($salarie));
Voilà plusieurs jours que je bloque sur ce pb, j'attends beaucoup de vous
Code : Sélectionner tout - Visualiser dans une fenêtre à part Neither the property "type" nor one of the methods "getType()", "type()", "isType()", "hasType()", "__get()" exist and have public access in class "KG\CompanyManagementBundle\Entity\Salarie".
Merci d'avance pour vos réponses.
Partager