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 :

Validation formulaire manytomany


Sujet :

Symfony PHP

  1. #1
    Membre à l'essai
    Homme Profil pro
    Développeur Web
    Inscrit en
    Janvier 2013
    Messages
    17
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Activité : Développeur Web
    Secteur : High Tech - Multimédia et Internet

    Informations forums :
    Inscription : Janvier 2013
    Messages : 17
    Points : 12
    Points
    12
    Par défaut Validation formulaire manytomany
    Bonjour à tous,

    Je fais appel à vous car je ne trouve pas la source de mon erreur.

    Je suis entreint de développer une sorte d'appli qui permet de gérer différents genre de document comme les factures, avoir, etc ...
    Je souhaite donc qu'on puisse crée un document avec plusieurs documents.
    J'ai donc une entite document :
    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
    <?php
     
    namespace Sdz\BlogBundle\Entity;
     
    use Doctrine\ORM\Mapping as ORM;
    use Symfony\Component\Validator\Constraints as Assert;
    use Doctrine\Common\Collections\ArrayCollection;
     
    /**
     * Documents
     *
     * @ORM\Table(name="documents")
     * @ORM\Entity
     */
    class Documents
    {
        /**
         * @var integer
         *
         * @ORM\Column(name="id_document", type="integer", nullable=false)
         * @ORM\Id
         * @ORM\GeneratedValue(strategy="IDENTITY")
         */
        private $idDocument;
     
        /**
         * @var \DateTime
         *
         * @ORM\Column(name="date", type="datetime", nullable=false)
         */
        private $date;
     
        /**
         * @var float
         *
         * @ORM\Column(name="prix", type="float", nullable=false)
         */
        private $prix;
     
        /**
         * @var \Documents
         *
         * @ORM\ManyToOne(targetEntity="Documents")
         * @ORM\JoinColumns({
         *   @ORM\JoinColumn(name="id_ref_facture", referencedColumnName="id_document")
         * })
         */
        private $idRefFacture;
     
        /**
         * @var \User
         *
         * @ORM\ManyToOne(targetEntity="User")
         * @ORM\JoinColumns({
         *   @ORM\JoinColumn(name="id_user", referencedColumnName="id_user")
         * })
         */
        private $idUser;
     
        /**
         * @var \TypeDocument
         *
         * @ORM\ManyToOne(targetEntity="TypeDocument")
         * @ORM\JoinColumns({
         *   @ORM\JoinColumn(name="id_type_document", referencedColumnName="id_type_document")
         * })
         */
        private $idTypeDocument;
     
        /**
         * @var \SocieteClient
         *
         * @ORM\ManyToOne(targetEntity="SocieteClient")
         * @ORM\JoinColumns({
         *   @ORM\JoinColumn(name="id_societe", referencedColumnName="id_societe")
         * })
         */
        private $idSociete;
     
    	 /**
         * @var \Produit
         *
         * @ORM\ManyToMany(targetEntity="Produit", cascade={"persist"})
         *
         */
        protected $products;
     
     
    	public function __construct()
        {
            $this->products = new ArrayCollection();
        }
        /**
         * Get idDocument
         *
         * @return integer 
         */
        public function getIdDocument()
        {
            return $this->idDocument;
        }
     
        /**
         * Set date
         *
         * @param \DateTime $date
         * @return Documents
         */
        public function setDate($date)
        {
            $this->date = $date;
     
            return $this;
        }
     
        /**
         * Get date
         *
         * @return \DateTime 
         */
        public function getDate()
        {
            return $this->date;
        }
     
        /**
         * Set prix
         *
         * @param float $prix
         * @return Documents
         */
        public function setPrix($prix)
        {
            $this->prix = $prix;
     
            return $this;
        }
     
        /**
         * Get prix
         *
         * @return float 
         */
        public function getPrix()
        {
            return $this->prix;
        }
     
        /**
         * Set idRefFacture
         *
         * @param \Sdz\BlogBundle\Entity\Documents $idRefFacture
         * @return Documents
         */
        public function setIdRefFacture(\Sdz\BlogBundle\Entity\Documents $idRefFacture = null)
        {
            $this->idRefFacture = $idRefFacture;
     
            return $this;
        }
     
        /**
         * Get idRefFacture
         *
         * @return \Sdz\BlogBundle\Entity\Documents 
         */
        public function getIdRefFacture()
        {
            return $this->idRefFacture;
        }
     
        /**
         * Set idUser
         *
         * @param \Sdz\BlogBundle\Entity\User $idUser
         * @return Documents
         */
        public function setIdUser(\Sdz\BlogBundle\Entity\User $idUser = null)
        {
            $this->idUser = $idUser;
     
            return $this;
        }
     
        /**
         * Get idUser
         *
         * @return \Sdz\BlogBundle\Entity\User 
         */
        public function getIdUser()
        {
            return $this->idUser;
        }
     
        /**
         * Set idTypeDocument
         *
         * @param \Sdz\BlogBundle\Entity\TypeDocument $idTypeDocument
         * @return Documents
         */
        public function setIdTypeDocument(\Sdz\BlogBundle\Entity\TypeDocument $idTypeDocument = null)
        {
            $this->idTypeDocument = $idTypeDocument;
     
            return $this;
        }
     
        /**
         * Get idTypeDocument
         *
         * @return \Sdz\BlogBundle\Entity\TypeDocument 
         */
        public function getIdTypeDocument()
        {
            return $this->idTypeDocument;
        }
     
        /**
         * Set idSociete
         *
         * @param \Sdz\BlogBundle\Entity\SocieteClient $idSociete
         * @return Documents
         */
        public function setIdSociete(\Sdz\BlogBundle\Entity\SocieteClient $idSociete = null)
        {
            $this->idSociete = $idSociete;
     
            return $this;
        }
     
        /**
         * Get idSociete
         *
         * @return \Sdz\BlogBundle\Entity\SocieteClient 
         */
        public function getIdSociete()
        {
            return $this->idSociete;
        }
     
    	public function getProducts()
        {
            return $this->products;
        }
        /**
         * Set products
         *
         * @param \Sdz\BlogBundle\Entity\Produit $idSociete
         * @return Produit
         */
        public function setProducts(ArrayCollection $products)
        {
            $this->products = $products;
        }
     
        /**
         * Set idDocument
         *
         * @param \Sdz\BlogBundle\Entity\Documents $iddocument
         * @return Documents
         */
        public function setIdDocument(\Sdz\BlogBundle\Entity\Documents $iddocument=null){
    	$this->idDocument=$iddocument;
    	return $this;
       }
    }
    et une entite produit :
    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
    <?php
     
    namespace Sdz\BlogBundle\Entity;
     
    use Doctrine\ORM\Mapping as ORM;
     
    /**
     * Produit
     *
     * @ORM\Table(name="produit")
     * @ORM\Entity
     */
    class Produit
    {
        /**
         * @var integer
         *
         * @ORM\Column(name="id_produit", type="integer", nullable=false)
         * @ORM\Id
         * @ORM\GeneratedValue(strategy="IDENTITY")
         */
        private $idProduit;
     
        /**
         * @var string
         *
         * @ORM\Column(name="name_product", type="string", length=100, nullable=false)
         */
        private $nameProduct;
     
        /**
         * @var float
         *
         * @ORM\Column(name="price_product_ht", type="float", nullable=false)
         */
        private $priceProductHt;
     
        /**
         * @var \Tva
         *
         * @ORM\ManyToOne(targetEntity="Tva")
         * @ORM\JoinColumns({
         *   @ORM\JoinColumn(name="id_tva", referencedColumnName="id_tva")
         * })
         */
        private $idTva;
     
     
     
        /**
         * Get idProduit
         *
         * @return integer 
         */
        public function getIdProduit()
        {
            return $this->idProduit;
        }
     
        /**
         * Set nameProduct
         *
         * @param string $nameProduct
         * @return Produit
         */
        public function setNameProduct($nameProduct)
        {
            $this->nameProduct = $nameProduct;
     
            return $this;
        }
     
        /**
         * Get nameProduct
         *
         * @return string 
         */
        public function getNameProduct()
        {
            return $this->nameProduct;
        }
     
        /**
         * Set priceProductHt
         *
         * @param float $priceProductHt
         * @return Produit
         */
        public function setPriceProductHt($priceProductHt)
        {
            $this->priceProductHt = $priceProductHt;
     
            return $this;
        }
     
        /**
         * Get priceProductHt
         *
         * @return float 
         */
        public function getPriceProductHt()
        {
            return $this->priceProductHt;
        }
     
        /**
         * Set idTva
         *
         * @param \Sdz\BlogBundle\Entity\Tva $idTva
         * @return Produit
         */
        public function setIdTva(\Sdz\BlogBundle\Entity\Tva $idTva = null)
        {
            $this->idTva = $idTva;
     
            return $this;
        }
     
        /**
         * Get idTva
         *
         * @return \Sdz\BlogBundle\Entity\Tva 
         */
        public function getIdTva()
        {
            return $this->idTva;
        }
    }
    Pour le formulaire, j'ai suivis un tuto qui m'a montré créer un formulaire imbriqué :

    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
    <?php
     
    namespace Sdz\BlogBundle\Form;
     
    use Symfony\Component\Form\AbstractType;
    use Symfony\Component\Form\FormBuilderInterface;
    use Symfony\Component\OptionsResolver\OptionsResolverInterface;
     
    class DocumentsType extends AbstractType
    {
        public function buildForm(FormBuilderInterface $builder, array $options)
        {
            $builder
                ->add('date','date')
                ->add('prix','integer')
                ->add('idDocument','entity',array(
                    'class'=>'SdzBlogBundle:Documents',
                    'property'=>'idDocument',
                    'label'=>'document ref'
                ))
                ->add('idTypeDocument','entity',array(
                    'class' => 'SdzBlogBundle:TypeDocument',
                    'property' => 'nameDocument',
                    'label'=>'Type de document '
                ))
                ->add('idSociete','entity',array(
                    'class'=>'SdzBlogBundle:SocieteClient',
                    'property'=>'nameSociete',
                    'label'=>'Societe'
                ))
                ->add('idUser','entity',array(
                    'class'=>'SdzBlogBundle:User',
                    'property'=>'lastname',
                    'label'=>'Utilisateur'
                ))
    			->add('products','collection', array(
    					'type'=> new ProduitType(),
    					'allow_add' => true,
    					'by_reference' => false,
    			));
     
     
            ;
        }
     
        public function setDefaultOptions(OptionsResolverInterface $resolver)
        {
            $resolver->setDefaults(array(
                'data_class' => 'Sdz\BlogBundle\Entity\Documents'
            ));
        }
     
        public function getName()
        {
            return 'sdz_blogbundle_documentstype';
        }
    }
    Jusqu'ici tout va bien. Mais quand je valide mon formulaire
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    Method "name_product" for object "Symfony\Component\Form\FormView" does not exist in SdzBlogBundle:Blog:ajouter.html.twig at line 82
    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
    <div id="date" class="date">
    			{{ form_row(form.date) }}
    			</div>
    			<div id="prix" class="prix">
    				{{ form_row(form.prix) }}
    			</div>
    			<div id="idDocument" class="idDocument">
    				{{ form_row(form.idDocument) }}
    			</div>
    			<div id="idTypeDocument" class="idTypeDocument">
    				{{ form_row(form.idTypeDocument) }}
    			</div>
    			<div id="idSociete" class="idSociete">
    				{{ form_row(form.idSociete) }}
    			</div>
    			<div id="idSociete" class="idSociete">
    				{{ form_row(form.idSociete) }}
    			</div>
    			<h4>produit</h4>
    			<ul class="products" data-prototype="{{ form_widget(form.products.vars.prototype)|e }}">
    			{# itère sur chaque tag existant et affiche son unique champ : name #}
    			{% for tag in form.products %}				
    				<li>{{ form_row(tag.name_product) }}</li>
    			{% endfor %}
    			</ul>
    J'ai déboguée un peu avec des die, etc ... et j'ai vu que mon formulaire n'est pas valid dans le 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
    if( $request->getMethod() == 'POST' )
            {
     
                $form->bind($request);
     
                if( $form->isValid() )
                {
     
                    $em = $this->getDoctrine()->getEntityManager();
                    $em->persist($doc);
                    $em->flush();
                    return $this->redirect( $this->generateUrl('sdzblog_accueil')
                    );
                }
            }
    L'affichage de mon formulaire fonctionne, c'est la validation qui me pose problème.

    Voila, je me remet à vous. Si vous pouviez me guider vers une solution, ca serait sympa

    Au plaisir,
    RYL

  2. #2
    Membre régulier
    Profil pro
    Inscrit en
    Mai 2013
    Messages
    60
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Mai 2013
    Messages : 60
    Points : 80
    Points
    80
    Par défaut
    Bonjour,

    Une petite question pour te guider :
    Dans ton entité "Product", as-tu une méthode "getName_Product()"?

  3. #3
    Membre à l'essai
    Homme Profil pro
    Développeur Web
    Inscrit en
    Janvier 2013
    Messages
    17
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Activité : Développeur Web
    Secteur : High Tech - Multimédia et Internet

    Informations forums :
    Inscription : Janvier 2013
    Messages : 17
    Points : 12
    Points
    12
    Par défaut
    Bonjour,

    Non j'ai une méthode getNameProduct(). J'ai compris d'ou venait le problème. J'suis vraiment étourdis
    j'ai remplacé ma variable par nameProduct est tout s'est déroulé normalement. Merci frfrance31

    A part un détail, les noms des champs de ma table "association" ne correspondait pas à ceux chercher par symfony.

    Par exemple : Symfony demandait documents_id alors que le champ de ma table etait id_documents.

    Je l'ai remplacé par les champs demandé mais je pense pas que ca soit la meilleur des manières. Est-ce que c'est un fonctionnement par défaut ? Une norme adopté par sf ?

  4. #4
    Membre à l'essai
    Homme Profil pro
    Développeur Web
    Inscrit en
    Janvier 2013
    Messages
    17
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Activité : Développeur Web
    Secteur : High Tech - Multimédia et Internet

    Informations forums :
    Inscription : Janvier 2013
    Messages : 17
    Points : 12
    Points
    12
    Par défaut
    Bonjour,

    Je reviens vers vous après des recherches qui ne menaient pas à grand chose :/. J'ai un problème d'édition de mon entité Documents.

    Mais je pense qu'il vient de la manière dont je gère mes formulaires. Dite moi si je me trompe mais je pars du fait que j'utilise une relation ManyToMany entre Documents et Produit.

    J'ai trois entité qui me permettent de gérer les facture/devis/avenant et l'ajout de produit pour le type facture et devis.

    Documents :

    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
    <?php
     
    namespace Sdz\BlogBundle\Entity;
     
    use Doctrine\ORM\Mapping as ORM;
    use Symfony\Component\Validator\Constraints as Assert;
    use Doctrine\Common\Collections\ArrayCollection;
     
    /**
     * Documents
     *
     * @ORM\Table(name="documents")
     * @ORM\Entity(repositoryClass="Sdz\BlogBundle\Repository\DocumentsRepository")
     */
    class Documents
    {
        /**
         * @var integer
         *
         * @ORM\Column(name="id_document", type="integer", nullable=false)
         * @ORM\Id
         * @ORM\GeneratedValue(strategy="IDENTITY")
         */
        private $idDocument;
     
        /**
         * @var \DateTime
         *
         * @ORM\Column(name="date", type="datetime", nullable=false)
         */
        private $date;
     
        /**
         * @var float
         *
         * @ORM\Column(name="prix", type="float", nullable=false)
         */
        private $prix;
     
        /**
         * @var \Documents
         *
         * @ORM\ManyToOne(targetEntity="Documents")
         * @ORM\JoinColumns({
         *   @ORM\JoinColumn(name="id_ref_facture", referencedColumnName="id_document", nullable=true)
         * })
         */
        private $idRefFacture;
     
        /**
         * @var \User
         *
         * @ORM\ManyToOne(targetEntity="User")
         * @ORM\JoinColumns({
         *   @ORM\JoinColumn(name="id_user", referencedColumnName="id_user")
         * })
         */
        private $idUser;
     
        /**
         * @var \TypeDocument
         *
         * @ORM\ManyToOne(targetEntity="TypeDocument")
         * @ORM\JoinColumns({
         *   @ORM\JoinColumn(name="id_type_document", referencedColumnName="id_type_document")
         * })
         */
        private $idTypeDocument;
     
        /**
         * @var \SocieteClient
         *
         * @ORM\ManyToOne(targetEntity="SocieteClient")
         * @ORM\JoinColumns({
         *   @ORM\JoinColumn(name="id_societe", referencedColumnName="id_societe")
         * })
         */
        private $idSociete;
     
    	 /**
         * @var \Produit
         *
         * @ORM\ManyToMany(targetEntity="Produit", cascade={"persist"},mappedBy="documents")
         *
         */
        protected $products;
     
     
    	public function __construct()
        {
            $this->products = new ArrayCollection();
        }
        /**
         * Get idDocument
         *
         * @return integer 
         */
        public function getIdDocument()
        {
            return $this->idDocument;
        }
     
        /**
         * Set date
         *
         * @param \DateTime $date
         * @return Documents
         */
        public function setDate($date)
        {
            $this->date = $date;
     
            return $this;
        }
     
        /**
         * Get date
         *
         * @return \DateTime 
         */
        public function getDate()
        {
            return $this->date;
        }
     
        /**
         * Set prix
         *
         * @param float $prix
         * @return Documents
         */
        public function setPrix($prix)
        {
            $this->prix = $prix;
     
            return $this;
        }
     
        /**
         * Get prix
         *
         * @return float 
         */
        public function getPrix()
        {
            return $this->prix;
        }
     
        /**
         * Set idRefFacture
         *
         * @param \Sdz\BlogBundle\Entity\Documents $idRefFacture
         * @return Documents
         */
        public function setIdRefFacture(\Sdz\BlogBundle\Entity\Documents $idRefFacture = null)
        {
            $this->idRefFacture = $idRefFacture;
     
            return $this;
        }
     
        /**
         * Get idRefFacture
         *
         * @return \Sdz\BlogBundle\Entity\Documents 
         */
        public function getIdRefFacture()
        {
            return $this->idRefFacture;
        }
     
        /**
         * Set idUser
         *
         * @param \Sdz\BlogBundle\Entity\User $idUser
         * @return Documents
         */
        public function setIdUser(\Sdz\BlogBundle\Entity\User $idUser = null)
        {
            $this->idUser = $idUser;
     
            return $this;
        }
     
        /**
         * Get idUser
         *
         * @return \Sdz\BlogBundle\Entity\User 
         */
        public function getIdUser()
        {
            return $this->idUser;
        }
     
        /**
         * Set idTypeDocument
         *
         * @param \Sdz\BlogBundle\Entity\TypeDocument $idTypeDocument
         * @return Documents
         */
        public function setIdTypeDocument(\Sdz\BlogBundle\Entity\TypeDocument $idTypeDocument = null)
        {
            $this->idTypeDocument = $idTypeDocument;
     
            return $this;
        }
     
        /**
         * Get idTypeDocument
         *
         * @return \Sdz\BlogBundle\Entity\TypeDocument 
         */
        public function getIdTypeDocument()
        {
            return $this->idTypeDocument;
        }
     
        /**
         * Set idSociete
         *
         * @param \Sdz\BlogBundle\Entity\SocieteClient $idSociete
         * @return Documents
         */
        public function setIdSociete(\Sdz\BlogBundle\Entity\SocieteClient $idSociete = null)
        {
            $this->idSociete = $idSociete;
     
            return $this;
        }
     
        /**
         * Get idSociete
         *
         * @return \Sdz\BlogBundle\Entity\SocieteClient 
         */
        public function getIdSociete()
        {
            return $this->idSociete;
        }
     
        /**
         * Get products
         *
         * @return \Sdz\BlogBundle\Entity\Produit
         */
    	public function getProducts()
        {
            return $this->products;
        }
        /**
         * Set products
         *
         * @param \Sdz\BlogBundle\Entity\Produit $products
         * @return Produit
         */
        public function setProducts(ArrayCollection $products)
        {
            $this->products = $products;
            return $this;
        }
     
        /**
         * Set idDocument
         *
         * @param \Sdz\BlogBundle\Entity\Documents $iddocument
         * @return Documents
         */
        public function setIdDocument(\Sdz\BlogBundle\Entity\Documents $iddocument=null){
            $this->idDocument=$iddocument;
            return $this;
       }
     
        /**
         * Add products
         *
         * @param \Sdz\BlogBundle\Entity\Produit $prod
         * @return Documents
         */
       public function addProduct(Produit $prod){
     
           $this->products[]=$prod;
     
           return $this;
       }
    }
    DocumentsProduit qui fait la liaison entre Documents et Produit
    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
    <?php
     
    namespace Sdz\BlogBundle\Entity;
     
    use Doctrine\ORM\Mapping as ORM;
     
    /**
     * DocumentProduit
     *
     * @ORM\Table(name="documents_produit")
     * @ORM\Entity
     */
    class DocumentProduit
    {
        /**
         * @var integer
         *
         * @ORM\Column(name="id_content", type="integer", nullable=false)
         * @ORM\Id
         * @ORM\GeneratedValue(strategy="IDENTITY")
         */
        private $idContent;
     
        /**
         * @var integer
         *
         * @ORM\Column(name="quantite", type="integer", nullable=false)
         */
        private $quantite;
     
        /**
         * @var float
         *
         * @ORM\Column(name="prix_total_ht", type="float", nullable=false)
         */
        private $prixTotalHt;
     
        /**
         * @var float
         *
         * @ORM\Column(name="prix_total_ttc", type="float", nullable=false)
         */
        private $prixTotalTtc;
     
        /**
         * @var \Documents
         *
         * @ORM\ManyToOne(targetEntity="Documents")
         * @ORM\JoinColumns({
         *   @ORM\JoinColumn(name="documents_id", referencedColumnName="id_document")
         * })
         */
        private $idDocument;
     
        /**
         * @var \Produit
         *
         * @ORM\ManyToOne(targetEntity="Produit")
         * @ORM\JoinColumns({
         *   @ORM\JoinColumn(name="produit_id", referencedColumnName="id_produit")
         * })
         */
        private $produitId;
     
     
        /**
         * Get idContent
         *
         * @return integer 
         */
        public function getIdContent()
        {
            return $this->idContent;
        }
     
        /**
         * Set quantite
         *
         * @param integer $quantite
         * @return DocumentProduit
         */
        public function setQuantite($quantite)
        {
            $this->quantite = $quantite;
     
            return $this;
        }
     
        /**
         * Get quantite
         *
         * @return integer 
         */
        public function getQuantite()
        {
            return $this->quantite;
        }
     
        /**
         * Set prixTotalHt
         *
         * @param float $prixTotalHt
         * @return DocumentProduit
         */
        public function setPrixTotalHt($prixTotalHt)
        {
            $this->prixTotalHt = $prixTotalHt;
     
            return $this;
        }
     
        /**
         * Get prixTotalHt
         *
         * @return float 
         */
        public function getPrixTotalHt()
        {
            return $this->prixTotalHt;
        }
     
        /**
         * Set prixTotalTtc
         *
         * @param float $prixTotalTtc
         * @return DocumentProduit
         */
        public function setPrixTotalTtc($prixTotalTtc)
        {
            $this->prixTotalTtc = $prixTotalTtc;
     
            return $this;
        }
     
        /**
         * Get prixTotalTtc
         *
         * @return float 
         */
        public function getPrixTotalTtc()
        {
            return $this->prixTotalTtc;
        }
     
        /**
         * Set idDocument
         *
         * @param \Sdz\BlogBundle\Entity\Documents $idDocument
         * @return DocumentProduit
         */
        public function setIdDocument(\Sdz\BlogBundle\Entity\Documents $idDocument = null)
        {
            $this->idDocument = $idDocument;
     
            return $this;
        }
     
        /**
         * Get idDocument
         *
         * @return \Sdz\BlogBundle\Entity\Documents 
         */
        public function getIdDocument()
        {
            return $this->idDocument;
        }
     
        /**
         * Set produitId
         *
         * @param \Sdz\BlogBundle\Entity\Produit $idProduit
         * @return DocumentProduit
         */
        public function setProduitId(\Sdz\BlogBundle\Entity\Produit $idProduit = null)
        {
            $this->produitId = $idProduit;
     
            return $this;
        }
     
        /**
         * Get produitId
         *
         * @return \Sdz\BlogBundle\Entity\Produit 
         */
        public function getProduitId()
        {
            return $this->$produitId;
        }
    }
    Et Produit :
    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
    <?php
     
    namespace Sdz\BlogBundle\Entity;
     
    use Doctrine\ORM\Mapping as ORM;
     
    /**
     * Produit
     *
     * @ORM\Table(name="produit")
     * @ORM\Entity
     */
    class Produit
    {
        /**
         * @var integer
         *
         * @ORM\Column(name="id_produit", type="integer", nullable=false)
         * @ORM\Id
         * @ORM\GeneratedValue(strategy="IDENTITY")
         */
        private $idProduit;
     
        /**
         * @var string
         *
         * @ORM\Column(name="name_product", type="string", length=100, nullable=false)
         */
        private $nameProduct;
     
        /**
         * @var float
         *
         * @ORM\Column(name="price_product_ht", type="float", nullable=false)
         */
        private $priceProductHt;
     
        /**
         * @var \Tva
         *
         * @ORM\ManyToOne(targetEntity="Tva")
         * @ORM\JoinColumns({
         *   @ORM\JoinColumn(name="id_tva", referencedColumnName="id_tva")
         * })
         */
        private $idTva;
     
     
     
        /**
         * Get idProduit
         *
         * @return integer 
         */
        public function getIdProduit()
        {
            return $this->idProduit;
        }
     
        /**
         * Set nameProduct
         *
         * @param string $nameProduct
         * @return Produit
         */
        public function setNameProduct($nameProduct)
        {
            $this->nameProduct = $nameProduct;
     
            return $this;
        }
     
        /**
         * Get nameProduct
         *
         * @return string 
         */
        public function getNameProduct()
        {
            return $this->nameProduct;
        }
     
        /**
         * Set priceProductHt
         *
         * @param float $priceProductHt
         * @return Produit
         */
        public function setPriceProductHt($priceProductHt)
        {
            $this->priceProductHt = $priceProductHt;
     
            return $this;
        }
     
        /**
         * Get priceProductHt
         *
         * @return float 
         */
        public function getPriceProductHt()
        {
            return $this->priceProductHt;
        }
     
        /**
         * Set idTva
         *
         * @param \Sdz\BlogBundle\Entity\Tva $idTva
         * @return Produit
         */
        public function setIdTva(\Sdz\BlogBundle\Entity\Tva $idTva = null)
        {
            $this->idTva = $idTva;
     
            return $this;
        }
     
        /**
         * Get idTva
         *
         * @return \Sdz\BlogBundle\Entity\Tva 
         */
        public function getIdTva()
        {
            return $this->idTva;
        }
    }
    Mes classe formulaires utilisé :
    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
    <?php
     
    namespace Sdz\BlogBundle\Form;
     
    use Symfony\Component\Form\AbstractType;
    use Symfony\Component\Form\FormBuilderInterface;
    use Symfony\Component\OptionsResolver\OptionsResolverInterface;
     
    class DocumentsType extends AbstractType
    {
        public function buildForm(FormBuilderInterface $builder, array $options)
        {
            $builder
                ->add('date','date')
                ->add('prix','integer')
                ->add('idRefFacture','entity',array(
                    'class'=>'SdzBlogBundle:Documents',
                    'property'=>'idDocument',
                    'label'=>'document ref'
                ))
                ->add('idTypeDocument','entity',array(
                    'class' => 'SdzBlogBundle:TypeDocument',
                    'property' => 'nameDocument',
                    'label'=>'Type de document '
                ))
                ->add('idSociete','entity',array(
                    'class'=>'SdzBlogBundle:SocieteClient',
                    'property'=>'nameSociete',
                    'label'=>'Societe'
                ))
                ->add('idUser','entity',array(
                    'class'=>'SdzBlogBundle:User',
                    'property'=>'lastname',
                    'label'=>'Utilisateur'
                ))
    			->add('products','collection', array(
    					'type'=> new ProduitType(),
    					'allow_add' => true,
    					'by_reference' => false,
    			));
     
     
            ;
        }
     
        public function setDefaultOptions(OptionsResolverInterface $resolver)
        {
            $resolver->setDefaults(array(
                'data_class' => 'Sdz\BlogBundle\Entity\Documents'
            ));
        }
     
        public function getName()
        {
            return 'sdz_blogbundle_documentstype';
        }
    }
    ProduitType :
    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
    <?php
     
    namespace Sdz\BlogBundle\Form;
     
    use Symfony\Component\Form\AbstractType;
    use Symfony\Component\Form\FormBuilderInterface;
    use Symfony\Component\OptionsResolver\OptionsResolverInterface;
     
    class ProduitType extends AbstractType
    {
        public function buildForm(FormBuilderInterface $builder, array $options)
        {
            $builder
                ->add('nameProduct')
                ->add('priceProductHt')
                ->add('idTva','entity',array('class'=>'Sdz\BlogBundle\Entity\Tva', 'property'=>'idTva'))
            ;
        }
     
        public function setDefaultOptions(OptionsResolverInterface $resolver)
        {
            $resolver->setDefaults(array(
                'data_class' => 'Sdz\BlogBundle\Entity\Produit'
            ));
        }
     
        public function getName()
        {
            return 'sdz_blogbundle_produittype';
        }
    }
    Et enfin mon action edition :
    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
    public function editAction($id)
        {
     
            $em = $this->getDoctrine()->getManager();
            $doc = $em->getRepository('SdzBlogBundle:Documents')->find($id);
     
     
            if (!$doc) {
                throw $this->createNotFoundException('Aucune tâche trouvée pour cet id : '.$id);
            }
     
     
            $editForm = $this->createForm(new DocumentsType(), $doc);
            var_dump($editForm);die;
            $request = $this->get('request');
            if ($request->getMethod()=='POST') {
                $editForm->bind($this->getRequest());
                if ($editForm->isValid()) {
                    //$em->persist($prod);
     
                    $em->persist($doc);
                    $em->flush();
                    return $this->redirect( $this->generateUrl('sdzblog_accueil')
                    );
                }
     
            }
     
    		$lname_user=$this->get('security.context')->getToken()->getUser()->getFirstName();
    		$fname=$this->get('security.context')->getToken()->getUser()->getLastName();
     
            return $this->render('SdzBlogBundle:Blog:ajouter.html.twig',array(
                'form' => $editForm->createView(),'edit'=>1,'id'=>$id,'nom'=>$lname_user,
    			'prenom'=>$fname,
            ));
     
            // affiche un template de formulaire quelconque
        }
    L'erreur que je rencontre :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    Notice: Undefined index: documents in /var/www/hera.croissance-net.com/htdocs/vendor/doctrine/orm/lib/Doctrine/ORM/Persisters/BasicEntityPersister.php line 965
    J'ai également une autre erreur dans le profiler :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    The association Sdz\BlogBundle\Entity\Documents#products refers to the owning side field Sdz\BlogBundle\Entity\Produit#documents which does not exist.
    J'ai mis beaucoup de code pour essayer de vous donner le maximum d'information.

    Je vous remercie d'avance,
    RYL

  5. #5
    Membre habitué
    Homme Profil pro
    Développeur Web
    Inscrit en
    Octobre 2009
    Messages
    126
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Activité : Développeur Web

    Informations forums :
    Inscription : Octobre 2009
    Messages : 126
    Points : 183
    Points
    183
    Par défaut Doctrine2 et mapping
    Salut !

    Fais un tour sur la doc de doctrine 2

    https://doctrine-orm.readthedocs.org...n-mapping.html

    Tu te casses la tête avec les id des entités. Le mapping se fait via les meta données (ici tes annotations)
    cf 6.1 6.1. Many-To-One, Unidirectional
    The above @JoinColumn is optional as it would default to address_id and id anyways. You can omit it and let it use the defaults.

Discussions similaires

  1. valid formulaire + params
    Par philippe123 dans le forum Général JavaScript
    Réponses: 9
    Dernier message: 01/09/2005, 15h29
  2. Validation formulaire dynamique
    Par odelayen dans le forum Général JavaScript
    Réponses: 2
    Dernier message: 31/08/2005, 17h47
  3. test validation formulaire
    Par Nkubi dans le forum Général JavaScript
    Réponses: 3
    Dernier message: 28/06/2005, 11h22
  4. Valider Formulaire
    Par Gourouni dans le forum ASP
    Réponses: 18
    Dernier message: 06/01/2005, 15h34
  5. Pb validation formulaire
    Par cosminutza dans le forum Général JavaScript
    Réponses: 3
    Dernier message: 03/12/2004, 10h35

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