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 :

OneToOne : affichage dans une boucle


Sujet :

Doctrine2 PHP

  1. #1
    Membre à l'essai
    Profil pro
    Inscrit en
    Octobre 2008
    Messages
    22
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Octobre 2008
    Messages : 22
    Points : 13
    Points
    13
    Par défaut OneToOne : affichage dans une boucle
    Bonjour,

    Ayant commencé il ya peu le développement sous symfony2 et doctrine2, je n'arrive pas à comprendre comment afficher une colonne de la table qui est lié à ma table principale. Je m'explique, j'ai deux tables, une table "users" et une table "pays" lié par "pays_id". Quand j'affiche un enregistrement unique (ex : users.id = 2), alors il m'est possible de récupérer le nom du pays comme ceci : $entity->getPaysId()->getPays().

    Malheureusement, ceci ne fonctionne pas quand je suis dans une boucle foreach. Une erreur "entity not found m'est retournée".

    Voici les différents codes des fichiers utilisés pour ceci :

    Les fichiers Users :
    Users.orm.yml
    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
     
    Ito\DefaultBundle\Entity\Users:
      type: entity
      table: users
      repositoryClass: Ito\DefaultBundle\Repository\UsersRepository
      manyToMany:
        userRoles:
          targetEntity: Roles
          joinTable:
            name: users_roles
            joinColumns:
              user_id:
                referencedColumnName: id
            inverseJoinColumns:
              role_id:
                referencedColumnName: id
      oneToOne:
        pays_id:
          targetEntity: Pays
          joinColumn:
            name: pays_id
            referencedColumnName: id
      fields:
        id:
          type: integer
          id: true
          generator:
            strategy: AUTO
        username:
          type: string
          length: 255
        password:
          type: string
          length: 255
        nom:
          type: string
          length: 255
        prenom:
          type: string
          length: 255
        entreprise:
          type: string
          length: 255
        adresse1:
          type: string
          length: 255
        adresse2:
          type: string
          length: 255
        cp:
          type: string
          length: 255
        ville:
          type: string
          length: 255
     
    #    pays_id:
    #      type: integer
        numero_tva_intracommunautaire:
          type: string
          length: 255
      lifecycleCallbacks: {  }
    Pays.orm.yml
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
     
    Ito\DefaultBundle\Entity\Pays:
      type: entity
      table: pays
      fields:
        id:
          type: integer
          id: true
          generator:
            strategy: AUTO
        pays:
          type: string
          length: 255
      lifecycleCallbacks: {  }
    Users.php
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
    239
    240
    241
    242
    243
    244
    245
    246
    247
    248
    249
    250
    251
    252
    253
    254
    255
    256
    257
    258
    259
    260
    261
    262
    263
    264
    265
    266
    267
    268
    269
    270
    271
    272
    273
    274
    275
    276
    277
    278
    279
    280
    281
    282
    283
    284
    285
    286
    287
    288
    289
    290
    291
    292
    293
    294
    295
    296
    297
    298
    299
    300
    301
    302
    303
    304
    305
    306
    307
    308
    309
    310
    311
    312
    313
    314
    315
    316
    317
    318
    319
    320
    321
    322
    323
    324
    325
    326
    327
    328
    329
    330
    331
    332
    333
    334
    335
    336
    337
    338
    339
    340
    341
    342
    343
    344
    345
    346
    347
    348
    349
    350
    351
    352
    353
    354
    355
    356
    357
    358
    359
    360
    361
    362
    363
    364
    365
    366
    367
    368
    369
    370
    371
    372
    373
    374
    375
    376
    377
    378
    379
    380
    381
    382
    383
    384
    385
    386
    387
    388
    389
    390
    391
    392
    393
    394
    395
    396
    397
    398
    399
    400
    401
    402
    403
    404
    405
    406
    407
    408
    409
    410
    <?php
     
    namespace Ito\DefaultBundle\Entity;
     
    use Doctrine\ORM\Mapping as ORM;
    use \Symfony\Component\Security\Core\User\UserInterface;
     
    use Doctrine\Common\Collections\ArrayCollection;
     
    /**
     * Ito\DefaultBundle\Entity\Users
     */
    class Users implements UserInterface, \Serializable
    {
        /**
         * @var integer $id
         */
        private $id;
     
        /**
         * @var string $username
         */
        private $username;
     
        /**
         * @var string $password
         */
        private $password;
     
        /**
         * @var string $nom
         */
        private $nom;
     
        /**
         * @var string $prenom
         */
        private $prenom;
     
        /**
         * @var string $entreprise
         */
        private $entreprise;
     
        /**
         * @var string $adresse1
         */
        private $adresse1;
     
        /**
         * @var string $adresse2
         */
        private $adresse2;
     
        /**
         * @var string $cp
         */
        private $cp;
     
        /**
         * @var string $ville
         */
        private $ville;
     
        /**
         * @var integer $pays_id
         */
        private $pays_id;
     
        private $pays;
     
        public function getPays() {
            return $this->getPaysId();
        }
     
        /**
         * @var string $numero_tva_intracommunautaire
         */
        private $numero_tva_intracommunautaire;
     
        /**
         * @var array $userRoles
         */
        private $userRoles;
     
     
        private $salt;
     
        public function __toString() {
            return $this->username;
        }
     
       /**
         * Gets an array of roles.
         *
         * @return array An array of Role objects
         */
        public function getRoles()
        {
            //return array('ROLE_ADMIN');
            return $this->getUserRoles()->toArray();
        }
     
        /**
         * Gets the user roles.
         *
         * @return ArrayCollection A Doctrine ArrayCollection
         */
        public function getUserRoles()
        {
            return $this->userRoles;
        }
     
        /**
         * Compares this user to another to determine if they are the same.
         *
         * @param UserInterface $user The user
         * @return boolean True if equal, false othwerwise.
         */
        public function equals(UserInterface $user)
        {
     
            return md5($this->getUsername()) == md5($user->getUsername());
        }
     
        /**
         * Gets the user salt.
         *
         * @return string The salt.
         */
        public function getSalt()
        {
            return '';
            //return '$this->salt';
        }
     
        /**
         * Sets the user salt.
         *
         * @param string $value The salt.
         */
        public function setSalt($value)
        {
            $this->salt = md5($value);
        }
     
        /**
         * Erases the user credentials.
         */
        public function eraseCredentials()
        {
     
        }
     
        /**
         * Get id
         *
         * @return integer 
         */
        public function getId()
        {
            return $this->id;
        }
     
        /**
         * Set username
         *
         * @param string $username
         */
        public function setUsername($username)
        {
            $this->username = $username;
        }
     
        /**
         * Get username
         *
         * @return string 
         */
        public function getUsername()
        {
            return $this->username;
        }
     
        /**
         * Set password
         *
         * @param string $password
         */
        public function setPassword($password)
        {
            $this->password = $password;
        }
     
        /**
         * Get password
         *
         * @return string 
         */
        public function getPassword()
        {
            return $this->password;
        }
     
        /**
         * Set nom
         *
         * @param string $nom
         */
        public function setNom($nom)
        {
            $this->nom = $nom;
        }
     
        /**
         * Get nom
         *
         * @return string 
         */
        public function getNom()
        {
            return $this->nom;
        }
     
        /**
         * Set prenom
         *
         * @param string $prenom
         */
        public function setPrenom($prenom)
        {
            $this->prenom = $prenom;
        }
     
        /**
         * Get prenom
         *
         * @return string 
         */
        public function getPrenom()
        {
            return $this->prenom;
        }
     
        /**
         * Set entreprise
         *
         * @param string $entreprise
         */
        public function setEntreprise($entreprise)
        {
            $this->entreprise = $entreprise;
        }
     
        /**
         * Get entreprise
         *
         * @return string 
         */
        public function getEntreprise()
        {
            return $this->entreprise;
        }
     
        /**
         * Set adresse1
         *
         * @param string $adresse1
         */
        public function setAdresse1($adresse1)
        {
            $this->adresse1 = $adresse1;
        }
     
        /**
         * Get adresse1
         *
         * @return string 
         */
        public function getAdresse1()
        {
            return $this->adresse1;
        }
     
        /**
         * Set adresse2
         *
         * @param string $adresse2
         */
        public function setAdresse2($adresse2)
        {
            $this->adresse2 = $adresse2;
        }
     
        /**
         * Get adresse2
         *
         * @return string 
         */
        public function getAdresse2()
        {
            return $this->adresse2;
        }
     
        /**
         * Set cp
         *
         * @param string $cp
         */
        public function setCp($cp)
        {
            $this->cp = $cp;
        }
     
        /**
         * Get cp
         *
         * @return string 
         */
        public function getCp()
        {
            return $this->cp;
        }
     
        /**
         * Set ville
         *
         * @param string $ville
         */
        public function setVille($ville)
        {
            $this->ville = $ville;
        }
     
        /**
         * Get ville
         *
         * @return string 
         */
        public function getVille()
        {
            return $this->ville;
        }
     
        /**
         * Set pays_id
         *
         * @param integer $paysId
         */
        public function setPaysId($paysId)
        {
            $this->pays_id = $paysId;
        }
     
        /**
         * Get pays_id
         *
         * @return integer 
         */
        public function getPaysId()
        {
            return $this->pays_id;
        }
     
        /*public function getTest() {
    
            
            return $this->getPaysId()->getPays();
        }*/
     
        /**
         * Set numero_tva_intracommunautaire
         *
         * @param string $numeroTvaIntracommunautaire
         */
        public function setNumeroTvaIntracommunautaire($numeroTvaIntracommunautaire)
        {
            $this->numero_tva_intracommunautaire = $numeroTvaIntracommunautaire;
        }
     
        /**
         * Get numero_tva_intracommunautaire
         *
         * @return string 
         */
        public function getNumeroTvaIntracommunautaire()
        {
            return $this->numero_tva_intracommunautaire;
        }
     
     
        public function serialize() {
            return implode(',', array(
                'username'    => $this->getUsername()
            ));
        }
     
     
        public function unserialize($strSerialized) {
            $serialized = explode(',', $strSerialized);
     
     
            $this->setUsername($serialized[0]);
     
        }
     
     
     
     
    }
    Pays.php
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    <?php
     
    namespace Ito\DefaultBundle\Entity;
     
    use Doctrine\ORM\Mapping as ORM;
     
    /**
     * Ito\DefaultBundle\Entity\Pays
     */
    class Pays
    {
        /**
         * @var integer $id
         */
        private $id;
     
        /**
         * @var string $pays
         */
        private $pays;
     
        public function  __toString() {
            return $this->pays;
        }
     
        /**
         * Get id
         *
         * @return integer 
         */
        public function getId()
        {
            return $this->id;
        }
     
        /**
         * Set pays
         *
         * @param string $pays
         */
        public function setPays($pays)
        {
            $this->pays = $pays;
        }
     
        /**
         * Get pays
         *
         * @return string 
         */
        public function getPays()
        {
            return $this->pays;
        }
    }
    Comment faire pour récupérer facilement le nom du pays dans un foreach?

    Merci

  2. #2
    Membre averti
    Homme Profil pro
    Développeur Web
    Inscrit en
    Avril 2004
    Messages
    318
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 41
    Localisation : France, Ille et Vilaine (Bretagne)

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

    Informations forums :
    Inscription : Avril 2004
    Messages : 318
    Points : 362
    Points
    362
    Par défaut
    Bonjour.

    Tout d'abord pour accèder à une entité en relation, ce n'est pas "$user->getPaysId()->getPays()" qu'il faut faire, mais simplement $user->getPays().

    En plus je pense que ta relation n'est pas la bonne. Ca serait plus un manyToOne, de manière que de ton entité pays, tu ais une fonction getUsers(), listant tout les utilisateurs du pays.

    Donc dans ton mapping User :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    manyToOne:
        pays:
          targetEntity: tonchemindebundle\Entity\Pays
          joinColumns:
            pays_id:
              referencedColumnName: id
              nullable: false
    Et dans ton mapping Pays :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    oneToMany:
        users:
          targetEntity: tonchemindebundle\Entity\User
          mappedBy: pays
    Moi j'aurais fait comme ca, après ce n'est peut-être pas la solution. Mais je préfère avoir la relation des deux côtés.

  3. #3
    Membre à l'essai
    Profil pro
    Inscrit en
    Octobre 2008
    Messages
    22
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Octobre 2008
    Messages : 22
    Points : 13
    Points
    13
    Par défaut
    Merci, pour l'aiguillage, c'était presque ca. En fait j'ai du ajouter un manyToOne en plus de mon OneToOne et cela fonctionne. J'ai du garder le oneTonOne pour l'edition des infos sinon j'avais un champ text au lieu d'une liste déroulante. Le oneToMany pour Pays est optionnel.

    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
    Ito\DefaultBundle\Entity\Users:
      type: entity
      table: users
      repositoryClass: Ito\DefaultBundle\Repository\UsersRepository
      manyToMany:
        userRoles:
          targetEntity: Roles
          joinTable:
            name: users_roles
            joinColumns:
              user_id:
                referencedColumnName: id
            inverseJoinColumns:
              role_id:
                referencedColumnName: id
      oneToOne:
        pays_id:
          targetEntity: Pays
          joinColumn:
            name: pays_id
            referencedColumnName: id
      manyToOne:
        pays:
          targetEntity: Pays
          joinColumn:
            name: pays_id
            referencedColumnName: id
      fields:
        id:
          type: integer
          id: true
          generator:
            strategy: AUTO
        username:
          type: string
          length: 255
        password:
          type: string
          length: 255
        nom:
          type: string
          length: 255
        prenom:
          type: string
          length: 255
        entreprise:
          type: string
          length: 255
        adresse1:
          type: string
          length: 255
        adresse2:
          type: string
          length: 255
        cp:
          type: string
          length: 255
        ville:
          type: string
          length: 255
     
     
        numero_tva_intracommunautaire:
          type: string
          length: 255
      lifecycleCallbacks: {  }

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

Discussions similaires

  1. affichage d'un objet uicontrol texte dans une boucle
    Par oliv27400 dans le forum Interfaces Graphiques
    Réponses: 8
    Dernier message: 25/08/2010, 14h35
  2. Mise en forme de l'affichage dans une boucle
    Par sam01 dans le forum Langage
    Réponses: 6
    Dernier message: 20/05/2010, 16h50
  3. Affichage JLabel dans une boucle for
    Par krikri150489 dans le forum Composants
    Réponses: 4
    Dernier message: 27/04/2009, 20h03
  4. Réponses: 5
    Dernier message: 30/06/2008, 14h43
  5. Forcer l'affichage dans une boucle
    Par jb36123 dans le forum Général JavaScript
    Réponses: 4
    Dernier message: 01/08/2006, 14h40

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