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 :

An exception occurred while executing 'INSERT INTO [2.x]


Sujet :

Symfony PHP

  1. #1
    Membre régulier
    Profil pro
    Inscrit en
    Juillet 2007
    Messages
    159
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Juillet 2007
    Messages : 159
    Points : 88
    Points
    88
    Par défaut An exception occurred while executing 'INSERT INTO
    Hello,

    J'ai deux entité avec une relation ManyToOne bidirectionnelle c'est à dire que dans :
    Mon entité Honoraire j'ai :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    	/**
    	 * @ORM\ManyToOne(targetEntity="Nas\AppBundle\Entity\Specialite", inversedBy="honoraires")
    	 * @ORM\JoinColumn(nullable=false)
    	 */
    	private $specialite;
    Et dans mon entité Specialite j'ai :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
     
    	/**
    	* @ORM\OneToMany(targetEntity="Nas\AppBundle\Entity\Honoraire", mappedBy="specialite", cascade={"persist"})
    	*/
    	private $honoraires; // Ici honoraires prend un « s », car une spécialité a plusieurs honoraires !
    Ceci afin de faire un formulaire pour créer des spécialité et en même temps créer les honoraires qui vont bien. Dans mon formulaire "Specialite" je peux ajouter (imbriquer) x nombre de formulaire "Honoraire".
    Mais quand je valide mon formulaire j'ai une erreur :
    An exception occurred while executing 'INSERT INTO Honoraire (type, pourcentageFacture, pourcentageDevis, specialite_id) VALUES (?, ?, ?, ?)' with params ["tre", "123", null, null]:
    Je ne vois pas comment régler le problème... Besoin d'aide

    Je post le code de mon entité "Spécialite" afin de montrer comment je gère la relation avec la collection d'honoraire :
    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
    <?php
     
    namespace Nas\AppBundle\Entity;
     
    use Doctrine\ORM\Mapping as ORM;
    use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
     
    /**
     * Specialite
     *
     * @ORM\Table(name="specialite")
     * @ORM\Entity(repositoryClass="Nas\AppBundle\Entity\SpecialiteRepository")
     * @UniqueEntity (fields="specialite", message="Cette specialite existe deja.")
     */
    class Specialite
    {
        /**
         * @var integer
         *
         * @ORM\Column(name="id", type="integer")
         * @ORM\Id
         * @ORM\GeneratedValue(strategy="AUTO")
         */
        private $id;
     
        /**
         * @var string
         *
         * @ORM\Column(name="specialite", type="string", length=255)
         */
        private $specialite;
     
    	/**
    	* @ORM\OneToMany(targetEntity="Nas\AppBundle\Entity\Honoraire", mappedBy="specialite", cascade={"persist"})
    	*/
    	private $honoraires; // Ici honoraires prend un « s », car une spécialité a plusieurs honoraires !
     
        /**
         * Get id
         *
         * @return integer 
         */
        public function getId()
        {
            return $this->id;
        }
     
        /**
         * Set specialite
         *
         * @param string $specialite
         * @return Specialite
         */
        public function setSpecialite($specialite)
        {
            $this->specialite = $specialite;
     
            return $this;
        }
     
        /**
         * Get specialite
         *
         * @return string 
         */
        public function getSpecialite()
        {
            return $this->specialite;
        }
        /**
         * Constructor
         */
        public function __construct()
        {
            $this->honoraires = new \Doctrine\Common\Collections\ArrayCollection();
        }
     
        /**
         * Add honoraires
         *
         * @param \Nas\AppBundle\Entity\Honoraire $honoraires
         * @return Specialite
         */
        public function addHonoraire(\Nas\AppBundle\Entity\Honoraire $honoraire)
        {		
            $this->honoraires[] = $honoraire;
    		$honoraires->setSpecialite($this);
     
            return $this;
        }
     
        /**
         * Remove honoraires
         *
         * @param \Nas\AppBundle\Entity\Honoraire $honoraires
         */
        public function removeHonoraire(\Nas\AppBundle\Entity\Honoraire $honoraire)
        {
            $this->honoraires->removeElement($honoraires);
        }
     
        /**
         * Get honoraires
         *
         * @return \Doctrine\Common\Collections\Collection 
         */
        public function getHonoraires()
        {
            return $this->honoraires;
        }
    }
    Merci pour votre aide

    Ju

  2. #2
    Nouveau membre du Club
    Homme Profil pro
    Ingénieur développement logiciels
    Inscrit en
    Janvier 2010
    Messages
    25
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Finistère (Bretagne)

    Informations professionnelles :
    Activité : Ingénieur développement logiciels
    Secteur : High Tech - Produits et services télécom et Internet

    Informations forums :
    Inscription : Janvier 2010
    Messages : 25
    Points : 34
    Points
    34
    Par défaut
    Bonjour,

    Pas plus de détails sur l'exception?

    A première vue, tu essayes d'insérer une valeur NULL pour specialite_id, or specialite_id est définie comme "NOT NULL" dans ton entité honoraire (annotation: @ORM\JoinColumn(nullable=false) )

  3. #3
    Membre régulier
    Profil pro
    Inscrit en
    Juillet 2007
    Messages
    159
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Juillet 2007
    Messages : 159
    Points : 88
    Points
    88
    Par défaut
    Non pas vraiment d'informations supplémentaires sur l'erreur et puis c'est clair. Il indique bien que le champ specialite_id est vide. Par contre je ne comprends pas pourquoi... Je bloque dessus, frustration total !

    C'est comme si mon entité "Honoraire" n'était pas alimenté par l'entité "Spécialite".

  4. #4
    Nouveau membre du Club
    Homme Profil pro
    Ingénieur développement logiciels
    Inscrit en
    Janvier 2010
    Messages
    25
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Finistère (Bretagne)

    Informations professionnelles :
    Activité : Ingénieur développement logiciels
    Secteur : High Tech - Produits et services télécom et Internet

    Informations forums :
    Inscription : Janvier 2010
    Messages : 25
    Points : 34
    Points
    34
    Par défaut
    Peux-tu mettre ici le code de ton formulaire?

    Parce que l'exception est tout à fait normale, la question c'est pourquoi la valeur de specialité_id est nulle.

  5. #5
    Membre régulier
    Profil pro
    Inscrit en
    Juillet 2007
    Messages
    159
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Juillet 2007
    Messages : 159
    Points : 88
    Points
    88
    Par défaut
    Bonsoir,

    Alors mon SpecialiteType :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
        public function buildForm(FormBuilderInterface $builder, array $options)
        {
            $builder
                ->add('specialite', 'text')
    	    ->add('honoraires', 'collection', array('type' => new HonoraireType(),
    								'allow_add' =>true,
    								'allow_delete' =>true))
            ;
        }
    Et l'appel depuis mon SpecialiteController :
    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
    public function ajouterAction()
    	{
    		if (!$this->get('security.context')->isGranted('ROLE_ADMIN')) 
    		{
    			// Sinon on déclenche une exception « Accès interdit »
    			throw new AccessDeniedHttpException('Accès limité aux auteurs');
    		}
     
    		$specialite = new Specialite();
     
    		$form = $this->createForm(new SpecialiteType, $specialite);
     
    		$request = $this->get('request');
     
    		//si la requete est de type POST
    		if($request->getMethod() == 'POST')
    		{	
    			//lien requete <=> formulaire
    			$form->bind($request);
     
    			if($form->isValid())
    			{
    				$em = $this->getDoctrine()->getManager();
    				$em->persist($specialite);
    				var_dump($specialite);
    				print_r($specialite);
    				$em->flush();
     
    				return $this->redirect($this->generateUrl('nasApp_listeSpecialite'));
    			}
    		}
     
    		return $this->render('NasAppBundle:Specialite:ajouter.html.twig', array('form'=>$form->createView()));
    	}
    Désolé pour le var_dump et print_r je suis désespéré

  6. #6
    Membre éprouvé
    Homme Profil pro
    Inscrit en
    Juin 2011
    Messages
    725
    Détails du profil
    Informations personnelles :
    Sexe : Homme

    Informations forums :
    Inscription : Juin 2011
    Messages : 725
    Points : 1 050
    Points
    1 050
    Par défaut
    Bonjour,

    ça semble être un cas classique de owning et reverse side dans Doctrine

    http://www.developpez.net/forums/d11...y/#post6408197

    http://docs.doctrine-project.org/pro...d-inverse-side

    http://symfony.com/fr/doc/current/co...llections.html
    (section "Doctrine: Relations de Cascade et sauvegarde du côté « Inverse »")

  7. #7
    Membre régulier
    Profil pro
    Inscrit en
    Juillet 2007
    Messages
    159
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Juillet 2007
    Messages : 159
    Points : 88
    Points
    88
    Par défaut
    Bonjour,

    Les liens postés ne fonctionnent pas ...
    Mais je vais suivre la piste , merci.

  8. #8
    Membre régulier
    Profil pro
    Inscrit en
    Juillet 2007
    Messages
    159
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Juillet 2007
    Messages : 159
    Points : 88
    Points
    88
    Par défaut
    Par contre à aucun moment dans mon code j'appel la fonction addHonoraire de mon entité Specialite. C'est bien elle qui doit lier ma Specialite à mon Honoraire, non ?

  9. #9
    Membre éprouvé
    Homme Profil pro
    Inscrit en
    Juin 2011
    Messages
    725
    Détails du profil
    Informations personnelles :
    Sexe : Homme

    Informations forums :
    Inscription : Juin 2011
    Messages : 725
    Points : 1 050
    Points
    1 050
    Par défaut
    J'ai éditer mon précédent message avec les liens fonctionnels

    à aucun moment dans mon code j'appel la fonction addHonoraire
    Normalement le formulaire va l'appeler tout seul:
    http://symfony.com/blog/form-goodnes...n-improvements

    C'est bien elle qui doit lier ma Specialite à mon Honoraire, non ?
    Dans le 1er lien il y a 3 méthodes proposés afin de rendre cohérentes les relations entre les objets.
    J'y ajoute également celle-ci:
    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
     
    //SpecialiteType
        public function buildForm(FormBuilderInterface $builder, array $options)
        {
            $builder
                ->add('specialite', 'text')
    	    ->add('honoraires', 'collection', array( 'type' => new HonoraireType(),
                                                                    'options'=>array(
                                                                          'empty_data' => function (FormInterface $form) {
                                                                               $specialite=$form//le formulaire bindé à une entité honoraire
                                                                                                      ->getParent()//le formulaire bindé à la collection d'honoraires
                                                                                                       ->getParent()//le formulaire bindé à spécialité
                                                                                                       ->getData();//l'entité Specialité
                                                                               $honoraire=new Honoraire;
                                                                               $honoraire->setSpecialite($specialite);
                                                                               return $honoraire;
                                                                    }),
    								'allow_add' =>true,
    								'allow_delete' =>true))
            ;
        }
    à priori faire le traitement dans la méthode addHonoraire est la méthode la + propre au sens POO.


    parfois il faut modifier la méthode get ainsi
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
     
    public function getHonoraires(){
       return $this->honoraires->toArray();
    }
    parfois jouer avec l'option "by_reference" dans le form type.

  10. #10
    Membre régulier
    Profil pro
    Inscrit en
    Juillet 2007
    Messages
    159
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Juillet 2007
    Messages : 159
    Points : 88
    Points
    88
    Par défaut
    J'ai contrôlé mon code grâce au lien suivant : http://docs.doctrine-project.org/pro...d-inverse-side

    J'ai donc supprimé le cascade={"persist"} de mon entité Specialite :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    	/**
    	* @ORM\OneToMany(targetEntity="Nas\AppBundle\Entity\Honoraire", mappedBy="specialite")
    	*/
    	private $honoraires; // Ici honoraires prend un « s », car une spécialité a plusieurs honoraires !
    Le reste était ok
    => cela me donne une erreur :
    A new entity was found through the relationship 'Nas\AppBundle\Entity\Specialite#honoraires' that was not configured to cascade persist operations for entity: Nas\AppBundle\Entity\Honoraire
    Le lien : http://symfony.com/fr/doc/current/co...llections.html
    (section "Doctrine: Relations de Cascade et sauvegarde du côté « Inverse »")

    Concerne une relation ManyToMany ... moi c'est, ManyToOne - OneToMany

    Je vais regarder ton 1er lien merci a toi !!

  11. #11
    Membre régulier
    Profil pro
    Inscrit en
    Juillet 2007
    Messages
    159
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Juillet 2007
    Messages : 159
    Points : 88
    Points
    88
    Par défaut
    Hello,

    Ca fonctionne !!

    Je post mon code, histoire de dépanner quelqu'un si besoin.

    Entité Specialite OneToMany (entité inverse si j'ai bien compris):
    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
    <?php
     
    namespace Nas\AppBundle\Entity;
     
    use Doctrine\ORM\Mapping as ORM;
    use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
     
    /**
     * Specialite
     *
     * @ORM\Table(name="specialite")
     * @ORM\Entity(repositoryClass="Nas\AppBundle\Entity\SpecialiteRepository")
     * @UniqueEntity (fields="specialite", message="Cette specialite existe deja.")
     */
    class Specialite
    {
        /**
         * @var integer
         *
         * @ORM\Column(name="id", type="integer")
         * @ORM\Id
         * @ORM\GeneratedValue(strategy="AUTO")
         */
        private $id;
     
        /**
         * @var string
         *
         * @ORM\Column(name="specialite", type="string", length=255)
         */
        private $specialite;
     
    	/**
    	* @ORM\OneToMany(targetEntity="Nas\AppBundle\Entity\Honoraire", mappedBy="specialite", cascade={"persist"})
    	*/
    	private $honoraires; // Ici honoraires prend un « s », car une spécialité a plusieurs honoraires !
     
    	/**
         * Constructor
         */
        public function __construct()
        {
            $this->honoraires = new \Doctrine\Common\Collections\ArrayCollection();
        }
     
        /**
         * Get id
         *
         * @return integer 
         */
        public function getId()
        {
            return $this->id;
        }
     
        /**
         * Set specialite
         *
         * @param string $specialite
         * @return Specialite
         */
        public function setSpecialite($specialite)
        {
            $this->specialite = $specialite;
     
            return $this;
        }
     
        /**
         * Get specialite
         *
         * @return string 
         */
        public function getSpecialite()
        {
            return $this->specialite;
        }
     
        /**
         * Add honoraires
         *
         * @param \Nas\AppBundle\Entity\Honoraire $honoraires
         * @return Specialite
         */
        public function addHonoraire(\Nas\AppBundle\Entity\Honoraire $honoraires)
        {		
            $this->honoraires[] = $honoraires;
    		$honoraires->setSpecialite($this);
     
            return $this;
        }
     
        /**
         * Remove honoraires
         *
         * @param \Nas\AppBundle\Entity\Honoraire $honoraires
         */
        public function removeHonoraire(\Nas\AppBundle\Entity\Honoraire $honoraire)
        {
            $this->honoraires->removeElement($honoraires);
        }
     
        /**
         * Get honoraires
         *
         * @return \Doctrine\Common\Collections\Collection 
         */
        public function getHonoraires()
        {
            return $this->honoraires->toArray();
        }
    }
    entité Honoraire, ManyToOne (entité propriétaire):
    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
    <?php
     
    namespace Nas\AppBundle\Entity;
     
    use Doctrine\ORM\Mapping as ORM;
     
    /**
     * Honoraire
     *
     * @ORM\Table()
     * @ORM\Entity(repositoryClass="Nas\AppBundle\Entity\HonoraireRepository")
     */
    class Honoraire
    {
     
    	/**
    	 * @ORM\ManyToOne(targetEntity="Nas\AppBundle\Entity\Specialite", inversedBy="honoraires")
    	 * @ORM\JoinColumn(name="specialite_id", referencedColumnName="id")
    	 */
    	private $specialite;
     
        /**
         * @var integer
         *
         * @ORM\Column(name="id", type="integer")
         * @ORM\Id
         * @ORM\GeneratedValue(strategy="AUTO")
         */
        private $id;
     
        /**
         * @var string
         *
         * @ORM\Column(name="type", type="string", length=255)
         */
        private $type;
     
        /**
         *
         * @ORM\Column(name="pourcentageFacture", type="decimal", scale=2)
         */
        private $pourcentageFacture;
     
        /**
         *
         * @ORM\Column(name="pourcentageDevis", type="decimal", scale=2, nullable=true)
         */
        private $pourcentageDevis;
     
     
        /**
         * Get id
         *
         * @return integer 
         */
        public function getId()
        {
            return $this->id;
        }
     
        /**
         * Set type
         *
         * @param string $type
         * @return Honoraire
         */
        public function setType($type)
        {
            $this->type = $type;
     
            return $this;
        }
     
        /**
         * Get type
         *
         * @return string 
         */
        public function getType()
        {
            return $this->type;
        }
     
        /**
         * Set pourcentageFacture
         *
         * @param integer $pourcentageFacture
         * @return Honoraire
         */
        public function setPourcentageFacture($pourcentageFacture)
        {
            $this->pourcentageFacture = $pourcentageFacture;
     
            return $this;
        }
     
        /**
         * Get pourcentageFacture
         *
         * @return integer 
         */
        public function getPourcentageFacture()
        {
            return $this->pourcentageFacture;
        }
     
        /**
         * Set pourcentageDevis
         *
         * @param integer $pourcentageDevis
         * @return Honoraire
         */
        public function setPourcentageDevis($pourcentageDevis)
        {
            $this->pourcentageDevis = $pourcentageDevis;
     
            return $this;
        }
     
        /**
         * Get pourcentageDevis
         *
         * @return integer 
         */
        public function getPourcentageDevis()
        {
            return $this->pourcentageDevis;
        }
     
        /**
         * Set specialite
         *
         * @param \Nas\AppBundle\Entity\Specialite $specialite
         * @return Honoraire
         */
        public function setSpecialite(\Nas\AppBundle\Entity\Specialite $specialite)
        {
            $this->specialite = $specialite;
     
            return $this;
        }
     
        /**
         * Get specialite
         *
         * @return \Nas\AppBundle\Entity\Specialite 
         */
        public function getSpecialite()
        {
            return $this->specialite;
        }
    }
    Et voila

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

Discussions similaires

  1. Réponses: 0
    Dernier message: 06/01/2010, 22h05
  2. [AC-2003] Problème avec oDB.Execute"Insert into" erreur 3061
    Par SIGER_971 dans le forum Access
    Réponses: 5
    Dernier message: 08/07/2009, 11h54
  3. [Axis2] Exception occurred while trying to invoke service method
    Par wifsimster dans le forum Services Web
    Réponses: 3
    Dernier message: 06/06/2009, 20h03
  4. Réponses: 3
    Dernier message: 05/08/2008, 15h24
  5. [EJBQL] An exception occured while creating a query in EntityManager
    Par Asmod_D dans le forum Java EE
    Réponses: 3
    Dernier message: 30/01/2008, 10h39

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