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 :

relation entre entité


Sujet :

Symfony PHP

  1. #1
    Membre à l'essai
    Inscrit en
    Juillet 2010
    Messages
    43
    Détails du profil
    Informations forums :
    Inscription : Juillet 2010
    Messages : 43
    Points : 24
    Points
    24
    Par défaut relation entre entité
    bonjour je souhaiterais faire une relation entre deux entité user et description

    voici mon user.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
     
    <?php
     
    namespace App\Entity;
     
    use Doctrine\ORM\Mapping as ORM;
    use Symfony\Component\Security\Core\User\UserInterface;
    use Symfony\Component\Validator\Constraints as Assert;
    use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
    use Doctrine\Common\Collections\ArrayCollection;
     
    /**
     * @ORM\Entity(repositoryClass="App\Repository\UserRepository")
     * @UniqueEntity(
     *     fields = {"username"},
     *     message = "Ce pseudo est déjà utilisé!"
     * )
     * @UniqueEntity(
     *     fields= {"email"},
     *     message= "Cet email es déjà utilisé!",
     * )
     */
    class User implements UserInterface
    {
        /**
         * @ORM\Id()
         * @ORM\GeneratedValue()
         * @ORM\Column(type="integer")
         */
        private $id;
     
        /**
         * @ORM\Column(type="string", length=255)
         * @Assert\Email(
         *     message = "Veuillez renseigner une adresse email valide!"
         * )
         */
        private $email;
     
        /**
         * @ORM\Column(type="string", length=255)
         * @Assert\Length(min="5", minMessage="Mot de passe trop court! (Minimum 5 caractères)")
         */
        private $username;
     
        /**
         * @ORM\Column(type="string", length=255)
         * @Assert\Length(min="8", minMessage="Mot de passe trop court! (Minimum 8 caractères)")
         */
        private $password;
     
        /**
         * @Assert\EqualTo(propertyPath="password", message="Les deux mots de passe ne sont pas identiques!")
         */
     
        public $confirm_password;
     
        /**
         * @ORM\Column(type="string", length=255)
         */
        private $sexe;
     
        /**
         * @ORM\OneToOne(targetEntity="App\Entity\Description", cascade={"persist", "remove"})
         * @ORM\JoinColumn(nullable=false)
         */
        private $description;
     
     
     
     
     
     
        public function getId(): ?int
        {
            return $this->id;
        }
     
        public function getEmail(): ?string
        {
            return $this->email;
        }
     
        public function setEmail(string $email): self
        {
            $this->email = $email;
     
            return $this;
        }
     
        public function getUsername(): ?string
        {
            return $this->username;
        }
     
        public function setUsername(string $username): self
        {
            $this->username = $username;
     
            return $this;
        }
     
        public function getPassword(): ?string
        {
            return $this->password;
        }
     
        public function setPassword(string $password): self
        {
            $this->password = $password;
     
            return $this;
        }
     
        public function getSexe(): ?string
        {
            return $this->sexe;
        }
     
        public function setSexe(string $sexe): self
        {
            $this->sexe = $sexe;
     
            return $this;
        }
     
        public function eraseCredentials()
        {
        }
        public function getSalt()
        {
        }
        public function getRoles()
        {
            return ['ROLE_USER'];
        }
     
        public function getDescription(): ?Description
        {
            return $this->description;
        }
     
        public function setDescription(Description $description): self
        {
            $this->description = $description;
     
            return $this;
        }
    }
    mon description.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
    411
    412
    413
    414
    415
    416
     
    <?php
     
    namespace App\Entity;
     
    use Doctrine\ORM\Mapping as ORM;
     
    /**
     * @ORM\Entity(repositoryClass="App\Repository\DescriptionRepository")
     */
    class Description
    {
        /**
         * @ORM\Id()
         * @ORM\GeneratedValue()
         * @ORM\Column(type="integer")
         */
        private $id;
     
        /**
         * @ORM\Column(type="integer")
         */
        private $user_id;
     
        /**
         * @ORM\Column(type="string", length=255, nullable=true)
         */
        private $naissance_lui;
     
        /**
         * @ORM\Column(type="string", length=255, nullable=true)
         */
        private $naissance_elle;
     
        /**
         * @ORM\Column(type="string", length=255, nullable=true)
         */
        private $yeux_lui;
     
        /**
         * @ORM\Column(type="string", length=255, nullable=true)
         */
        private $yeux_elle;
     
        /**
         * @ORM\Column(type="string", length=255, nullable=true)
         */
        private $cheveux_lui;
     
        /**
         * @ORM\Column(type="string", length=255, nullable=true)
         */
        private $cheveux_elle;
     
        /**
         * @ORM\Column(type="string", length=255, nullable=true)
         */
        private $taille_lui;
     
        /**
         * @ORM\Column(type="string", length=255, nullable=true)
         */
        private $taille_elle;
     
        /**
         * @ORM\Column(type="string", length=255, nullable=true)
         */
        private $poids_lui;
     
        /**
         * @ORM\Column(type="string", length=255, nullable=true)
         */
        private $poids_elle;
     
        /**
         * @ORM\Column(type="string", length=255, nullable=true)
         */
        private $silhouette_lui;
     
        /**
         * @ORM\Column(type="string", length=255, nullable=true)
         */
        private $silhouette_elle;
     
        /**
         * @ORM\Column(type="string", length=255, nullable=true)
         */
        private $profession_lui;
     
        /**
         * @ORM\Column(type="string", length=255, nullable=true)
         */
        private $profession_elle;
     
        /**
         * @ORM\Column(type="string", length=255, nullable=true)
         */
        private $fumeur_lui;
     
        /**
         * @ORM\Column(type="string", length=255, nullable=true)
         */
        private $fumeur_elle;
     
        /**
         * @ORM\Column(type="string", length=255, nullable=true)
         */
        private $origine_lui;
     
        /**
         * @ORM\Column(type="string", length=255, nullable=true)
         */
        private $origine_elle;
     
        /**
         * @ORM\Column(type="string", length=255, nullable=true)
         */
        private $sexualite_lui;
     
        /**
         * @ORM\Column(type="string", length=255, nullable=true)
         */
        private $sexualite_elle;
     
        /**
         * @ORM\Column(type="string", length=255, nullable=true)
         */
        private $statut;
     
        /**
         * @ORM\Column(type="string", length=255, nullable=true)
         */
        private $enfant;
     
        public function getId(): ?int
        {
            return $this->id;
        }
     
        public function getUserId(): ?int
        {
            return $this->user_id;
        }
     
        public function setUserId(int $user_id): self
        {
            $this->user_id = $user_id;
     
            return $this;
        }
     
        public function getNaissanceLui(): ?string
        {
            return $this->naissance_lui;
        }
     
        public function setNaissanceLui(?string $naissance_lui): self
        {
            $this->naissance_lui = $naissance_lui;
     
            return $this;
        }
     
        public function getNaissanceElle(): ?string
        {
            return $this->naissance_elle;
        }
     
        public function setNaissanceElle(?string $naissance_elle): self
        {
            $this->naissance_elle = $naissance_elle;
     
            return $this;
        }
     
        public function getYeuxLui(): ?string
        {
            return $this->yeux_lui;
        }
     
        public function setYeuxLui(?string $yeux_lui): self
        {
            $this->yeux_lui = $yeux_lui;
     
            return $this;
        }
     
        public function getYeuxElle(): ?string
        {
            return $this->yeux_elle;
        }
     
        public function setYeuxElle(?string $yeux_elle): self
        {
            $this->yeux_elle = $yeux_elle;
     
            return $this;
        }
     
        public function getCheveuxLui(): ?string
        {
            return $this->cheveux_lui;
        }
     
        public function setCheveuxLui(?string $cheveux_lui): self
        {
            $this->cheveux_lui = $cheveux_lui;
     
            return $this;
        }
     
        public function getCheveuxElle(): ?string
        {
            return $this->cheveux_elle;
        }
     
        public function setCheveuxElle(?string $cheveux_elle): self
        {
            $this->cheveux_elle = $cheveux_elle;
     
            return $this;
        }
     
        public function getTailleLui(): ?string
        {
            return $this->taille_lui;
        }
     
        public function setTailleLui(?string $taille_lui): self
        {
            $this->taille_lui = $taille_lui;
     
            return $this;
        }
     
        public function getTailleElle(): ?string
        {
            return $this->taille_elle;
        }
     
        public function setTailleElle(?string $taille_elle): self
        {
            $this->taille_elle = $taille_elle;
     
            return $this;
        }
     
        public function getPoidsLui(): ?string
        {
            return $this->poids_lui;
        }
     
        public function setPoidsLui(?string $poids_lui): self
        {
            $this->poids_lui = $poids_lui;
     
            return $this;
        }
     
        public function getPoidsElle(): ?string
        {
            return $this->poids_elle;
        }
     
        public function setPoidsElle(?string $poids_elle): self
        {
            $this->poids_elle = $poids_elle;
     
            return $this;
        }
     
        public function getSilhouetteLui(): ?string
        {
            return $this->silhouette_lui;
        }
     
        public function setSilhouetteLui(?string $silhouette_lui): self
        {
            $this->silhouette_lui = $silhouette_lui;
     
            return $this;
        }
     
        public function getSilhouetteElle(): ?string
        {
            return $this->silhouette_elle;
        }
     
        public function setSilhouetteElle(?string $silhouette_elle): self
        {
            $this->silhouette_elle = $silhouette_elle;
     
            return $this;
        }
     
        public function getProfessionLui(): ?string
        {
            return $this->profession_lui;
        }
     
        public function setProfessionLui(?string $profession_lui): self
        {
            $this->profession_lui = $profession_lui;
     
            return $this;
        }
     
        public function getProfessionElle(): ?string
        {
            return $this->profession_elle;
        }
     
        public function setProfessionElle(?string $profession_elle): self
        {
            $this->profession_elle = $profession_elle;
     
            return $this;
        }
     
        public function getFumeurLui(): ?string
        {
            return $this->fumeur_lui;
        }
     
        public function setFumeurLui(?string $fumeur_lui): self
        {
            $this->fumeur_lui = $fumeur_lui;
     
            return $this;
        }
     
        public function getFumeurElle(): ?string
        {
            return $this->fumeur_elle;
        }
     
        public function setFumeurElle(?string $fumeur_elle): self
        {
            $this->fumeur_elle = $fumeur_elle;
     
            return $this;
        }
     
        public function getOrigineLui(): ?string
        {
            return $this->origine_lui;
        }
     
        public function setOrigineLui(?string $origine_lui): self
        {
            $this->origine_lui = $origine_lui;
     
            return $this;
        }
     
        public function getOrigineElle(): ?string
        {
            return $this->origine_elle;
        }
     
        public function setOrigineElle(?string $origine_elle): self
        {
            $this->origine_elle = $origine_elle;
     
            return $this;
        }
     
        public function getSexualiteLui(): ?string
        {
            return $this->sexualite_lui;
        }
     
        public function setSexualiteLui(?string $sexualite_lui): self
        {
            $this->sexualite_lui = $sexualite_lui;
     
            return $this;
        }
     
        public function getSexualiteElle(): ?string
        {
            return $this->sexualite_elle;
        }
     
        public function setSexualiteElle(?string $sexualite_elle): self
        {
            $this->sexualite_elle = $sexualite_elle;
     
            return $this;
        }
     
        public function getStatut(): ?string
        {
            return $this->statut;
        }
     
        public function setStatut(?string $statut): self
        {
            $this->statut = $statut;
     
            return $this;
        }
     
        public function getEnfant(): ?string
        {
            return $this->enfant;
        }
     
        public function setEnfant(?string $enfant): self
        {
            $this->enfant = $enfant;
     
            return $this;
        }
     
    }
    et mon SecurityController comme ceci:

    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
     
    public function registration(Request $request, ObjectManager $manager, UserPasswordEncoderInterface $encoder) {
        $user = new User();
        $description = new Description();
        $form = $this->createForm(RegistrationType::class, $user);
        $form->handleRequest($request);
     
        if ($form->isSubmitted() && $form->isValid()) {
            $description->setYeuxLui('Bleus');
     
            $hash = $encoder->encodePassword($user, $user->getPassword());
            $user->setPassword($hash);
            $user->setDescription($description);
            $manager->persist($description);
            $manager->persist($user);
            $manager->flush();
     
     
     
            return $this->redirectToRoute('security_login');
        }
     
        return $this->render('security/registration.html.twig', [
            'form' => $form->createView()
        ]);
     
    }
    je me suis basé sur la doc de symfony ici:

    https://symfony.com/doc/current/doct...ociations.html

    mais à la soumission de mon formulaire je rencontre l'erreur suivante:
    An exception occurred while executing 'INSERT INTO description (user_id, naissance_lui, naissance_elle, yeux_lui, yeux_elle, cheveux_lui, cheveux_elle, taille_lui, taille_elle, poids_lui, poids_elle, silhouette_lui, silhouette_elle, profession_lui, profession_elle, fumeur_lui, fumeur_elle, origine_lui, origine_elle, sexualite_lui, sexualite_elle, statut, enfant) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)' with params [null, null, null, "Bleus", null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null]:

    SQLSTATE[23000]: Integrity constraint violation: 1048 Le champ 'user_id' ne peut être vide (null)

  2. #2
    Membre expert
    Avatar de Dendrite
    Femme Profil pro
    Développeuse informatique
    Inscrit en
    Juin 2008
    Messages
    2 129
    Détails du profil
    Informations personnelles :
    Sexe : Femme
    Âge : 58
    Localisation : France, Paris (Île de France)

    Informations professionnelles :
    Activité : Développeuse informatique
    Secteur : Administration - Collectivité locale

    Informations forums :
    Inscription : Juin 2008
    Messages : 2 129
    Points : 3 627
    Points
    3 627
    Billets dans le blog
    8
    Par défaut
    Bonjour !
    Tu sais qu'il y a un truc qui s'appelle la CNIL dans ce pays, et qui te dira que ça :

    origine_lui, origine_elle, sexualite_lui, sexualite_elle,

    Tu n'as tout simplement pas le DROIT de le stocker.
    PDO, une soupe et au lit !
    Partir de la fin est un bon moyen de retrouver son chemin. Bibi - 2020

  3. #3
    Membre à l'essai
    Inscrit en
    Juillet 2010
    Messages
    43
    Détails du profil
    Informations forums :
    Inscription : Juillet 2010
    Messages : 43
    Points : 24
    Points
    24
    Par défaut
    ah bon??

    et depuis quand la CNIL interdit de stoquer l'origine et le sexe des inscrits??

    donc du coup meetic, baddo, libertic et j'en passe doivent donc fermer??

    si j'ai bien compris ton message...

    en attendant ça règle pas mon soucis mais bon...

  4. #4
    Membre confirmé
    Homme Profil pro
    Étudiant
    Inscrit en
    Juillet 2011
    Messages
    351
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 32
    Localisation : France, Ain (Rhône Alpes)

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Juillet 2011
    Messages : 351
    Points : 582
    Points
    582
    Par défaut
    Salut,

    Dendrite a raison de t’alerter sur ce point car ce sont-là des données dîtes "sensibles" : https://www.cnil.fr/fr/definition/donnee-sensible
    Comme tu le verras si tu te documentes un peu sur le site de la CNIL, c'est désormais le RGPD qui s'applique et qui encadre (strictement) l'utilisation des données personnelles, et encore plus strictement les données sensibles en interdisant a priori leur recueil et leur usage sauf dans des cas bien précis (cf. lien de la CNIL).
    Donc il est pertinent de penser en amont à comment appliquer le RGPD dans le cas de ton application, que ce soit en matière de consentement, d'information ou encore de sécurité : https://www.cnil.fr/fr/rgpd-par-ou-commencer

    Sinon retour à ta question d'origine, à mon avis tu es dans un cas similaire à celui décrit ici : https://stackoverflow.com/a/51906028
    Donc soit tu rends ta relation bidirectionnelle (https://www.doctrine-project.org/pro...-bidirectional) :
    Code php : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    //App\Entity\User.php
    /**
    * @ORM\OneToOne(targetEntity="App\Entity\Description", inversedBy="user_id", cascade={"persist", "remove"})
    * @ORM\JoinColumn(nullable=false)
    */
    private $description;
    Code php : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    //App\Entity\Description.php
    /**
    * ORM\OneToOne(targetEntity="App\Entity\User", mappedBy="description")
    */
    private $user_id;
    Soit rester sur une relation unidirectionnelle (https://www.doctrine-project.org/pro...unidirectional) en supprimant ce qui suit dans Description (la relation étant portée uniquement par User) :
    Code php : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    /**
    * @ORM\Column(type="integer")
    */
    private $user_id;
    Ensuite dans ton contrôleur il te suffira de persister $user (donc tu pourras supprimer $manager->persist($description);).

    Remarque : si tu décides de définir une relation bidirectionnelle, plutôt que $user_id tu devrais renommer la propriété en $user car elle contiendra un objet User complet et pas seulement son id.

  5. #5
    Membre à l'essai
    Inscrit en
    Juillet 2010
    Messages
    43
    Détails du profil
    Informations forums :
    Inscription : Juillet 2010
    Messages : 43
    Points : 24
    Points
    24
    Par défaut
    j'ai donc modifié pour faire une relation ManyToOne donc du coup l'enregistrement ce fait bien
    par contre la connection à l'user ne se fait plus j'ai droit à un jolie message comme quoi les identifiants ne sont pas bons

    mon User.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
    <?php
     
    namespace App\Entity;
     
    use Doctrine\ORM\Mapping as ORM;
    use Symfony\Component\Security\Core\User\UserInterface;
    use Symfony\Component\Validator\Constraints as Assert;
    use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
    use Doctrine\Common\Collections\ArrayCollection;
     
    /**
     * @ORM\Entity(repositoryClass="App\Repository\UserRepository")
     * @UniqueEntity(
     *     fields = {"username"},
     *     message = "Ce pseudo est déjà utilisé!"
     * )
     * @UniqueEntity(
     *     fields= {"email"},
     *     message= "Cet email es déjà utilisé!",
     * )
     */
    class User implements UserInterface
    {
        /**
         * @ORM\Id()
         * @ORM\GeneratedValue()
         * @ORM\Column(type="integer")
         */
        private $id;
     
        /**
         * @ORM\Column(type="string", length=255)
         * @Assert\Email(
         *     message = "Veuillez renseigner une adresse email valide!"
         * )
         */
        private $email;
     
        /**
         * @ORM\Column(type="string", length=255)
         * @Assert\Length(min="5", minMessage="Mot de passe trop court! (Minimum 5 caractères)")
         */
        private $username;
     
        /**
         * @ORM\Column(type="string", length=255)
         * @Assert\Length(min="8", minMessage="Mot de passe trop court! (Minimum 8 caractères)")
         */
        private $password;
     
        /**
         * @Assert\EqualTo(propertyPath="password", message="Les deux mots de passe ne sont pas identiques!")
         */
     
        public $confirm_password;
     
        /**
         * @ORM\Column(type="string", length=255)
         */
        private $sexe;
     
        /**
         * @ORM\ManyToOne(targetEntity="App\Entity\description", inversedBy="users")
         * @ORM\JoinColumn(nullable=false)
         */
        private $description;
     
     
        public function getId(): ?int
        {
            return $this->id;
        }
     
        public function getEmail(): ?string
        {
            return $this->email;
        }
     
        public function setEmail(string $email): self
        {
            $this->email = $email;
     
            return $this;
        }
     
        public function getUsername(): ?string
        {
            return $this->username;
        }
     
        public function setUsername(string $username): self
        {
            $this->username = $username;
     
            return $this;
        }
     
        public function getPassword(): ?string
        {
            return $this->password;
        }
     
        public function setPassword(string $password): self
        {
            $this->password = $password;
     
            return $this;
        }
     
        public function getSexe(): ?string
        {
            return $this->sexe;
        }
     
        public function setSexe(string $sexe): self
        {
            $this->sexe = $sexe;
     
            return $this;
        }
     
        public function eraseCredentials()
        {
        }
        public function getSalt()
        {
        }
        public function getRoles()
        {
            return ['ROLE_USER'];
        }
     
        public function getDescription(): ?description
        {
            return $this->description;
        }
     
        public function setDescription(?description $description): self
        {
            $this->description = $description;
     
            return $this;
        }
     
     
     
    }
    mon Description.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
    411
    412
    413
    414
    415
    416
    417
    418
    419
    420
    421
    422
    423
    424
    425
    426
    427
    428
    429
    430
    431
    432
    433
    434
    435
    436
    437
    438
    439
    440
    441
    442
    443
    444
    445
    446
    447
    448
    449
    450
    451
    452
    453
    454
    455
    456
    457
    458
    459
    460
    461
    <?php
     
    namespace App\Entity;
     
    use Doctrine\Common\Collections\ArrayCollection;
    use Doctrine\Common\Collections\Collection;
    use Doctrine\ORM\Mapping as ORM;
     
    /**
     * @ORM\Entity(repositoryClass="App\Repository\DescriptionRepository")
     */
    class Description
    {
        /**
         * @ORM\Id()
         * @ORM\GeneratedValue()
         * @ORM\Column(type="integer")
         */
        private $id;
     
     
        /**
         * @ORM\Column(type="string", length=255, nullable=true)
         */
        private $naissance_lui;
     
        /**
         * @ORM\Column(type="string", length=255, nullable=true)
         */
        private $naissance_elle;
     
        /**
         * @ORM\Column(type="string", length=255, nullable=true)
         */
        private $yeux_lui;
     
        /**
         * @ORM\Column(type="string", length=255, nullable=true)
         */
        private $yeux_elle;
     
        /**
         * @ORM\Column(type="string", length=255, nullable=true)
         */
        private $cheveux_lui;
     
        /**
         * @ORM\Column(type="string", length=255, nullable=true)
         */
        private $cheveux_elle;
     
        /**
         * @ORM\Column(type="string", length=255, nullable=true)
         */
        private $taille_lui;
     
        /**
         * @ORM\Column(type="string", length=255, nullable=true)
         */
        private $taille_elle;
     
        /**
         * @ORM\Column(type="string", length=255, nullable=true)
         */
        private $poids_lui;
     
        /**
         * @ORM\Column(type="string", length=255, nullable=true)
         */
        private $poids_elle;
     
        /**
         * @ORM\Column(type="string", length=255, nullable=true)
         */
        private $silhouette_lui;
     
        /**
         * @ORM\Column(type="string", length=255, nullable=true)
         */
        private $silhouette_elle;
     
        /**
         * @ORM\Column(type="string", length=255, nullable=true)
         */
        private $profession_lui;
     
        /**
         * @ORM\Column(type="string", length=255, nullable=true)
         */
        private $profession_elle;
     
        /**
         * @ORM\Column(type="string", length=255, nullable=true)
         */
        private $fumeur_lui;
     
        /**
         * @ORM\Column(type="string", length=255, nullable=true)
         */
        private $fumeur_elle;
     
        /**
         * @ORM\Column(type="string", length=255, nullable=true)
         */
        private $origine_lui;
     
        /**
         * @ORM\Column(type="string", length=255, nullable=true)
         */
        private $origine_elle;
     
        /**
         * @ORM\Column(type="string", length=255, nullable=true)
         */
        private $sexualite_lui;
     
        /**
         * @ORM\Column(type="string", length=255, nullable=true)
         */
        private $sexualite_elle;
     
        /**
         * @ORM\Column(type="string", length=255, nullable=true)
         */
        private $statut;
     
        /**
         * @ORM\Column(type="string", length=255, nullable=true)
         */
        private $enfant;
     
        /**
         * @ORM\Column(type="string", length=255)
         */
        private $username;
     
        /**
         * @ORM\OneToMany(targetEntity="App\Entity\User", mappedBy="description")
         */
        private $users;
     
        public function __construct()
        {
            $this->users = new ArrayCollection();
        }
     
     
        public function getId(): ?int
        {
            return $this->id;
        }
     
        public function getNaissanceLui(): ?string
        {
            return $this->naissance_lui;
        }
     
        public function setNaissanceLui(?string $naissance_lui): self
        {
            $this->naissance_lui = $naissance_lui;
     
            return $this;
        }
     
        public function getNaissanceElle(): ?string
        {
            return $this->naissance_elle;
        }
     
        public function setNaissanceElle(?string $naissance_elle): self
        {
            $this->naissance_elle = $naissance_elle;
     
            return $this;
        }
     
        public function getYeuxLui(): ?string
        {
            return $this->yeux_lui;
        }
     
        public function setYeuxLui(?string $yeux_lui): self
        {
            $this->yeux_lui = $yeux_lui;
     
            return $this;
        }
     
        public function getYeuxElle(): ?string
        {
            return $this->yeux_elle;
        }
     
        public function setYeuxElle(?string $yeux_elle): self
        {
            $this->yeux_elle = $yeux_elle;
     
            return $this;
        }
     
        public function getCheveuxLui(): ?string
        {
            return $this->cheveux_lui;
        }
     
        public function setCheveuxLui(?string $cheveux_lui): self
        {
            $this->cheveux_lui = $cheveux_lui;
     
            return $this;
        }
     
        public function getCheveuxElle(): ?string
        {
            return $this->cheveux_elle;
        }
     
        public function setCheveuxElle(?string $cheveux_elle): self
        {
            $this->cheveux_elle = $cheveux_elle;
     
            return $this;
        }
     
        public function getTailleLui(): ?string
        {
            return $this->taille_lui;
        }
     
        public function setTailleLui(?string $taille_lui): self
        {
            $this->taille_lui = $taille_lui;
     
            return $this;
        }
     
        public function getTailleElle(): ?string
        {
            return $this->taille_elle;
        }
     
        public function setTailleElle(?string $taille_elle): self
        {
            $this->taille_elle = $taille_elle;
     
            return $this;
        }
     
        public function getPoidsLui(): ?string
        {
            return $this->poids_lui;
        }
     
        public function setPoidsLui(?string $poids_lui): self
        {
            $this->poids_lui = $poids_lui;
     
            return $this;
        }
     
        public function getPoidsElle(): ?string
        {
            return $this->poids_elle;
        }
     
        public function setPoidsElle(?string $poids_elle): self
        {
            $this->poids_elle = $poids_elle;
     
            return $this;
        }
     
        public function getSilhouetteLui(): ?string
        {
            return $this->silhouette_lui;
        }
     
        public function setSilhouetteLui(?string $silhouette_lui): self
        {
            $this->silhouette_lui = $silhouette_lui;
     
            return $this;
        }
     
        public function getSilhouetteElle(): ?string
        {
            return $this->silhouette_elle;
        }
     
        public function setSilhouetteElle(?string $silhouette_elle): self
        {
            $this->silhouette_elle = $silhouette_elle;
     
            return $this;
        }
     
        public function getProfessionLui(): ?string
        {
            return $this->profession_lui;
        }
     
        public function setProfessionLui(?string $profession_lui): self
        {
            $this->profession_lui = $profession_lui;
     
            return $this;
        }
     
        public function getProfessionElle(): ?string
        {
            return $this->profession_elle;
        }
     
        public function setProfessionElle(?string $profession_elle): self
        {
            $this->profession_elle = $profession_elle;
     
            return $this;
        }
     
        public function getFumeurLui(): ?string
        {
            return $this->fumeur_lui;
        }
     
        public function setFumeurLui(?string $fumeur_lui): self
        {
            $this->fumeur_lui = $fumeur_lui;
     
            return $this;
        }
     
        public function getFumeurElle(): ?string
        {
            return $this->fumeur_elle;
        }
     
        public function setFumeurElle(?string $fumeur_elle): self
        {
            $this->fumeur_elle = $fumeur_elle;
     
            return $this;
        }
     
        public function getOrigineLui(): ?string
        {
            return $this->origine_lui;
        }
     
        public function setOrigineLui(?string $origine_lui): self
        {
            $this->origine_lui = $origine_lui;
     
            return $this;
        }
     
        public function getOrigineElle(): ?string
        {
            return $this->origine_elle;
        }
     
        public function setOrigineElle(?string $origine_elle): self
        {
            $this->origine_elle = $origine_elle;
     
            return $this;
        }
     
        public function getSexualiteLui(): ?string
        {
            return $this->sexualite_lui;
        }
     
        public function setSexualiteLui(?string $sexualite_lui): self
        {
            $this->sexualite_lui = $sexualite_lui;
     
            return $this;
        }
     
        public function getSexualiteElle(): ?string
        {
            return $this->sexualite_elle;
        }
     
        public function setSexualiteElle(?string $sexualite_elle): self
        {
            $this->sexualite_elle = $sexualite_elle;
     
            return $this;
        }
     
        public function getStatut(): ?string
        {
            return $this->statut;
        }
     
        public function setStatut(?string $statut): self
        {
            $this->statut = $statut;
     
            return $this;
        }
     
        public function getEnfant(): ?string
        {
            return $this->enfant;
        }
     
        public function setEnfant(?string $enfant): self
        {
            $this->enfant = $enfant;
     
            return $this;
        }
     
        public function getUsername(): ?string
        {
            return $this->username;
        }
     
        public function setUsername(string $username): self
        {
            $this->username = $username;
     
            return $this;
        }
     
        /**
         * @return Collection|User[]
         */
        public function getUsers(): Collection
        {
            return $this->users;
        }
     
        public function addUser(User $user): self
        {
            if (!$this->users->contains($user)) {
                $this->users[] = $user;
                $user->setDescription($this);
            }
     
            return $this;
        }
     
        public function removeUser(User $user): self
        {
            if ($this->users->contains($user)) {
                $this->users->removeElement($user);
                // set the owning side to null (unless already changed)
                if ($user->getDescription() === $this) {
                    $user->setDescription(null);
                }
            }
     
            return $this;
        }
     
     
    }
    mon SecurityController.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
    <?php
     
    namespace App\Controller;
     
    use App\Entity\User;
    use App\Entity\Description;
    use App\Form\RegistrationType;
    use Doctrine\Common\Persistence\ObjectManager;
    use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
    use Symfony\Component\HttpFoundation\Request;
    use Symfony\Component\Routing\Annotation\Route;
    use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface;
    use Symfony\Component\Security\Core\Security;
    use Symfony\Component\HttpFoundation\Response;
    use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
     
    class SecurityController extends AbstractController
    {
     
        /**
         * @Route ("/register", name="security_register")
         */
        public function registration(Request $request, ObjectManager $manager, UserPasswordEncoderInterface $encoder) {
            $description = new Description();
            $user = new User();
            $form = $this->createForm(RegistrationType::class, $user);
            $form->handleRequest($request);
     
            if ($form->isSubmitted() && $form->isValid()) {
                $description->setUsername($user->getUsername());
                $user->setDescription($description);
                $hash = $encoder->encodePassword($user, $user->getPassword());
                $user->setPassword($hash);
     
                $manager->persist($description);
                $manager->persist($user);
     
                $manager->flush();
     
     
     
     
                return $this->redirectToRoute('security_login');
            }
     
            return $this->render('security/registration.html.twig', [
                'form' => $form->createView()
            ]);
     
        }
     
        /**
         * @Route("/", name="security_login")
         */
        public function login(AuthenticationUtils $authenticationUtils) {
     
            // get the login error if there is one
            $error = $authenticationUtils->getLastAuthenticationError();
     
            // last username entered by the user
            $lastUsername = $authenticationUtils->getLastUsername();
     
     
     
     
            return $this->render('security/login.html.twig', [
                'last_username' => $lastUsername,
                'error'         => $error,
            ]);
        }
     
        /**
         * @Route("/logout", name="security_logout")
         */
        public function logout() {
     
        }
    }
    et je vous met aussi mon security.yaml au cas ou

    Code yaml : 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
    security:
        encoders:
            App\Entity\User:
                algorithm: 'auto'
        # https://symfony.com/doc/current/security.html#where-do-users-come-from-user-providers
        providers:
            in_memory: { memory: null }
            in_database:
                entity:
                    class: App\Entity\User
                    property: username
        firewalls:
            dev:
                pattern: ^/(_(profiler|wdt)|css|images|js)/
                security: false
     
     
            main:
                anonymous: true
                provider: in_database
     
                form_login:
                    login_path: security_login
                    check_path: security_login
                    csrf_parameter: _csrf_security_token
                    csrf_token_id: a_private_string
                    default_target_path: profile_home
     
                logout:
                    path: security_logout
                    target: security_login
     
                # activate different ways to authenticate
                # https://symfony.com/doc/current/security.html#firewalls-authentication
     
                # https://symfony.com/doc/current/security/impersonating_user.html
                # switch_user: true
     
        # Easy way to control access for large sections of your site
        # Note: Only the *first* access control that matches will be used
        access_control:
            # - { path: ^/admin, roles: ROLE_ADMIN }
            - { path: ^/profile, roles: ROLE_USER }

  6. #6
    Membre confirmé
    Homme Profil pro
    Étudiant
    Inscrit en
    Juillet 2011
    Messages
    351
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 32
    Localisation : France, Ain (Rhône Alpes)

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Juillet 2011
    Messages : 351
    Points : 582
    Points
    582
    Par défaut
    Vérifie dans ta classe Authenticator quelles sont les valeurs utilisées dans checkCredentials, ça peut te mettre sur une piste si certaines sont vides ou nulles.
    Sinon vu que visiblement tout fonctionne jusqu'à la vérification du mot de passe, contrôle que le hash du mot de passe lors de la création est bien identique à celui stocké en base (dd($user->getPassword()); après le flush mais avant la redirection).

    P.S. Par sécurité tu devrais créer une propriété plainPassword pour ne pas risquer de sauvegarder le mot de passe avant de l'encoder. Tu peux trouver le pourquoi du comment ici :
    - https://symfonycasts.com/screencast/...plain-password
    - https://symfonycasts.com/screencast/...-user-password
    (l'accès aux vidéos est payant mais les scripts sont librement accessibles)

  7. #7
    Membre à l'essai
    Inscrit en
    Juillet 2010
    Messages
    43
    Détails du profil
    Informations forums :
    Inscription : Juillet 2010
    Messages : 43
    Points : 24
    Points
    24
    Par défaut
    non j'avais tout vérifié,

    j'avais même enregistré le mdp en claire afin de vérifier que ça ne venais pas de là

    par contre par la suite je me suis aperçu grace au log que l'entité qui était appelé à la connection n'était plus User mais Description
    donc forcément vu que mon password était pas enregistré dans l'entité Description ça ne pouvais pas coller

    j'ai donc compris que je faisais la liaison dans la mauvaise entité et que je devais la faire dans Description

    Du coup tout est rentré dans l'ordre

    Je laisse encore un peut ouvert ce post car je sais pas si on peut répondre ici aux sujets résolu
    si jamais tu voulais laisser une réponse

    je le mettrais en résolu par la suite

Discussions similaires

  1. [2.x] Relation entre entité
    Par lodizzz dans le forum Symfony
    Réponses: 1
    Dernier message: 12/01/2012, 11h06
  2. [2.x] relations entre entités ManyToOne ou ManyToMany
    Par ziemelitis dans le forum Symfony
    Réponses: 5
    Dernier message: 02/01/2012, 18h19
  3. relation entre entités dans microsoft crm
    Par bssouf21 dans le forum Microsoft Dynamics CRM
    Réponses: 3
    Dernier message: 06/12/2011, 18h44
  4. relation entre entités dans microsoft crm
    Par bssouf21 dans le forum Microsoft Dynamics CRM
    Réponses: 0
    Dernier message: 05/12/2011, 10h20
  5. Relation entre entités dans différents bundles
    Par benderpremier dans le forum Doctrine2
    Réponses: 6
    Dernier message: 24/06/2011, 04h06

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