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

ORM PHP Discussion :

Mapping des entités


Sujet :

ORM PHP

  1. #1
    Candidat au Club
    Homme Profil pro
    Étudiant
    Inscrit en
    Juin 2019
    Messages
    3
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 25
    Localisation : France, Nord (Nord Pas de Calais)

    Informations professionnelles :
    Activité : Étudiant
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Juin 2019
    Messages : 3
    Points : 2
    Points
    2
    Par défaut Mapping des entités
    Bonjour,
    je suis un débutant sous Symfony 4 et je suis bloqué sur un exercice que l'on m'a donné. L'exercice est le suivant : je dois mettre en place les relations manquantes en fonction du diagramme de classe (je vous met toutes les informations ci-dessous).

    Nom : mapping.PNG
Affichages : 726
Taille : 150,6 Ko

    Pour réaliser ses relations manquantes, on m'a fournis un projet Symfony avec les différents fichiers contenant les entités. Les variables sont déjà déclarés dans les fichiers en question, et je dois remplir les ORM. Cependant, je suis perturbée à propos de ce diagramme de classe. Par exemple, entre "Pizza" et "IngredientPizza", la relation est un OneToMany partant de "Pizza" vers "IngredientPizza", mais je n'arrive pas à mettre en place cette relation.

    Ma question est donc la suivante : quelqu'un peut-il m'expliquer comment mettre en place la relation entre la table "Pizza" et "IngredientPizza" ?
    Je vous met le code des fichiers "IngredientPizza.php" et "Pizza.php" afin que vous ayez toutes les ressources nécéssaire.

    Entity\Pizza.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
     
    <?php
     
    declare(strict_types = 1);
     
    namespace App\Entity;
     
    use Doctrine\Common\Collections\ArrayCollection;
    use Doctrine\Common\Collections\Collection;
    use Doctrine\ORM\Mapping as ORM;
     
    /**
     * @ORM\Table(name="pizza")
     * @ORM\Entity(repositoryClass="App\Repository\PizzaRepository")
     */
    class Pizza
    {
        /**
         * @var int
         * @ORM\Column(name="id_pizza", type="integer")
         * @ORM\Id
         * @ORM\GeneratedValue(strategy="AUTO")
         */
        private $id;
     
        /**
         * @var string
         * @ORM\Column(name="nom", type="string", length=255, unique=true)
         */
        private $nom;
     
        /**
         * @var Collection
         */
        private $quantiteIngredients;
     
        /**
         * Constructor
         */
        public function __construct()
        {
            $this->quantiteIngredients = new ArrayCollection();
        }
     
        /**
         * @return int
         */
        public function getId(): ?int
        {
            return $this->id;
        }
     
        /**
         * @param int $id
         * @return Pizza
         */
        public function setId(int $id): Pizza
        {
            $this->id = $id;
     
            return $this;
        }
     
        /**
         * @return string
         */
        public function getNom(): ?string
        {
            return $this->nom;
        }
     
        /**
         * @param string $nom
         * @return Pizza
         */
        public function setNom(string $nom): Pizza
        {
            $this->nom = $nom;
     
            return $this;
        }
     
        /**
         * @param IngredientPizza $quantiteIngredients
         * @return Pizza
         */
        public function addQuantiteIngredients(IngredientPizza $quantiteIngredients): Pizza
        {
            $this->quantiteIngredients[] = $quantiteIngredients;
     
            return $this;
        }
     
        /**
         * @param IngredientPizza $quantiteIngredients
         */
        public function removeQuantiteIngredient(IngredientPizza $quantiteIngredients): void
        {
            $this->quantiteIngredients->removeElement($quantiteIngredients);
        }
     
        /**
         * @return Collection
         */
        public function getQuantiteIngredients(): Collection
        {
            return $this->quantiteIngredients;
        }
    }
    Entity\IngredientPizza.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
     
    <?php
     
    declare(strict_types = 1);
     
    namespace App\Entity;
     
    use Doctrine\ORM\Mapping as ORM;
     
    /**
     * @ORM\Table(name="nombre_ingredient_par_pizza")
     * @ORM\Entity(repositoryClass="App\Repository\IngredientPizzaRepository")
     */
    class IngredientPizza
    {
        /** @var float */
        const GRAMME_VERS_KILO = 0.001;
     
        /**
         * @var int
         * @ORM\Column(name="id_ingredient_pizza", type="integer")
         * @ORM\Id
         * @ORM\GeneratedValue(strategy="AUTO")
         */
        private $id;
     
        /**
         * La quanité de l'ingrédient en gramme
         * @var int
         * @ORM\Column(name="quantite", type="integer")
         */
        private $quantite;
     
        /**
         * @var Ingredient
         * @ORM\ManyToOne(targetEntity="App\Entity\Ingredient")
         * @ORM\JoinColumn(
         *     name="ingredient_id",
         *     referencedColumnName="id_ingredient"
         * )
         */
        private $ingredient;
     
        /**
         * @param float $grammes
         * @return float
         */
        public static function convertirGrammeEnKilo($grammes)
        {
            return (float) $grammes * self::GRAMME_VERS_KILO;
        }
     
        /**
         * @return int
         */
        public function getId(): ?int
        {
            return $this->id;
        }
     
        /**
         * @param int $id
         * @return IngredientPizza
         */
        public function setId(int $id): IngredientPizza
        {
            $this->id = $id;
     
            return $this;
        }
     
        /**
         * @return int
         */
        public function getQuantite(): ?int
        {
            return $this->quantite;
        }
     
        /**
         * @param int $quantite
         * @return IngredientPizza
         */
        public function setQuantite(int $quantite): IngredientPizza
        {
            $this->quantite = $quantite;
     
            return $this;
        }
     
        /**
         * @return Ingredient
         */
        public function getIngredient(): ?Ingredient
        {
            return $this->ingredient;
        }
     
        /**
         * @param Ingredient $ingredient
         * @return IngredientPizza
         */
        public function setIngredient(Ingredient $ingredient): IngredientPizza
        {
            $this->ingredient = $ingredient;
     
            return $this;
        }
    }
    Il y a d'autres relations à mettre en place, comme celle entre la table "Pizzeria" et "Pizza", mais je préfère que l'on s'y interesse une fois que j'aurais compris la première relation entre la table "Pizza" et "IngredientPizza".
    Je vous remercie d'avance pour votre aide, je regarderais dès que possible les réponses sur ce post (plusieurs fois par jour).
    Bonne journée

  2. #2
    Modérateur

    Avatar de MaitrePylos
    Homme Profil pro
    DBA
    Inscrit en
    Juin 2005
    Messages
    5 496
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 51
    Localisation : Belgique

    Informations professionnelles :
    Activité : DBA
    Secteur : Service public

    Informations forums :
    Inscription : Juin 2005
    Messages : 5 496
    Points : 12 596
    Points
    12 596
    Par défaut
    Bonsoir, tu peux modifier ton Entity avec
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    php bin/console make:entity Pizza
    et ton définir ta relation, il fera le tout tout seul.

  3. #3
    Candidat au Club
    Homme Profil pro
    Étudiant
    Inscrit en
    Juin 2019
    Messages
    3
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 25
    Localisation : France, Nord (Nord Pas de Calais)

    Informations professionnelles :
    Activité : Étudiant
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Juin 2019
    Messages : 3
    Points : 2
    Points
    2
    Par défaut
    J'ai fait 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
    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
     
    PS C:\wamp64\www\Symfony\test-pizzeria-master> php bin/console make:entity Pizza
     
     Your entity already exists! So let's add some new fields!
     
     New property name (press <return> to stop adding fields):
     > quantiteIngredients
     
     Field type (enter ? to see all types) [string]:
     > relation
     
     What class should this entity be related to?:
     > IngredientPizza
     
    What type of relationship is this?
     ------------ ----------------------------------------------------------------------------
      Type         Description                                                                
     ------------ ----------------------------------------------------------------------------
      ManyToOne    Each Pizza relates to (has) one IngredientPizza.
                   Each IngredientPizza can relate to (can have) many Pizza objects
     
      OneToMany    Each Pizza can relate to (can have) many IngredientPizza objects.
                   Each IngredientPizza relates to (has) one Pizza
     
      ManyToMany   Each Pizza can relate to (can have) many IngredientPizza objects.
                   Each IngredientPizza can also relate to (can also have) many Pizza objects
     
      OneToOne     Each Pizza relates to (has) exactly one IngredientPizza.
                   Each IngredientPizza also relates to (has) exactly one Pizza.
     ------------ ----------------------------------------------------------------------------
     
     Relation type? [ManyToOne, OneToMany, ManyToMany, OneToOne]:
     > OneToMany
     
     A new property will also be added to the IngredientPizza class so that you can access and set the related Pizza object from it.
     
     New field name inside IngredientPizza [pizza]:
     >pizza
     
     Is the IngredientPizza.pizza property allowed to be null (nullable)? (yes/no) [yes]:
     >yes
     
     Not generating IngredientPizza::getPizza(): method already exists
     Not generating IngredientPizza::setPizza(): method already exists
     Not generating Pizza::getQuantiteIngredients(): method already exists
     Not generating Pizza::addQuantiteIngredient(): method already exists
     Not generating Pizza::removeQuantiteIngredient(): method already exists
     updated: src/Entity/Pizza.php
     updated: src/Entity/IngredientPizza.php
     
     Add another property? Enter the property name (or press <return> to stop adding fields):
     >
     
     
     
      Success! 
     
     
     Next: When you're ready, create a migration with make:migration
     
    PS C:\wamp64\www\Symfony\test-pizzeria-master>
    Ducoup j'ai obtenu cela (dans la classe Pizza)

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
     
        /**
         * @ORM\OneToMany(targetEntity="App\Entity\IngredientPizza", mappedBy="pizza")
         */
        private $quantiteIngredients;
    Et ceci dans la classe IngredientPizza

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
     
        /**
         * @ORM\ManyToOne(targetEntity="App\Entity\Pizza", inversedBy="quantiteIngredients")
         */
        private $pizza;
    Et lorsque je veux migrer pour voir si la relation est bien prise en compte dans mon phpMyAdmin, j'ai ce retour console qui m'indique une erreur

    Nom : Capture.PNG
Affichages : 549
Taille : 141,7 Ko

    Une idée ?

  4. #4
    Modérateur

    Avatar de MaitrePylos
    Homme Profil pro
    DBA
    Inscrit en
    Juin 2005
    Messages
    5 496
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 51
    Localisation : Belgique

    Informations professionnelles :
    Activité : DBA
    Secteur : Service public

    Informations forums :
    Inscription : Juin 2005
    Messages : 5 496
    Points : 12 596
    Points
    12 596
    Par défaut
    Ben il essaye de supprimer une table test qui n'existe pas.

  5. #5
    Candidat au Club
    Homme Profil pro
    Étudiant
    Inscrit en
    Juin 2019
    Messages
    3
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 25
    Localisation : France, Nord (Nord Pas de Calais)

    Informations professionnelles :
    Activité : Étudiant
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Juin 2019
    Messages : 3
    Points : 2
    Points
    2
    Par défaut
    Dans mon phpmyadmin j'ai ces tables la :

    Nom : Capture.PNG
Affichages : 559
Taille : 71,8 Ko

    Je vais essayer de tout reprendre depuis le début, et remettre en place la relation pour voir

Discussions similaires

  1. [VB.Net]Conversion des entités XML
    Par azerty25 dans le forum Windows Forms
    Réponses: 3
    Dernier message: 18/07/2006, 07h06
  2. identification des entités
    Par Eric26 dans le forum Schéma
    Réponses: 10
    Dernier message: 02/06/2006, 18h23
  3. [Débutant] Problème de mapping des ports
    Par zehle dans le forum VHDL
    Réponses: 1
    Dernier message: 22/05/2006, 22h37
  4. [Tableaux] décoder des entités
    Par stehga dans le forum Langage
    Réponses: 6
    Dernier message: 16/01/2006, 12h53
  5. [MSXML] Comment empécher la conversion des entités ?
    Par nima dans le forum XSL/XSLT/XPATH
    Réponses: 3
    Dernier message: 08/11/2002, 14h14

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