Bonjour,
J'ai deux entités Advert (mère) et CarAdvert (fille).
J'ai utilisé un héritage SINGLE_TABLE et un héritage de formulaire.
Il se pose un problème de validation à mon avis.
Voici mes codes:
-les entités:
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 /** * Advert * @ORM\Entity * @ORM\Table(name="advert") * @ORM\InheritanceType("SINGLE_TABLE") * @ORM\DiscriminatorColumn(name="discr", type="string") * @ORM\DiscriminatorMap({"advert" = "Advert", "car_advert" = "CarAdvert"}) */ class Advert { /** * @var integer * * @ORM\Column(name="id", type="integer") * @ORM\Id * @ORM\GeneratedValue(strategy="AUTO") */ protected $id; /** * @ORM\ManyToOne(targetEntity="Jack\TestBundle\Entity\Category", cascade={"persist"}) * @ORM\JoinColumn(nullable=false) */ protected $category; /** * @var string * * @ORM\Column(name="advert_title", type="string", length=255, unique=true) * @Assert\Length(min=10, minMessage="Le titre doit faire au moins {{ limit }} caractères.") */ protected $title; /** * @var string * * @ORM\Column(name="advert_content", type="text") * @Assert\NotBlank() */ protected $content; /** * @var float * * @ORM\Column(name="advert_price", type="float") */ protected $price; /** * @ORM\Column(name="advert_published", type="boolean") */ protected $published = true; /** * @ORM\Column(name="advert_updated_at", type="datetime", nullable=true) */ protected $updatedAt; public function __construct() { $this->category = new ArrayCollection(); } /** * Set title * * @param string $title * @return Advert */ public function setTitle($title) { $this->title = $title; return $this; } /** * Get title * * @return string */ public function getTitle() { return $this->title; } /** * Set content * * @param string $content * @return Advert */ public function setContent($content) { $this->content = $content; return $this; } /** * Get content * * @return string */ public function getContent() { return $this->content; } /** * Set price * * @param float $price * @return Advert */ public function setPrice($price) { $this->price = $price; return $this; } /** * Get price * * @return float */ public function getPrice() { return $this->price; } /** * Set category * * @param \Jack\TestBundle\Entity\Category $category * @return Advert */ public function setCategory(\Jack\TestBundle\Entity\Category $category) { $this->category = $category; return $this; } /** * Get category * * @return \Jack\TestBundle\Entity\Category */ public function getCategory() { return $this->category; } /** * Get id * * @return integer */ public function getId() { return $this->id; } /** * Set published * * @param boolean $published * @return Advert */ public function setPublished($published) { $this->published = $published; return $this; } /** * Get published * * @return boolean */ public function getPublished() { return $this->published; } /** * Set updatedAt * * @param \DateTime $updatedAt * @return Advert */ public function setUpdatedAt($updatedAt) { $this->updatedAt = $updatedAt; return $this; } /** * Get updatedAt * * @return \DateTime */ public function getUpdatedAt() { return $this->updatedAt; }-les formulaires
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 /** * CarAdvert * * @ORM\Table("car_advert") * @ORM\Entity(repositoryClass="Jack\TestBundle\Entity\CategoryRepository") */ class CarAdvert extends Advert { /** * @var string * * @ORM\Column(name="car_model", type="string", length=255) */ protected $model; /** * @var string * * @ORM\Column(name="car_make", type="string", length=255) */ protected $make; public function __construct() { parent::__construct(); } /** * Set model * * @param string $model * @return CarAdvert */ public function setModel($model) { $this->model = $model; return $this; } /** * Get model * * @return string */ public function getModel() { return $this->model; } /** * Set make * * @param string $make * @return CarAdvert */ public function setMake($make) { $this->make = $make; return $this; } /** * Get make * * @return string */ public function getMake() { return $this->make; } }
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 namespace Jack\TestBundle\Form; use Symfony\Component\Form\AbstractType; use Symfony\Component\Form\FormBuilderInterface; use Symfony\Component\OptionsResolver\OptionsResolverInterface; class AdvertType extends AbstractType { /** * @param FormBuilderInterface $builder * @param array $options */ public function buildForm(FormBuilderInterface $builder, array $options) { $builder ->add('title') ->add('content') ->add('price') ->add('category', 'entity', array( 'class' => 'JackTestBundle:Category', 'property' => 'name', )) ; } /** * @param OptionsResolverInterface $resolver */ public function setDefaultOptions(OptionsResolverInterface $resolver) { $resolver->setDefaults(array( 'data_class' => 'Jack\TestBundle\Entity\Advert', )); } /** * @return string */ public function getName() { return 'advert'; }-L'action du 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
35
36
37
38
39
40 namespace Jack\TestBundle\Form; use Jack\TestBundle\Form\AdvertType; use Symfony\Component\Form\FormBuilderInterface; use Symfony\Component\OptionsResolver\OptionsResolverInterface; class CarAdvertType extends AdvertType { /** * @param FormBuilderInterface $builder * @param array $options */ public function buildForm(FormBuilderInterface $builder, array $options) { parent::buildForm($builder, $options); $builder ->add('model') ->add('make') ; } /** * @param OptionsResolverInterface $resolver */ public function setDefaultOptions(OptionsResolverInterface $resolver) { $resolver->setDefaults(array( 'data_class' => 'Jack\TestBundle\Entity\CarAdvert', )); } /** * @return string */ public function getName() { return 'caradvert'; } }
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 /** * Create a new advert car * * @Route("/test/create", name="jack_test_create") * @Method("POST") * */ public function createAction(Request $request) { $entity = new CarAdvert(); $form = $this->createForm(new CarAdvertType(), $entity); $form->handleRequest($request); if ($form->isValid()) { $em = $this->getDoctrine()->getManager(); $em->persist($entity); $em->flush(); } return new Response(var_dump($form->getData())); }En essayant de mettre null dans le prototype de setCategory:
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3 Catchable Fatal Error: Argument 1 passed to Jack\TestBundle\Entity\Advert::setCategory() must be an instance of Jack\TestBundle\Entity\Category, null given, called in ...\PropertyAccess\PropertyAccessor.php on line 438 and defined in \Jack\TestBundle\Entity\Advert.php line 142J'obtient après soumission du formulaire,
Code : Sélectionner tout - Visualiser dans une fenêtre à part public function setCategory(\Jack\TestBundle\Entity\Category $category = null)
Enfin, je me suis rendu compte que tous les champs hérités sont nulls.
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10 object(Jack\TestBundle\Entity\CarAdvert)[388] protected 'model' => string 'Avensis' (length=7) protected 'make' => string 'TOYOTA' (length=6) protected 'price' => null protected 'id' => null protected 'category' => null protected 'title' => null protected 'content' => null protected 'published' => boolean true protected 'updatedAt' => null
Alors, je me demande où se trouve le problème:
Merci d'avance!
Partager