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 :

SonataBundle et relation entre entité


Sujet :

ORM PHP

  1. #1
    Membre à l'essai
    Homme Profil pro
    Étudiant
    Inscrit en
    Novembre 2011
    Messages
    26
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Novembre 2011
    Messages : 26
    Points : 14
    Points
    14
    Par défaut SonataBundle et relation entre entité
    Bonjour tout le monde,

    J'ai trois entités : Catalog, Pack, et CatalogPack

    Entity Catalog
    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
    <?php
     
    namespace AppBundle\Entity\Shop;
     
    use Doctrine\Common\Collections\ArrayCollection;
    use Doctrine\ORM\Mapping as ORM;
     
     
    /**
     * Catalog
     *
     * @ORM\Table(name="catalog")
     * @ORM\Entity(repositoryClass="AppBundle\Repository\Shop\CatalogRepository")
     */
    class Catalog
    {
        /**
         * @var int
         *
         * @ORM\Column(name="id", type="integer")
         * @ORM\Id
         * @ORM\GeneratedValue(strategy="AUTO")
         */
        private $id;
     
        /**
         * @var string
         *
         * @ORM\Column(name="name", type="string", length=255)
         */
        private $name;
     
        /**
         * @var \DateTime
         *
         * @ORM\Column(name="dateVisibility", type="datetime")
         */
        private $dateVisibility;
     
        /**
         * @ORM\OneToOne(targetEntity="AppBundle\Entity\Shop\CatalogCategory")
         */
        private $catalogCategory;
     
        /**
         * @ORM\OneToMany(targetEntity="AppBundle\Entity\Shop\CatalogPack", mappedBy="catalog")
         */
        private $packs;
     
        public function __toString()
        {
            return '' . $this->name;
        }
     
        public function __construct()
        {
            $this->packs = new ArrayCollection();
        }
     
        /**
         * Get id
         *
         * @return integer 
         */
        public function getId()
        {
            return $this->id;
        }
     
        /**
         * Set name
         *
         * @param string $name
         * @return Catalog
         */
        public function setName($name)
        {
            $this->name = $name;
     
            return $this;
        }
     
        /**
         * Get name
         *
         * @return string 
         */
        public function getName()
        {
            return $this->name;
        }
     
        /**
         * Set dateVisibility
         *
         * @param \DateTime $dateVisibility
         * @return Catalog
         */
        public function setDateVisibility($dateVisibility)
        {
            $this->dateVisibility = $dateVisibility;
     
            return $this;
        }
     
        /**
         * Get dateVisibility
         *
         * @return \DateTime 
         */
        public function getDateVisibility()
        {
            return $this->dateVisibility;
        }
     
        /**
         * @return mixed
         */
        public function getCatalogCategory()
        {
            return $this->catalogCategory;
        }
     
        /**
         * @param mixed $catalogCategory
         */
        public function setCatalogCategory($catalogCategory)
        {
            $this->catalogCategory = $catalogCategory;
        }
     
        /**
         * @return mixed
         */
        public function getPacks()
        {
            return $this->packs;
        }
     
        public function addPack(CatalogPack $pack)
        {
            $this->packs[] = $pack;
     
            $pack->setCatalog($this);
            return $this;
        }
     
        public function removePack(CatalogPack $pack)
        {
            $this->packs->removeElement($pack);
        }
     
    }
    Entity Pack
    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
    <?php
     
    namespace AppBundle\Entity\Shop;
     
    use Doctrine\DBAL\Types\DecimalType;
    use Doctrine\ORM\Mapping as ORM;
     
    /**
     * Pack
     *
     * @ORM\Table(name="pack")
     * @ORM\Entity(repositoryClass="AppBundle\Repository\PackRepository")
     */
    class Pack
    {
        /**
         * @var string
         *
         * @ORM\Column(name="name", type="string")
         */
        protected $name;
     
        /**
         * @var integer
         *
         * @ORM\Column(name="nbPost", type="integer")
         */
        protected $nbPost;
     
        /**
         * @var integer
         *
         * @ORM\Column(name="quota", type="integer")
         */
        protected $quota;
     
        /**
         * @var integer
         *
         * @ORM\Column(name="expireAt", type="integer")
         */
        protected $expireAt;
     
        /**
         * @var boolean
         *
         * @ORM\Column(name="activate", type="boolean")
         */
        protected $activate;
     
        /**
         * @ORM\oneToMany(targetEntity="AppBundle\Entity\Shop\CatalogPack", mappedBy="pack")
         * @ORM\JoinColumn(nullable=false)
         */
        private $catalog;
     
        /**
         * @return mixed
         */
        public function getCatalog()
        {
            return $this->catalog;
        }
     
        /**
         * @param mixed $catalog
         */
        public function setCatalog($catalog)
        {
            $this->catalog = $catalog;
        }
     
        public function __toString()
        {
            return '' . $this->name;
        }
     
        public function __construct()
        {
            $this->expireAt = new \DateTime();
        }
     
        /**
         * Set name
         *
         * @param string $name
         * @return Pack
         */
        public function setName($name)
        {
            $this->name = $name;
     
            return $this;
        }
     
        /**
         * Get name
         *
         * @return string
         */
        public function getName()
        {
            return $this->name;
        }
     
        /**
         * Set nbPost
         *
         * @param integer $nbPost
         * @return Pack
         */
        public function setNbPost($nbPost)
        {
            $this->nbPost = $nbPost;
     
            return $this;
        }
     
        /**
         * Get nbPost
         *
         * @return integer
         */
        public function getNbPost()
        {
            return $this->nbPost;
        }
     
        /**
         * Set quota
         *
         * @param integer $quota
         * @return Pack
         */
        public function setQuota($quota)
        {
            $this->quota = $quota;
     
            return $this;
        }
     
        /**
         * Get quota
         *
         * @return integer
         */
        public function getQuota()
        {
            return $this->quota;
        }
     
        /**
         * Set expireAt
         *
         * @param integer $expireAt
         * @return Pack
         */
        public function setExpireAt($expireAt)
        {
            $this->expireAt = $expireAt;
     
            return $this;
        }
     
        /**
         * Get expireAt
         *
         * @return integer
         */
        public function getExpireAt()
        {
            return $this->expireAt;
        }
     
        /**
         * Set activate
         *
         * @param boolean $activate
         * @return Pack
         */
        public function setActivate($activate)
        {
            $this->activate = $activate;
     
            return $this;
        }
     
        /**
         * Get activate
         *
         * @return boolean
         */
        public function getActivate()
        {
            return $this->activate;
        }
     
        /**
         * Set price
         *
         * @param DecimalType $price
         * @return Pack
         */
        public function setPrice($price)
        {
            $this->price = $price;
     
            return $this;
        }
     
        /**
         * Get price
         *
         * @return DecimalType
         */
        public function getPrice()
        {
            return $this->price;
        }
        /**
         * @var integer
         *
         * @ORM\Column(name="id", type="integer")
         * @ORM\Id
         * @ORM\GeneratedValue(strategy="AUTO")
         */
        protected $id;
     
        /**
         * Get id
         *
         * @return integer 
         */
        public function getId()
        {
            return $this->id;
        }
    }
    Entity CatalogPack

    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
    <?php
     
    namespace AppBundle\Entity\Shop;
     
    use Doctrine\ORM\Mapping as ORM;
     
    /**
     * CatalogProduct
     *
     * @ORM\Table(name="catalog_pack")
     * @ORM\Entity(repositoryClass="AppBundle\Repository\Shop\CatalogPackRepository")
     */
    class CatalogPack
    {
        /**
         * @var \DateTime
         *
         * @ORM\Column(name="expireAt", type="datetime")
         */
        private $expireAt;
     
        /**
         * @var string
         *
         * @ORM\Column(name="price", type="decimal", precision=10, scale=2)
         */
        private $price;
     
        /**
         * @ORM\Id
         * @ORM\ManyToOne(targetEntity="AppBundle\Entity\Shop\Catalog", inversedBy="packs")
         * @ORM\JoinColumn(nullable=true)
         */
        private $catalog;
     
        /**
         * @ORM\Id
         * @ORM\ManyToOne(targetEntity="AppBundle\Entity\Shop\Pack", inversedBy="catalog")
         * @ORM\JoinColumn(nullable=true)
         */
        private $pack;
     
        /**
         * Set expireAt
         *
         * @param \DateTime $expireAt
         * @return CatalogProduct
         */
        public function setExpireAt($expireAt)
        {
            $this->expireAt = $expireAt;
     
            return $this;
        }
     
        /**
         * Get expireAt
         *
         * @return \DateTime 
         */
        public function getExpireAt()
        {
            return $this->expireAt;
        }
     
        /**
         * Set price
         *
         * @param string $price
         * @return CatalogProduct
         */
        public function setPrice($price)
        {
            $this->price = $price;
     
            return $this;
        }
     
        /**
         * Get price
         *
         * @return string 
         */
        public function getPrice()
        {
            return $this->price;
        }
     
        /**
         * @return mixed
         */
        public function getCatalog()
        {
            return $this->catalog;
        }
     
        /**
         * @param mixed $catalog
         */
        public function setCatalog($catalog)
        {
            $this->catalog = $catalog;
        }
     
        /**
         * @return mixed
         */
        public function getPack()
        {
            return $this->pack;
        }
     
        /**
         * @param mixed $pack
         */
        public function setPack($pack)
        {
            $this->pack = $pack;
        }
     
     
    }
    Voici le MCD pour ces trois tables :
    Nom : mcd.PNG
Affichages : 407
Taille : 18,5 Ko


    J'utilise le Bundle Sonata pour gérer le CRUD.

    Voici la classe Admin pour le Catalogue

    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
    <?php
     
    namespace AppBundle\Admin;
     
    use AppBundle\Entity\Shop\Catalog;
    use AppBundle\Entity\Shop\CatalogCategory;
    use AppBundle\Entity\Shop\Pack;
    use Sonata\AdminBundle\Admin\Admin;
    use Sonata\AdminBundle\Datagrid\DatagridMapper;
    use Sonata\AdminBundle\Show\ShowMapper;
    use Sonata\AdminBundle\Form\FormMapper;
    use Sonata\AdminBundle\Datagrid\ListMapper;
    use Sonata\AdminBundle\Route\RouteCollection;
    use Sonata\DoctrineORMAdminBundle\Datagrid\ProxyQuery;
     
    use AppBundle\Traits\AdminTrait;
     
    class CatalogAdmin extends Admin
    {
        use AdminTrait;
     
        protected $baseRouteName        = 'app_catalog';
        protected $baseRoutePattern     = 'catalog';
     
        /**
         * @param ListMapper $listMapper
         */
        protected function configureListFields(ListMapper $listMapper)
        {
            $listMapper
                ->addIdentifier('name', 'string', array(
                    'label' => 'Nom du catalogue'
                ))
                ->add('dateVisibility', 'datetime', array(
                    'label' => 'Date de visibilité du catalogue'
                ))
            ;
        }
     
        /**
         * Modele pour le formulaire de création et d'édition
         *
         * @param FormMapper $formMapper
         */
        protected function configureFormFields(FormMapper $formMapper)
        {
            $box_class = $this->getContainer()->getParameter('box_class');
            $em        = $this->getEntityManager();
     
            $listCategory = $em->getRepository('AppBundle:Shop\CatalogCategory')->getCategory()
                                        ->getQuery();
            $listPack     = $em->getRepository('AppBundle:Shop\Pack')->getPack()
                                        ->getQuery();
     
            $formMapper
                ->tab('Catalogue')
                ->with('Général', array('class' => 'col-md-12', 'box_class' => $box_class))->end()
                ->with('Produits', array('class' => 'col-md-12', 'box_class' => $box_class))->end()
                ->end()
            ;
     
            $formMapper
                ->tab('Catalogue')
                ->with('Général')
                ->add('name', null ,array(
                    'label' => 'Nom du catalogue'
                ))
                ->add('catalogCategory', 'sonata_type_model', array(
                    'label' => 'Catégorie',
                    'query' => $listCategory
                ))
                ->add('dateVisibility', 'date', array(
                    'format' => 'dMy',
                    'label' => 'Date de visibilité du catalogue'
                ))
                ->end()
                ->with('Produits')
                ->add('packs', 'sonata_type_model', array(
                    'label' => 'Produit',
                    'query' => $listPack
                ))
                ->end()
            ;
        }
     
     
     
        public function configureRoutes(RouteCollection $collection)
        {
            // Option : batch create delete export edit list show
            parent::configureRoutes($collection);
            $collection->remove('delete');
        }
     
        public function getExportFormats()
        {
            return array('xls');
        }
    }
    Je souhaite à partir de CatalogAdmin récupérer la liste complète des packs
    afin de les proposer pour les ajouter ou non au catalogue.

    Grâce à :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
            $listPack     = $em->getRepository('AppBundle:Shop\Pack')->getPack()
                                        ->getQuery();
    Je récupère bien la liste des packs mais j'ai une erreur lorsque j'essai de l'ajouter au formMapper ->

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
                ->add('packs', 'sonata_type_model', array(
                    'label' => 'Produit',
                    'query' => $listPack
                ))
    L'erreur retournée :
    No entity manager defined for class Doctrine\Common\Collections\ArrayCollection
    500 Internal Server Error - RuntimeException
    Auriez vous une idée du problème ?

    Sachant que j'ai fait quelques recherche bien sur avant de poster ce sujet.

    J'ai vu qu'il était possible d'ajouter une option "Multiple" à true comme ceci :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
                ->add('packs', 'sonata_type_model', array(
                    'label' => 'Produit',
                    'multiple' => true,
                    'query' => $listPack
                ))
    je n'ai plus d'erreur mais ma liste n'est pas affichée ...

    Merci d'avance pour votre aide !

    Ju

  2. #2
    Membre à l'essai
    Homme Profil pro
    Étudiant
    Inscrit en
    Novembre 2011
    Messages
    26
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Novembre 2011
    Messages : 26
    Points : 14
    Points
    14
    Par défaut
    Enjoy !!
    J'ai réussi ce que je voulais faire !

    Je posterai ce soir, les différentes modifications que j'ai apporté, cela pourra surement aider quelques personnes.

    Une bonne journée all.

Discussions similaires

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

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