Bonjour,
J'ai un formulaire pour ajouter une application mobile.
Cette application contient:
BASE APPLICATION
- son titre
- sa description
- son logo
- son prix
- sa catégorie
BASE FICHIER
- la version du fichier
- le nom du fichier
Mon problème:
Lorsque je valide le formulaire, je tombe sur cette erreur:
Le problème est donc:An exception occurred while executing 'INSERT INTO gosoft_fichiers (version, dateajout, publication, apk, user_id, application_id) VALUES (?, ?, ?, ?, ?, ?)' with params {"1":1,"2":"2013-01-21 15:35:14","3":0,"4":null,"5":1,"6":46}:
SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'apk' cannot be null
- la valeur de "version" est toujours à 1 (voir message d'erreur) même si j'indique 3 dans le formulaire par exemple.
- le champ d'upload "apk" qui ne fonctionne pas (en effet l'apk ne s'upload pas). Pourtant j'ai utilisé le même principe que pour le logo de l'application qui lui s'upload bien.
Voici donc mes fichiers:
ApplicationType.php
FichierType.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 <?php namespace GoSoft\MainBundle\Form; use Symfony\Component\Form\AbstractType; use Symfony\Component\Form\FormBuilderInterface; use Symfony\Component\OptionsResolver\OptionsResolverInterface; class ApplicationType extends AbstractType { public function buildForm(FormBuilderInterface $builder, array $options) { $builder ->add('nom', 'text') ->add('description', 'textarea') ->add('prix', 'number') ->add('categorie', 'entity', array('class' => 'GoSoftMainBundle:Categorie','property' => 'nom',)) ->add('logo') ->add('fichier', 'collection', array('type' => new FichierType(), 'allow_add' => true, 'allow_delete' => false, 'by_reference' => false)) ; } public function setDefaultOptions(OptionsResolverInterface $resolver) { $resolver->setDefaults(array( 'data_class' => 'GoSoft\MainBundle\Entity\Application' )); } public function getName() { return 'gosoft_mainbundle_applicationtype'; } }
Application.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 <?php namespace GoSoft\MainBundle\Form; use Symfony\Component\Form\AbstractType; use Symfony\Component\Form\FormBuilderInterface; use Symfony\Component\OptionsResolver\OptionsResolverInterface; class FichierType extends AbstractType { public function buildForm(FormBuilderInterface $builder, array $options) { $builder ->add('version', 'number') ->add('apk') ; } public function setDefaultOptions(OptionsResolverInterface $resolver) { $resolver->setDefaults(array( 'data_class' => 'GoSoft\MainBundle\Entity\Fichier' )); } public function getName() { return 'gosoft_mainbundle_fichiertype'; } }
Fichier.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 <?php namespace GoSoft\MainBundle\Entity; use Doctrine\ORM\Mapping as ORM; use Symfony\Component\Validator\Constraints as Assert; /** * GoSoft\MainBundle\Entity\Application * * @ORM\Table(name="gosoft_applications") * @ORM\HasLifecycleCallbacks * @ORM\Entity(repositoryClass="GoSoft\MainBundle\Entity\ApplicationRepository") */ class Application { /** * @var integer $id * * @ORM\Column(name="id", type="integer") * @ORM\Id * @ORM\GeneratedValue(strategy="AUTO") */ private $id; /** * @var string $nom * * @ORM\Column(name="nom", type="string", length=255) * @Assert\MinLength(limit = 2) */ //@Assert\MinLength(limit = 2, message = "Le nom de famille doit avoir au moins {{ limit }} caractères") private $nom; /** * @ORM\ManyToOne(targetEntity="GoSoft\UserBundle\Entity\User", inversedBy="applications") * @ORM\JoinColumn(nullable=false) * @Assert\Type(type="GoSoft\UserBundle\Entity\User") */ private $user; /** * @var text $description * * @ORM\Column(name="description", type="text") * @Assert\NotBlank() */ private $description; /** * @var decimal $prix * * @ORM\Column(name="prix", type="decimal", scale=2) * @Assert\NotBlank() */ private $prix; /** * @var decimal $prixpromo * * @ORM\Column(name="prixpromo", type="decimal", scale=2) */ private $prixpromo; /** * @var datetime $dateajout * * @ORM\Column(name="dateajout", type="datetime") * @Assert\DateTime() */ private $dateajout; /** * @var boolean $publication * @ORM\Column(name="publication", type="boolean") */ private $publication; /** * @ORM\ManyToOne(targetEntity="GoSoft\MainBundle\Entity\Categorie", inversedBy="applications") * @ORM\JoinColumn(nullable=false) */ private $categorie; /** * @var string $logo * @Assert\File( maxSize = "1024k", mimeTypesMessage = "Please upload a valid Image") * @ORM\Column(name="logo", type="string", length=255) */ private $logo; public function __construct() { $this->dateajout = new \Datetime; $this->publication = false; $this->prixpromo = 0; $this->fichier = new \Doctrine\Common\Collections\ArrayCollection(); } /** * Get id * * @return integer */ public function getId() { return $this->id; } /** * Set nom * * @param string $nom * @return Application */ public function setNom($nom) { $this->nom = $nom; return $this; } /** * Get nom * * @return string */ public function getNom() { return $this->nom; } /** * Set description * * @param string $description * @return Application */ public function setDescription($description) { $this->description = $description; return $this; } /** * Get description * * @return string */ public function getDescription() { return $this->description; } /** * Set prix * * @param float $prix * @return GoSoftApplication */ public function setPrix($prix) { $this->prix = $prix; return $this; } /** * Get prix * * @return float */ public function getPrix() { return $this->prix; } /** * Set publication * * @param boolean $publication * @return Application */ public function setPublication($publication) { $this->publication = $publication; return $this; } /** * Get publication * * @return boolean */ public function getPublication() { return $this->publication; } /** * Set categorie * * @param GoSoft\MainBundle\Entity\GoSoftCategorie $categorie * @return GoSoftApplication */ public function setCategorie(\GoSoft\MainBundle\Entity\Categorie $categorie) { $this->categorie = $categorie; return $this; } /** * Get categorie * * @return GoSoft\MainBundle\Entity\GoSoftCategorie */ public function getCategorie() { return $this->categorie; } /** * Set prixpromo * * @param float $prixpromo * @return GoSoftApplication */ public function setPrixpromo($prixpromo) { $this->prixpromo = $prixpromo; return $this; } /** * Get prixpromo * * @return float */ public function getPrixpromo() { return $this->prixpromo; } /** * Set dateajout * * @param \DateTime $dateajout * @return Application */ public function setDateajout($dateajout) { $this->dateajout = $dateajout; return $this; } /** * Get dateajout * * @return \DateTime */ public function getDateajout() { return $this->dateajout; } /** * Set user * * @param GoSoft\UserBundle\Entity\User $user * @return Application */ public function setUser(\GoSoft\UserBundle\Entity\User $user) { $this->user = $user; return $this; } /** * Get user * * @return GoSoft\UserBundle\Entity\User */ public function getUser() { return $this->user; } /** * Add user * * @param GoSoft\UserBundle\Entity\User $user * @return Application */ public function addUser(\GoSoft\UserBundle\Entity\User $user) { $this->user[] = $user; return $this; } /** * Remove user * * @param GoSoft\UserBundle\Entity\User $user */ public function removeUser(\GoSoft\UserBundle\Entity\User $user) { $this->user->removeElement($user); } /** * Set logo * * @param string $logo * @return logo */ public function setLogo($logo) { $this->logo = $logo; return $this; } /** * Get logo * * @return string */ public function getLogo() { return $this->logo; } public function getFullLogoPath() { return null === $this->logo ? null : $this->getUploadRootDir().'/logo/'. $this->logo; } protected function getUploadRootDir() { // the absolute directory path where uploaded documents should be saved return __DIR__ . '/../../../../web/upload/'.$this->getId().'/'; } protected function getTmpUploadRootDir() { // the absolute directory path where uploaded documents should be saved return __DIR__ . '/../../../../web/upload/temp/'; } /** * @ORM\PrePersist() * @ORM\PreUpdate() */ public function uploadLogo() { // the file property can be empty if the field is not required if (null === $this->logo) { return; } if(!$this->id){ $this->logo->move($this->getTmpUploadRootDir(), $this->logo->getClientOriginalName()); }else{ $this->logo->move($this->getUploadRootDir(), $this->logo->getClientOriginalName()); } $this->setLogo($this->logo->getClientOriginalName()); } /** * @ORM\PostPersist() */ public function moveLogo() { if (null === $this->logo) { return; } if(!is_dir($this->getUploadRootDir())){ mkdir($this->getUploadRootDir()); } $chemin_final = $this->getUploadRootDir().'/logo/'; if(!is_dir($chemin_final)){ mkdir($chemin_final); } copy($this->getTmpUploadRootDir().$this->logo, $this->getFullLogoPath()); unlink($this->getTmpUploadRootDir().$this->logo); } /** * @ORM\PreRemove() */ public function removeLogo() { unlink($this->getFullLogoPath()); rmdir($this->getUploadRootDir()); } }
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 <?php namespace GoSoft\MainBundle\Entity; use Doctrine\ORM\Mapping as ORM; use Symfony\Component\Validator\Constraints as Assert; /** * GoSoft\MainBundle\Entity\Fichier * * @ORM\Table(name="gosoft_fichiers") * @ORM\HasLifecycleCallbacks * @ORM\Entity(repositoryClass="GoSoft\MainBundle\Entity\FichierRepository") */ class Fichier { /** * @var integer $id * * @ORM\Column(name="id", type="integer") * @ORM\Id * @ORM\GeneratedValue(strategy="AUTO") */ private $id; /** * @ORM\ManyToOne(targetEntity="GoSoft\UserBundle\Entity\User", inversedBy="fichiers") * @ORM\JoinColumn(nullable=false) * @Assert\Type(type="GoSoft\UserBundle\Entity\User") */ private $user; /** * @ORM\ManyToOne(targetEntity="GoSoft\MainBundle\Entity\Application", inversedBy="fichiers") * @ORM\JoinColumn(nullable=false) * @Assert\Type(type="GoSoft\MainBundle\Entity\Application") */ private $application; /** * @var decimal $version * * @ORM\Column(name="version", type="decimal", scale=2) */ private $version; /** * @var datetime $dateajout * * @ORM\Column(name="dateajout", type="datetime") * @Assert\DateTime() */ private $dateajout; /** * @var boolean $publication * @ORM\Column(name="publication", type="boolean") */ private $publication; /** * @var string $apk * @Assert\File( maxSize = "100M", mimeTypesMessage = "Please upload a valid Apk") * @ORM\Column(name="apk", type="string", length=255) */ private $apk; public function __construct() { $this->dateajout = new \Datetime; $this->publication = false; $this->version = 1.0; } /** * Get id * * @return integer */ public function getId() { return $this->id; } /** * Set publication * * @param boolean $publication * @return Fichier */ public function setPublication($publication) { $this->publication = $publication; return $this; } /** * Get publication * * @return boolean */ public function getPublication() { return $this->publication; } /** * Set dateajout * * @param \DateTime $dateajout * @return Fichier */ public function setDateajout($dateajout) { $this->dateajout = $dateajout; return $this; } /** * Get dateajout * * @return \DateTime */ public function getDateajout() { return $this->dateajout; } /** * Set version * * @param string $version * @return Fichier */ public function setVersion($version) { $this->version = $version; return $this; } /** * Get version * * @return string */ public function getVersion() { return $this->version; } /** * Set application * * @param GoSoft\MainBundle\Entity\Application $application * @return Fichier */ public function setApplication(\GoSoft\MainBundle\Entity\Application $application) { $this->application = $application; return $this; } /** * Get application * * @return GoSoft\MainBundle\Entity\Application */ public function getApplication() { return $this->application; } /** * Set user * * @param GoSoft\UserBundle\Entity\User $user * @return Application */ public function setUser(\GoSoft\UserBundle\Entity\User $user) { $this->user = $user; return $this; } /** * Get user * * @return GoSoft\UserBundle\Entity\User */ public function getUser() { return $this->user; } /** * Set apk * * @param string $apk * @return apk */ public function setApk($apk) { $this->apk = $apk; return $this; } /** * Get apk * * @return string */ public function getApk() { return $this->apk; } public function getFullApkPath() { return null === $this->apk ? null : $this->getUploadRootDir().'/apk/'. $this->apk; } protected function getUploadRootDir() { // the absolute directory path where uploaded documents should be saved return __DIR__ . '/../../../../web/upload/'.$this->getApplication()->getId().'/'; } protected function getTmpUploadRootDir() { // the absolute directory path where uploaded documents should be saved return __DIR__ . '/../../../../web/upload/temp/'; } /** * @ORM\PrePersist() * @ORM\PreUpdate() */ public function uploadApk() { // the file property can be empty if the field is not required if (null === $this->apk) { return; } if(!$this->id){ $this->apk->move($this->getTmpUploadRootDir(), $this->apk->getClientOriginalName()); }else{ $this->logo->move($this->getUploadRootDir(), $this->apk->getClientOriginalName()); } $this->setApk($this->apk->getClientOriginalName()); } /** * @ORM\PostPersist() */ public function moveApk() { if (null === $this->apk) { return; } if(!is_dir($this->getUploadRootDir())){ mkdir($this->getUploadRootDir()); } $chemin_final = $this->getUploadRootDir().'/apk/'; if(!is_dir($chemin_final)){ mkdir($chemin_final); } copy($this->getTmpUploadRootDir().$this->apk, $this->getFullApkPath()); unlink($this->getTmpUploadRootDir().$this->apk); } /** * @ORM\PreRemove() */ public function removeApk() { unlink($this->getFullApkPath()); rmdir($this->getUploadRootDir()); } }
Partager