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

Doctrine2 PHP Discussion :

Problème relation ManyToMany et inverse side


Sujet :

Doctrine2 PHP

Vue hybride

Message précédent Message précédent   Message suivant Message suivant
  1. #1
    Membre confirmé
    Homme Profil pro
    Inscrit en
    Novembre 2006
    Messages
    155
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 48
    Localisation : France

    Informations forums :
    Inscription : Novembre 2006
    Messages : 155
    Par défaut Problème relation ManyToMany et inverse side
    Bonjour,

    J'ai une Entity CodeRome qui a un lien manytomany avec une entity SousDomaine.

    J'essaie de faire un formulaire afin de ne modifier que ce champ de l'entity CodeRome. Tout semble bien se passer: à la validation du formulaire, la redirection a bien lieu. Mais les associations ne sont pas créées en base, et au final, le champ sousdomaines de CodeRome n'a pas été mis à jour.

    Comment doit-on procéder pour réaliser ce genre de tâches ?

    Merci d'avance

    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
     
        // CodeRomeController.php
        /**
         * @Route("/rome/{slug}/domaine/add", name="_rome_add_domaine")
         * @Template
         */
        public function addDomaineAction(Request $request, $slug)
        {
            $codeRome = $this->getCodeRome($slug);
            $form = $this->createForm( new RomeSsDomaineType(), $codeRome);
     
            if ( $request->getMethod() == 'POST' )
            {
                $form->bindRequest($request);
                if ( $form->isValid() )
                {                
                    $em = $this->getDoctrine()->getEntityManager();
                    $em->persist($codeRome);
                    $em->flush();
     
                    $this->get('session')->setFlash('success', 'Un sous-domaine a été ajouté au code ROME '.$codeRome->getCodeV3());
                    return $this->redirect( $this->generateUrl('_rome_show', array('slug'=>$slug)) );
                }
            }
     
            return array('form'=>$form->createView(), 'codeRome'=>$codeRome);
        }
     
     
        /**
         * Find a CodeRome by its slug
         * @param string $slug
         * @return \AcDijon\GretabotBundle\Entity\CodeRome 
         */
        protected function getCodeRome($slug)
        {        
            $codeRome = $this->getDoctrine()
                             ->getRepository('AcDijonGretabotBundle:CodeRome')
                             ->findOneBySlug($slug);
            if ( !$codeRome ) throw $this->createNotFoundException('CodeRome not found');
     
            return $codeRome;
        }
    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
    // RomeSsDomaineType.php
    class RomeSsDomaineType extends AbstractType
    {
        public function buildForm( FormBuilder $builder, array $options )
        {
            $builder->add('sousdomaines');
        }
     
        public function getName()
        {
            return 'romessdomaine';
        }
     
        public function getDefaultOptions(array $options)
        {
            return array(
                'data_class'    =>  'AcDijon\GretabotBundle\Entity\CodeRome'
            );
        }
    }
    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
    // CodeRome.php
    /**
     * @ORM\Entity(repositoryClass="AcDijon\GretabotBundle\Repository\CodeRomeRepository")
     * @ORM\Table(name="gbot_CodeRome")
     * @Gedmo\SoftDeleteable(fieldName="deletedAt")
     */
    class CodeRome {
     
        /**
         * @ORM\Id
         * @ORM\Column(type="integer")
         * @ORM\GeneratedValue
         */
        protected $id;
     
        /**
         *
         * @var string codeV2
         * @ORM\Column(length=5)
         * @Assert\NotBlank(message="Veuillez indiquer le code V2")
         */
        protected $codeV2;
     
        /**
         *
         * @var string nomV2
         * @ORM\Column(length=255)
         * @Assert\NotBlank(message="Veuillez renseigner le nom V2")
         */
        protected $nomV2;
     
        /**
         *
         * @var string appellationV2
         * @ORM\Column(length=255)
         * @Assert\NotBlank(message="Veuillez renseigner l'appellation V2")
         */
        protected $appellationV2;
     
        /**
         *
         * @var string codeV3
         * @ORM\Column(length=5)
         * @Assert\NotBlank(message="Veuillez indiquer le code V3")
         */
        protected $codeV3;
     
        /**
         *
         * @var string nomV3
         * @ORM\Column(length=255)
         * @Assert\NotBlank(message="Veuillez renseigner le nom V3")
         */
        protected $nomV3;
     
        /**
         *
         * @var string appellationV3
         * @ORM\Column(length=255)
         * @Assert\NotBlank(message="Veuillez renseigner l'appellation V3")
         */
        protected $appellationV3;
     
        /**
         *
         * @var ArrayCollection offres
         * @ORM\ManyToMany(targetEntity="Offre", mappedBy="codesRome")
         */
        protected $offres;
     
        /**
         *
         * @var ArrayCollection codesNsf
         * @ORM\ManyToMany(targetEntity="CodeNsf", inversedBy="codesRome")
         * @ORM\JoinTable(name="gbot_codesRome_codesNsf")
         */
        protected $codesNsf;
     
        /**
         *
         * @var ArrayCollection formacodes
         * @ORM\ManyToMany(targetEntity="Formacode", inversedBy="codesRome")
         * @ORM\JoinTable(name="gbot_codesRome_formacodes")
         */
        protected $formacodes;
     
        /**
         *
         * @var ArrayCollection metiers
         * @ORM\OneToMany(targetEntity="Metier", mappedBy="codeRome")
         */
        protected $metiers;
     
        /**
         * @var ArrayCollection sousdomaines
         * @ORM\ManyToMany(targetEntity="SousDomaine", mappedBy="codesRome")
         */
        protected $sousdomaines;
     
     
        /**
         * @Gedmo\Slug(fields={"nomV3"})
         * @ORM\Column(length=128, unique=true)
         */
        protected $slug;
     
        /**
         * @Gedmo\Timestampable(on="create")
         * @ORM\Column(type="datetime")
         */
        protected $created;
     
        /**
         * @Gedmo\Timestampable(on="update")
         * @ORM\Column(type="datetime")
         */
        protected $updated;
     
        /**
         * @ORM\Column(name="deleted_at", type="datetime", nullable=true)
         */
        protected $deletedAt;
     
        public function __construct()
        {
            $this->offres = new \Doctrine\Common\Collections\ArrayCollection();
            $this->codesNsf = new \Doctrine\Common\Collections\ArrayCollection();
            $this->formacodes = new \Doctrine\Common\Collections\ArrayCollection();
            $this->metiers = new \Doctrine\Common\Collections\ArrayCollection();
            $this->sousdomaines = new \Doctrine\Common\Collections\ArrayCollection();
        }
     
        public function __toString()
        {
            return $this->getCodeV3().' : '.$this->getAppellationV3();
        }
     
        /**
         * Get id
         *
         * @return integer 
         */
        public function getId()
        {
            return $this->id;
        }
     
        /**
         * Set codeV2
         *
         * @param string $codeV2
         */
        public function setCodeV2($codeV2)
        {
            $this->codeV2 = $codeV2;
        }
     
        /**
         * Get codeV2
         *
         * @return string 
         */
        public function getCodeV2()
        {
            return $this->codeV2;
        }
     
        /**
         * Set nomV2
         *
         * @param string $nomV2
         */
        public function setNomV2($nomV2)
        {
            $this->nomV2 = $nomV2;
        }
     
        /**
         * Get nomV2
         *
         * @return string 
         */
        public function getNomV2()
        {
            return $this->nomV2;
        }
     
        /**
         * Set appellationV2
         *
         * @param string $appellationV2
         */
        public function setAppellationV2($appellationV2)
        {
            $this->appellationV2 = $appellationV2;
        }
     
        /**
         * Get appellationV2
         *
         * @return string 
         */
        public function getAppellationV2()
        {
            return $this->appellationV2;
        }
     
        /**
         * Set codeV3
         *
         * @param string $codeV3
         */
        public function setCodeV3($codeV3)
        {
            $this->codeV3 = $codeV3;
        }
     
        /**
         * Get codeV3
         *
         * @return string 
         */
        public function getCodeV3()
        {
            return $this->codeV3;
        }
     
        /**
         * Set nomV3
         *
         * @param string $nomV3
         */
        public function setNomV3($nomV3)
        {
            $this->nomV3 = $nomV3;
        }
     
        /**
         * Get nomV3
         *
         * @return string 
         */
        public function getNomV3()
        {
            return $this->nomV3;
        }
     
        /**
         * Set appellationV3
         *
         * @param string $appellationV3
         */
        public function setAppellationV3($appellationV3)
        {
            $this->appellationV3 = $appellationV3;
        }
     
        /**
         * Get appellationV3
         *
         * @return string 
         */
        public function getAppellationV3()
        {
            return $this->appellationV3;
        }
     
        /**
         * Set slug
         *
         * @param string $slug
         */
        public function setSlug($slug)
        {
            $this->slug = $slug;
        }
     
        /**
         * Get slug
         *
         * @return string 
         */
        public function getSlug()
        {
            return $this->slug;
        }
     
        /**
         * Set created
         *
         * @param datetime $created
         */
        public function setCreated($created)
        {
            $this->created = $created;
        }
     
        /**
         * Get created
         *
         * @return datetime 
         */
        public function getCreated()
        {
            return $this->created;
        }
     
        /**
         * Set updated
         *
         * @param datetime $updated
         */
        public function setUpdated($updated)
        {
            $this->updated = $updated;
        }
     
        /**
         * Get updated
         *
         * @return datetime 
         */
        public function getUpdated()
        {
            return $this->updated;
        }
     
        /**
         * Set deletedAt
         *
         * @param datetime $deletedAt
         */
        public function setDeletedAt($deletedAt)
        {
            $this->deletedAt = $deletedAt;
        }
     
        /**
         * Get deletedAt
         *
         * @return datetime 
         */
        public function getDeletedAt()
        {
            return $this->deletedAt;
        }
     
        /**
         * Add offres
         *
         * @param AcDijon\GretabotBundle\Entity\Offre $offres
         */
        public function addOffre(\AcDijon\GretabotBundle\Entity\Offre $offres)
        {
            $this->offres[] = $offres;
        }
     
        /**
         * Get offres
         *
         * @return Doctrine\Common\Collections\Collection 
         */
        public function getOffres()
        {
            return $this->offres;
        }
     
        /**
         * Add codesNsf
         *
         * @param AcDijon\GretabotBundle\Entity\CodeNsf $codesNsf
         */
        public function addCodeNsf(\AcDijon\GretabotBundle\Entity\CodeNsf $codesNsf)
        {
            $this->codesNsf[] = $codesNsf;
        }
     
        /**
         * Get codesNsf
         *
         * @return Doctrine\Common\Collections\Collection 
         */
        public function getCodesNsf()
        {
            return $this->codesNsf;
        }
     
        /**
         * Add formacodes
         *
         * @param AcDijon\GretabotBundle\Entity\Formacode $formacodes
         */
        public function addFormacode(\AcDijon\GretabotBundle\Entity\Formacode $formacodes)
        {
            $this->formacodes[] = $formacodes;
        }
     
        /**
         * Get formacodes
         *
         * @return Doctrine\Common\Collections\Collection 
         */
        public function getFormacodes()
        {
            return $this->formacodes;
        }
     
        /**
         * Add metiers
         *
         * @param AcDijon\GretabotBundle\Entity\Metier $metiers
         */
        public function addMetier(\AcDijon\GretabotBundle\Entity\Metier $metiers)
        {
            $this->metiers[] = $metiers;
        }
     
        /**
         * Get metiers
         *
         * @return Doctrine\Common\Collections\Collection 
         */
        public function getMetiers()
        {
            return $this->metiers;
        }
     
        /**
         * Add sousdomaines
         *
         * @param AcDijon\GretabotBundle\Entity\SousDomaine $sousdomaines
         */
        public function addSousDomaine(\AcDijon\GretabotBundle\Entity\SousDomaine $sousdomaines)
        {
            $this->sousdomaines[] = $sousdomaines;
        }
     
        /**
         * Get sousdomaines
         *
         * @return Doctrine\Common\Collections\Collection 
         */
        public function getSousdomaines()
        {
            return $this->sousdomaines;
        }
    }
    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
    /**
     * @ORM\Entity(repositoryClass="AcDijon\GretabotBundle\Repository\SousDomaineRepository")
     * @ORM\Table(name="gbot_SousDomaine")
     * @Gedmo\SoftDeleteable(fieldName="deletedAt")
     */
    class SousDomaine 
    {
        /**
         * @ORM\Id
         * @ORM\Column(type="integer")
         * @ORM\GeneratedValue
         */
        protected $id;
     
        /**
         * @ORM\Column(type="string", length=255, nullable=false)
         * @Assert\NotBlank(message="L'intitulé doit être renseigné")
         */
        protected $nom;  
     
        /**
         *
         * @var integer numero
         * @ORM\Column(type="integer", nullable=false)
         * @Assert\NotBlank(message="Veuillez donner un numéro à ce sous-domaine")
         * @Assert\Type(type="integer", message="La valeur {{ value }} n'est pas un {{ type }} valide")
         */
        protected $numero;
     
        /**
         * @Gedmo\Slug(fields={"nom"})
         * @ORM\Column(length=128, unique=true)
         */
        protected $slug;
     
        /**
         * @Gedmo\Timestampable(on="create")
         * @ORM\Column(type="datetime")
         */
        protected $created;
     
        /**
         * @Gedmo\Timestampable(on="update")
         * @ORM\Column(type="datetime")
         */
        protected $updated;
     
        /**
         * @ORM\Column(name="deleted_at", type="datetime", nullable=true)
         */
        protected $deletedAt;
     
        /**
         * @ORM\ManyToOne(targetEntity="Domaine", inversedBy="sousDomaines")
         * @ORM\JoinColumn(name="domaine_id", referencedColumnName="id")
         */
        protected $domaine;   
     
        /**
         * @var ArrayCollection formacodes
         * @ORM\ManyToMany(targetEntity="Formacode")
         * @ORM\JoinTable(name="gbot_sousDomaines_formacodes",
         *                joinColumns={@ORM\JoinColumn(name="sousdomaine_id", referencedColumnName="id")},
         *                inverseJoinColumns={@ORM\JoinColumn(name="formacode_id", referencedColumnName="id")}
         * )
         * @Assert\NotBlank(message="Sélectionnez un ou plusieurs Formacodes")
         */
        protected $formacodes;
     
        /**
         *
         * @var ArrayCollection codesRome
         * @ORM\ManyToMany(targetEntity="CodeRome", inversedBy="sousdomaines")
         * @ORM\JoinTable(name="gbot_sousDomaines_codesRomes")
         * @Assert\NotBlank(message="Sélectionnez un ou plusieurs code ROME")
         */
        protected $codesRome;
     
        /**
         *
         * @var ArrayCollection codesNsf
         * @ORM\ManyToMany(targetEntity="CodeNsf")
         * @ORM\JoinTable(name="gbot_sousDomaines_codesNsf",
         *                joinColumns={@ORM\JoinColumn(name="sousdomaine_id", referencedColumnName="id")},
         *                inverseJoinColumns={@ORM\JoinColumn(name="codensf_id", referencedColumnName="id")}
         * )
         * @Assert\NotBlank(message="Sélectionner un ou plusieurs code NSF")
         */
        protected $codesNsf;
     
     
        /**
         * Get id
         *
         * @return integer 
         */
        public function getId()
        {
            return $this->id;
        }
     
        /**
         * Set domaine
         *
         * @param AcDijon\GretabotBundle\Entity\Domaine $domaine
         */
        public function setDomaine(\AcDijon\GretabotBundle\Entity\Domaine $domaine)
        {
            $this->domaine = $domaine;
        }
     
        /**
         * Get domaine
         *
         * @return AcDijon\GretabotBundle\Entity\Domaine 
         */
        public function getDomaine()
        {
            return $this->domaine;
        }
     
        public function __construct()
        {
            $this->formacodes = new \Doctrine\Common\Collections\ArrayCollection();
            $this->codesRome = new \Doctrine\Common\Collections\ArrayCollection();
            $this->codesNsf = new \Doctrine\Common\Collections\ArrayCollection();
        }
     
        public function __toString() {
            return $this->getNom();
        }
     
        /**
         * Add formacodes
         *
         * @param AcDijon\GretabotBundle\Entity\Formacode $formacodes
         */
        public function addFormacode(\AcDijon\GretabotBundle\Entity\Formacode $formacodes)
        {
            $this->formacodes[] = $formacodes;
        }
     
        /**
         * Get formacodes
         *
         * @return Doctrine\Common\Collections\Collection 
         */
        public function getFormacodes()
        {
            return $this->formacodes;
        }
     
        /**
         * Set slug
         *
         * @param string $slug
         */
        public function setSlug($slug)
        {
            $this->slug = $slug;
        }
     
        /**
         * Get slug
         *
         * @return string 
         */
        public function getSlug()
        {
            return $this->slug;
        }
     
        /**
         * Set created
         *
         * @param datetime $created
         */
        public function setCreated($created)
        {
            $this->created = $created;
        }
     
        /**
         * Get created
         *
         * @return datetime 
         */
        public function getCreated()
        {
            return $this->created;
        }
     
        /**
         * Set updated
         *
         * @param datetime $updated
         */
        public function setUpdated($updated)
        {
            $this->updated = $updated;
        }
     
        /**
         * Get updated
         *
         * @return datetime 
         */
        public function getUpdated()
        {
            return $this->updated;
        }
     
        /**
         * Set deletedAt
         *
         * @param datetime $deletedAt
         */
        public function setDeletedAt($deletedAt)
        {
            $this->deletedAt = $deletedAt;
        }
     
        /**
         * Get deletedAt
         *
         * @return datetime 
         */
        public function getDeletedAt()
        {
            return $this->deletedAt;
        }
     
        /**
         * Set nom
         *
         * @param string $nom
         */
        public function setNom($nom)
        {
            $this->nom = $nom;
        }
     
        /**
         * Get nom
         *
         * @return string 
         */
        public function getNom()
        {
            return $this->nom;
        }
     
        /**
         * Add codesRome
         *
         * @param AcDijon\GretabotBundle\Entity\CodeRome $codesRome
         */
        public function addCodeRome(\AcDijon\GretabotBundle\Entity\CodeRome $codesRome)
        {
            $this->codesRome[] = $codesRome;
        }
     
        /**
         * Get codesRome
         *
         * @return Doctrine\Common\Collections\Collection 
         */
        public function getCodesRome()
        {
            return $this->codesRome;
        }
     
        /**
         * Add codesNsf
         *
         * @param AcDijon\GretabotBundle\Entity\CodeNsf $codesNsf
         */
        public function addCodeNsf(\AcDijon\GretabotBundle\Entity\CodeNsf $codesNsf)
        {
            $this->codesNsf[] = $codesNsf;
        }
     
        /**
         * Get codesNsf
         *
         * @return Doctrine\Common\Collections\Collection 
         */
        public function getCodesNsf()
        {
            return $this->codesNsf;
        }
     
        /**
         * Set numero
         *
         * @param integer $numero
         */
        public function setNumero($numero)
        {
            $this->numero = $numero;
        }
     
        /**
         * Get numero
         *
         * @return integer 
         */
        public function getNumero()
        {
            return $this->numero;
        }
    }

  2. #2
    Membre confirmé
    Homme Profil pro
    Inscrit en
    Novembre 2006
    Messages
    155
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 48
    Localisation : France

    Informations forums :
    Inscription : Novembre 2006
    Messages : 155
    Par défaut
    Je me demande si ça n'aurait pas à voir avec les histoires de owning side et de inverse side.

    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
    /**
         *
         * @var ArrayCollection codesNsf
         * @ORM\ManyToMany(targetEntity="CodeNsf", inversedBy="codesRome")
         * @ORM\JoinTable(name="gbot_codesRome_codesNsf")
         */
        protected $codesNsf;
     
        /**
         *
         * @var ArrayCollection formacodes
         * @ORM\ManyToMany(targetEntity="Formacode", inversedBy="codesRome")
         * @ORM\JoinTable(name="gbot_codesRome_formacodes")
         */
        protected $formacodes;
     
        /**
         *
         * @var ArrayCollection metiers
         * @ORM\OneToMany(targetEntity="Metier", mappedBy="codeRome")
         */
        protected $metiers;
     
        /**
         * @var ArrayCollection sousdomaines
         * @ORM\ManyToMany(targetEntity="SousDomaine", mappedBy="codesRome")
         */
        protected $sousdomaines;
    J'ai ce problème avec les relations à SousDomaine et à Metier (dans ce cas CodeRome est inverse side), mais pas avec les relations à CodeNsf et Formacode (où CodeRome est owning side).

    Je n'y comprends plus rien !

  3. #3
    Membre confirmé
    Homme Profil pro
    Inscrit en
    Novembre 2006
    Messages
    155
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 48
    Localisation : France

    Informations forums :
    Inscription : Novembre 2006
    Messages : 155
    Par défaut
    Vu le nombre de réponses que j'ai eu sur ce post, je me demande si j'ai été bien clair.

    Enfin bref, après quelques heures de prise de tête j'ai finis par trouvé la solution : Doctrine ne prenant pas en compte les changements sur le inverse side, il faut récupérer les owning sides et boucler dessus pour y faire les modifs.

    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
    public function editSousDomaineAction(Request $request, $slug)
        {
            $codeRome = $this->findCodeRome($slug);
            $currentSousdomaines = clone $codeRome->getSousdomaines();
     
            $form = $this->createForm( new RomeSousDomaineType(), $codeRome);
     
            if ($request->getMethod() == 'POST')
            {
                $form->bindRequest($request);
                if ( $form->isValid() )
                {
                    $em = $this->getDoctrine()->getEntityManager();
     
                    foreach ( $currentSousdomaines as $ssdom )
                    {
                        $ssdom->getCodesRome()->removeElement( $codeRome );
                        $em->persist($ssdom);
                    }
     
                    $romesousdomaine = $request->get('romesousdomaine');
                    $selectedKeys = $romesousdomaine['sousdomaines'];
     
                    $qb = $this->getDoctrine()->getRepository('AcDijonGretabotBundle:SousDomaine')
                                  ->createQueryBuilder('q');
                    $qb->where($qb->expr()->in('q.id', $selectedKeys));
                    $query = $qb->getQuery();
     
                    foreach ( $query->getResult() as $ssdom )
                    {
                        $ssdom->addCodeRome($codeRome);
                        $em->persist($ssdom);
                    }
     
                    $em->flush();
     
                    $this->get('session')->setFlash('success','Code ROME mis à jour');
                    return $this->redirect( $this->generateUrl('_rome_show', array('slug'=>$codeRome->getSlug())));
                }
            }
     
            return array('form'=>$form->createView(), 'codeRome'=>$codeRome);
        }

  4. #4
    Membre confirmé
    Homme Profil pro
    Inscrit en
    Novembre 2006
    Messages
    155
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 48
    Localisation : France

    Informations forums :
    Inscription : Novembre 2006
    Messages : 155
    Par défaut
    Ceci dit, avant d'en arriver là, j'étais passé par là :

    (extrait)

    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
    $codeRome = $this->findCodeRome($slug);
            $currentSousdomaines = clone $codeRome->getSousdomaines();
            
            $form = $this->createForm( new RomeSousDomaineType(), $codeRome);
            
            if ($request->getMethod() == 'POST')
            {
                $form->bindRequest($request);
                if ( $form->isValid() )
                {
                    $em = $this->getDoctrine()->getEntityManager();
                    $sousdomaines = $codeRome->getSousdomaines();
                    echo 'count : '.count($sousdomaines);
                    
                    foreach ( $currentSousdomaines as $ssdom )
                    {
    //                    echo 'current : '.$ssdom->getNom().', ';
    //                    $ssdom->getCodesRome()->removeElement( $codeRome );
    //                    $em->persist($ssdom);
                    }
                    
                    echo '<br/>count : '.count($sousdomaines).'<br/>';
    Ce qui m'interpelle, c'est que le premier echo (en rouge) me retourne par exemple 3 mais le deuxième me retourne 0. Si je commente tout le bloc foreach (foreach compris), j'ai bien 3 la première fois, et 3 la deuxième fois. Donc dans l'exemple ci-dessus, je ne comprends pas pourquoi après le foreach qui ne fait rien (les lignes à l'intérieur sont commentées), ma collection $sousdomaines se retrouve vidée ??!!

Discussions similaires

  1. [2.x] Probléme Relation ManyToMany
    Par jujurochedu42 dans le forum Symfony
    Réponses: 1
    Dernier message: 14/05/2013, 11h53
  2. Problème relation @ManyToMany
    Par pims42 dans le forum Développement Web en Java
    Réponses: 4
    Dernier message: 26/03/2013, 10h27
  3. Problème relation ManyTOMany
    Par Adraesh dans le forum Doctrine2
    Réponses: 2
    Dernier message: 01/03/2012, 14h43
  4. [EJB3] Problème de cascade en relation Manytomany
    Par mommsse dans le forum Java EE
    Réponses: 3
    Dernier message: 22/12/2010, 07h57
  5. Problème du count dans une relation ManyToMany
    Par Invité dans le forum Général Java
    Réponses: 0
    Dernier message: 10/05/2010, 12h58

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