IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Symfony PHP Discussion :

[Form] Contrainte d'intégrité quand je resoumets le formulaire [2.x]


Sujet :

Symfony PHP

  1. #1
    Membre du Club
    Homme Profil pro
    Chef de projet en SSII
    Inscrit en
    Juin 2006
    Messages
    45
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 51
    Localisation : France, Gironde (Aquitaine)

    Informations professionnelles :
    Activité : Chef de projet en SSII
    Secteur : High Tech - Multimédia et Internet

    Informations forums :
    Inscription : Juin 2006
    Messages : 45
    Points : 46
    Points
    46
    Par défaut [Form] Contrainte d'intégrité quand je resoumets le formulaire
    Bonjour à tous,
    J'ai créé un formulaire pour la saisie d'une annonce. La création fonctionne une fois, redirige vers la page de confirmation, mais si je reviens sur la page pour créer une 2ème annonce (form vierge, je ressaisis), j'ai un message d'erreur :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    An exception occurred while executing 'INSERT INTO Annonce (titre, description, telAnnonceur, mailAnnonceur, adresseAnnonceur, villeAnnonceur, troc, prix, lat, lng, photo, urlPartenaire, nomPartenaire, dateCreation, datePeremption, publication, dureeAnnonce, categorie_id) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)' with params {"1":"annonce5","2":"lsdkfsjdflskfj lsdkf jsqflkmqs f","3":null,"4":"bob@laposte.net","5":null,"6":"strasbourg","7":0,"8":0,"9":"48.583148","10":"7.747882","11":null,"12":null,"13":null,"14":"2013-03-25 21:44:38","15":"2013-06-25","16":1,"17":90,"18":1}:
     
    SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry '1' for key 'UNIQ_39E8AA79BCF5E72D'
    Je ne vois pas ce qui me manque dans mon formulaire pour permettre plusieurs saisies de suite...

    Ci-après mon 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
    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
    411
    412
    413
    414
    415
    416
    417
    418
    419
    420
    421
    422
    423
    424
    425
    426
    427
    428
    429
    430
    431
    432
    433
    434
    435
    436
    437
    438
    439
    440
    441
    442
    443
    444
    445
    446
    447
    448
    449
    450
    451
    452
    453
    454
    455
    456
    457
    458
    459
    460
    461
    462
    463
    464
    465
    466
    467
    468
    469
    470
    471
    472
    473
    474
    475
    476
    477
    478
    479
    480
    481
    482
    483
    484
    485
    486
    487
    488
    489
    490
    491
    492
    493
    494
    495
    496
    497
    498
    499
    500
    501
    502
    503
    504
    505
    506
    507
    508
    509
    510
    511
    512
    513
    514
    515
    516
    517
    518
    519
    520
    521
    522
    523
    524
    525
    526
    527
    528
    529
    530
    531
    532
    533
    534
    535
    536
    537
    538
    539
    540
    541
    542
    543
    544
    545
    546
    547
    548
    549
    550
    551
    552
    553
    554
    555
    556
    557
    558
    559
    560
    561
    562
    563
    564
    565
    566
    567
    568
    569
    570
    571
    572
    573
    574
    575
    576
    577
    578
    579
    580
    581
    582
    583
    584
    585
    586
    587
    588
    589
    590
    591
    592
    593
    <?php
     
    namespace Vbo\VegiswapBundle\Entity;
     
    use Doctrine\ORM\Mapping as ORM;
    use Doctrine\Common\Collections\ArrayCollection;
    use Symfony\Component\Validator\Constraints as Assert;
     
    /**
     * Annonce
     *
     * @ORM\Table()
     * @ORM\Entity(repositoryClass="Vbo\VegiswapBundle\Entity\AnnonceRepository")
     */
    class Annonce
    {
        /**
         * @var integer
         *
         * @ORM\Column(name="id", type="integer")
         * @ORM\Id
         * @ORM\GeneratedValue(strategy="AUTO")
         */
        private $id;
     
    	/**
    	* @Assert\NotBlank()
    	* @ORM\OneToOne(targetEntity="Vbo\VegiswapBundle\Entity\Categorie", cascade={"persist"})
    	*/
    	private $categorie;
     
        /**
         * @var string
         *
    	 * @Assert\NotBlank()
         * @ORM\Column(name="titre", type="text")
         */
        private $titre;
     
        /**
         * @var string
         *
    	 * @Assert\NotBlank()
         * @ORM\Column(name="description", type="text")
         */
        private $description;
     
        /**
         * @var string
         *
         * @ORM\Column(name="telAnnonceur", type="text", nullable=true)
         */
        private $telAnnonceur;
     
        /**
         * @var string
         *
         * @ORM\Column(name="mailAnnonceur", type="text", nullable=true)
         */
        private $mailAnnonceur;
     
        /**
         * @var string
         *
         * @ORM\Column(name="adresseAnnonceur", type="text", nullable=true)
         */
        private $adresseAnnonceur;
     
        /**
         * @var string
         *
    	 * @Assert\NotBlank()
         * @ORM\Column(name="villeAnnonceur", type="text")
         */
        private $villeAnnonceur;
     
        /**
         * @var boolean
         *
         * @ORM\Column(name="troc", type="boolean", nullable=true)
         */
        private $troc;
     
        /**
         * @var float
         *
    	 * @Assert\NotBlank()
         * @ORM\Column(name="prix", type="float")
         */
        private $prix;
     
        /**
         * @var float
         *
         * @ORM\Column(name="lat", type="float")
         */
        private $lat;
     
        /**
         * @var float
         *
         * @ORM\Column(name="lng", type="float")
         */
        private $lng;
     
        /**
         * @var string
         *
         * @ORM\Column(name="photo", type="text", nullable=true)
         */
        private $photo;
     
        /**
         * @var string
         *
         * @ORM\Column(name="urlPartenaire", type="text", nullable=true)
         */
        private $urlPartenaire;
     
        /**
         * @var string
         *
         * @ORM\Column(name="nomPartenaire", type="text", nullable=true)
         */
        private $nomPartenaire;
     
        /**
         * @var \DateTime
         *
    	 * @Assert\NotBlank()
    	 * @Assert\Type("\DateTime")
         * @ORM\Column(name="dateCreation", type="datetime")
         */
        private $dateCreation;
     
        /**
         * @var \DateTime
         *
    	 * @Assert\NotBlank()
    	 * @Assert\Type("\DateTime")
         * @ORM\Column(name="datePeremption", type="date")
         */
        private $datePeremption;
     
        /**
         * @var boolean
         *
         * @ORM\Column(name="publication", type="boolean")
         */
        private $publication;
     
        /**
         * @var integer
         *
         * @ORM\Column(name="dureeAnnonce", type="integer")
         */
        private $dureeAnnonce;
     
     
        /**
         * Get id
         *
         * @return integer 
         */
        public function getId()
        {
            return $this->id;
        }
     
        /**
         * Set titre
         *
         * @param string $titre
         * @return Annonce
         */
        public function setTitre($titre)
        {
            $this->titre = $titre;
     
            return $this;
        }
     
        /**
         * Get titre
         *
         * @return string 
         */
        public function getTitre()
        {
            return $this->titre;
        }
     
        /**
         * Set description
         *
         * @param string $description
         * @return Annonce
         */
        public function setDescription($description)
        {
            $this->description = $description;
     
            return $this;
        }
     
        /**
         * Get description
         *
         * @return string 
         */
        public function getDescription()
        {
            return $this->description;
        }
     
        /**
         * Set telAnnonceur
         *
         * @param string $telAnnonceur
         * @return Annonce
         */
        public function setTelAnnonceur($telAnnonceur)
        {
            $this->telAnnonceur = $telAnnonceur;
     
            return $this;
        }
     
        /**
         * Get telAnnonceur
         *
         * @return string 
         */
        public function getTelAnnonceur()
        {
            return $this->telAnnonceur;
        }
     
        /**
         * Set mailAnnonceur
         *
         * @param string $mailAnnonceur
         * @return Annonce
         */
        public function setMailAnnonceur($mailAnnonceur)
        {
            $this->mailAnnonceur = $mailAnnonceur;
     
            return $this;
        }
     
        /**
         * Get mailAnnonceur
         *
         * @return string 
         */
        public function getMailAnnonceur()
        {
            return $this->mailAnnonceur;
        }
     
        /**
         * Set adresseAnnonceur
         *
         * @param string $adresseAnnonceur
         * @return Annonce
         */
        public function setAdresseAnnonceur($adresseAnnonceur)
        {
            $this->adresseAnnonceur = $adresseAnnonceur;
     
            return $this;
        }
     
        /**
         * Get adresseAnnonceur
         *
         * @return string 
         */
        public function getAdresseAnnonceur()
        {
            return $this->adresseAnnonceur;
        }
     
        /**
         * Set villeAnnonceur
         *
         * @param string $villeAnnonceur
         * @return Annonce
         */
        public function setVilleAnnonceur($villeAnnonceur)
        {
            $this->villeAnnonceur = $villeAnnonceur;
     
            return $this;
        }
     
        /**
         * Get villeAnnonceur
         *
         * @return string 
         */
        public function getVilleAnnonceur()
        {
            return $this->villeAnnonceur;
        }
     
        /**
         * Set troc
         *
         * @param boolean $troc
         * @return Annonce
         */
        public function setTroc($troc)
        {
            $this->troc = $troc;
     
            return $this;
        }
     
        /**
         * Get troc
         *
         * @return boolean 
         */
        public function getTroc()
        {
            return $this->troc;
        }
     
        /**
         * Set prix
         *
         * @param float $prix
         * @return Annonce
         */
        public function setPrix($prix)
        {
            $this->prix = $prix;
     
            return $this;
        }
     
        /**
         * Get prix
         *
         * @return float 
         */
        public function getPrix()
        {
            return $this->prix;
        }
     
        /**
         * Set lat
         *
         * @param float $lat
         * @return Annonce
         */
        public function setLat($lat)
        {
            $this->lat = $lat;
     
            return $this;
        }
     
        /**
         * Get lat
         *
         * @return float 
         */
        public function getLat()
        {
            return $this->lat;
        }
     
        /**
         * Set lng
         *
         * @param float $lng
         * @return Annonce
         */
        public function setLng($lng)
        {
            $this->lng = $lng;
     
            return $this;
        }
     
        /**
         * Get lng
         *
         * @return float 
         */
        public function getLng()
        {
            return $this->lng;
        }
     
        /**
         * Set photo
         *
         * @param string $photo
         * @return Annonce
         */
        public function setPhoto($photo)
        {
            $this->photo = $photo;
     
            return $this;
        }
     
        /**
         * Get photo
         *
         * @return string 
         */
        public function getPhoto()
        {
            return $this->photo;
        }
     
        /**
         * Set urlPartenaire
         *
         * @param string $urlPartenaire
         * @return Annonce
         */
        public function setUrlPartenaire($urlPartenaire)
        {
            $this->urlPartenaire = $urlPartenaire;
     
            return $this;
        }
     
        /**
         * Get urlPartenaire
         *
         * @return string 
         */
        public function getUrlPartenaire()
        {
            return $this->urlPartenaire;
        }
     
        /**
         * Set nomPartenaire
         *
         * @param string $nomPartenaire
         * @return Annonce
         */
        public function setNomPartenaire($nomPartenaire)
        {
            $this->nomPartenaire = $nomPartenaire;
     
            return $this;
        }
     
        /**
         * Get nomPartenaire
         *
         * @return string 
         */
        public function getNomPartenaire()
        {
            return $this->nomPartenaire;
        }
     
        /**
         * Set dateCreation
         *
         * @param \DateTime $dateCreation
         * @return Annonce
         */
        public function setDateCreation($dateCreation)
        {
            $this->dateCreation = $dateCreation;
     
            return $this;
        }
     
        /**
         * Get dateCreation
         *
         * @return \DateTime 
         */
        public function getDateCreation()
        {
            return $this->dateCreation;
        }
     
        /**
         * Set datePeremption
         *
         * @param \DateTime $datePeremption
         * @return Annonce
         */
        public function setDatePeremption($datePeremption)
        {
            $this->datePeremption = $datePeremption;
     
            return $this;
        }
     
        /**
         * Get datePeremption
         *
         * @return \DateTime 
         */
        public function getDatePeremption()
        {
            return $this->datePeremption;
        }
        /**
         * Constructor
         */
        public function __construct()
        {
    		$this->publication = true;
    		$this->prix = 0;
    		$this->dateCreation = new \Datetime;
    		$this->datePeremption = new \Datetime;
        }
     
        /**
         * Set publication
         *
         * @param boolean $publication
         * @return Annonce
         */
        public function setPublication($publication)
        {
            $this->publication = $publication;
     
            return $this;
        }
     
        /**
         * Get publication
         *
         * @return boolean 
         */
        public function getPublication()
        {
            return $this->publication;
        }
     
        /**
         * Set categorie
         *
         * @param \Vbo\VegiswapBundle\Entity\Categorie $categorie
         * @return Annonce
         */
        public function setCategorie(\Vbo\VegiswapBundle\Entity\Categorie $categorie = null)
        {
            $this->categorie = $categorie;
     
            return $this;
        }
     
        /**
         * Get categorie
         *
         * @return \Vbo\VegiswapBundle\Entity\Categorie 
         */
        public function getCategorie()
        {
            return $this->categorie;
        }
     
        /**
         * Set dureeAnnonce
         *
         * @param integer $dureeAnnonce
         * @return Annonce
         */
        public function setDureeAnnonce($dureeAnnonce)
        {
            $this->dureeAnnonce = $dureeAnnonce;
     
            return $this;
        }
     
        /**
         * Get dureeAnnonce
         *
         * @return integer 
         */
        public function getDureeAnnonce()
        {
            return $this->dureeAnnonce;
        }
    }
    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
    <?php
     
    namespace Vbo\VegiswapBundle\Form\Type;
     
    use Symfony\Component\Form\AbstractType;
    use Symfony\Component\Form\FormBuilderInterface;
    use Symfony\Component\OptionsResolver\OptionsResolverInterface;
     
    class AnnonceType extends AbstractType
    {
        public function buildForm(FormBuilderInterface $builder, array $options)
        {
            $builder
                ->add('titre', 'text')
                ->add('description')
                ->add('telAnnonceur', 'text', array('required' => false))
                ->add('mailAnnonceur','email', array('required' => false))
                ->add('adresseAnnonceur', 'text', array('required' => false))
                ->add('villeAnnonceur', 'text')
                ->add('troc','checkbox', array('required'  => false))
                ->add('prix','money')
                ->add('lat','hidden')
                ->add('lng','hidden')
                ->add('photo', 'file', array('required'  => false))
    			->add('dureeAnnonce',
    					  'choice',
    					  array('choices' => array('5' => '5 jours', '15' => '15 jours', '30' => '1 mois', '90' => '3 mois'),
    					  'required'  => true,
    					  'data' => '15',
    					 ))
    			->add('categorie', 'entity', array('class' => 'VboVegiswapBundle:Categorie',
    													'multiple' => false,
    													'expanded' => false,
    													))
            ;
        }
     
        public function setDefaultOptions(OptionsResolverInterface $resolver)
        {
            $resolver->setDefaults(array(
                'data_class' => 'Vbo\VegiswapBundle\Entity\Annonce'
            ));
        }
     
        public function getName()
        {
            return 'vbo_vegiswapbundle_annoncetype';
        }
    }
    ?>
    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
    <?php
     
    namespace Vbo\VegiswapBundle\Controller;
     
    use Symfony\Bundle\FrameworkBundle\Controller\Controller;
    use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
    use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
    use Vbo\VegiswapBundle\Form\Type\AnnonceType;
    use Vbo\VegiswapBundle\Entity\Annonce;
     
    class AdsController extends Controller
    {
        /**
         * @Route("/ads", name="ads")
         * @Template()
         */
        public function indexAction()
        {
    		$annonce = new Annonce;
    		$form = $this->createForm(new AnnonceType, $annonce);
            $request = $this->get('request');
     
            // Si methode = POST, le formulaire vient d'être soumis
            if ($request->getMethod() == 'POST')
            {
                // Récupère les valeurs
                $form->bindRequest($request);
     
    			if ($form->isValid()) {
    				$annonce = $form->getData();
    				// COMPLETER POUR FINALISER L'OBJET ANNONCE
     
    				switch ($annonce->getDureeAnnonce()) {
    					case 5:
    						$datePeremption = new \DateTime('+5 day');
    						break;
    					case 15:
    						$datePeremption = new \DateTime('+15 day');
    						break;
    					case 30:
    						$datePeremption = new \DateTime('+1 month');
    						break;
    					case 90:
    						$datePeremption = new \DateTime('+3 month');
    						break;
    				}
     
    				$annonce->setDatePeremption($datePeremption);
     
    				// Enregistrement en base de données
    				$em = $this->getDoctrine()->getManager();
    				$em->persist($annonce);
    				$em->flush();
     
    				// Redirige vers le template de confirmation de dépôt d'annonce
    				return $this->render('VboVegiswapBundle:Ads:confirm.html.twig');
    			}
            }
     
    		// Sinon, soit c'est le 1er appel à la page (request en GET),
    		// soit le formulaire n'est pas valide,
    		// (ré)affichage du formulaire
    		return $this->render('VboVegiswapBundle:Ads:index.html.twig', array(
                'form' => $form->createView(),
            ));
        }
    }

  2. #2
    Membre expert
    Avatar de dukoid
    Homme Profil pro
    Développeur informatique
    Inscrit en
    Novembre 2012
    Messages
    2 100
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Activité : Développeur informatique

    Informations forums :
    Inscription : Novembre 2012
    Messages : 2 100
    Points : 3 004
    Points
    3 004
    Par défaut
    comme ça, je dirais que c'est directement dans ta base, il y a un champs à UNIQ ça doit être la clé primaire.

    tu enregistre une fois avec la clé 1 et une seconde fois encore avec la clé 1 et donc -> problemo

  3. #3
    Membre du Club
    Homme Profil pro
    Chef de projet en SSII
    Inscrit en
    Juin 2006
    Messages
    45
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 51
    Localisation : France, Gironde (Aquitaine)

    Informations professionnelles :
    Activité : Chef de projet en SSII
    Secteur : High Tech - Multimédia et Internet

    Informations forums :
    Inscription : Juin 2006
    Messages : 45
    Points : 46
    Points
    46
    Par défaut
    J'avais bien évidemment vérifié la base MySQL, mais je n'ai regardé que la clé 'primaire' et pas la colonne 'Unique'.
    C'est la colonne 'categorie_id' qui est 'Uniq', car j'ai mis une relation OneToOne dans l'entity.

    C'est plutôt une relation OneToMany (ou ManyToOne) que j'aurais dû mettre !

    Merci !

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. Merise : Contrainte d'intégrite fonctionnelle
    Par new_wave dans le forum Décisions SGBD
    Réponses: 2
    Dernier message: 22/06/2022, 11h51
  2. contrainte d'intégrité super dur a gérer !
    Par RockLee69 dans le forum Oracle
    Réponses: 3
    Dernier message: 30/11/2005, 15h02
  3. Réponses: 5
    Dernier message: 26/10/2005, 14h43
  4. [debutant] Contraintes d'intégrité définies sur un objet
    Par maysa dans le forum MS SQL Server
    Réponses: 2
    Dernier message: 25/05/2004, 14h57
  5. Question sur les contraintes d'intégrités
    Par eGGyyS dans le forum PostgreSQL
    Réponses: 3
    Dernier message: 27/04/2004, 13h51

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo