Relation between 3 entities

4

I have the following tables:

+----------+  +----------------------+ +------------+
| Produtos |  | ProdutosVsCategorias | | Categorias |
+----------+  +----------------------+ +------------+
- ID          - ID                     - ID
- TITULO      - ID_PRODUTO             - TITULO
              - ID_CATEGORIA
  • The Produtos table contains all my products
  • The ProdutosVsCategorias table has a relation to the Produtos and Categorias table.
  • The relationship between the Produtos and ProdutosVsCategorias tables occurs through the ID and ID_PRODUTO fields respectively and is OneToMany.
  • The relationship between the ProdutosVsCategorias and Categorias tables occurs through the ID_CATEGORIA and ID fields respectively and is ManyToOne.
  • For each table, I created an entity, but I do not know how to do the relationship between entities in Symfony using Doctrine.

    When instantiating the Produto entity, I need to know which categories are associated with the product.

    When instantiating a category, I also need to know which products are associated with the category.

    How to make the relationship between entities using the above example?

        
    asked by anonymous 28.08.2015 / 13:03

    1 answer

    0

    Depends on the case. You can create a many-to-many relationship between the Produto and Categoria entities, or two many-to-one relationships, and Produto , and another between ProdutoVsCategoria and ProdutoVsCategoria . The difference is that when you make the relationship using two many-to-one relationships, you can customize the intermediary entity (more attributes, for example).

    This explanation is found in the Doctrine documentation:

      

    Why are many-to-many associations less common? Because you frequently want to associate additional attributes with an association, in which case you introduce an association class. Consequently, the direct many-to-many association disappears and is replaced by one-to-many / many-to-one associations among the 3 participating classes.

         

    link

    But I'll explain how to do it using two entities.

    First, create the classes for the Categoria and Categorias tables.

    Class Produtos :

    <?php
    
    namespace AppBundle\Entity;
    
    use Doctrine\ORM\Mapping as ORM;
    
    /**
     * @ORM\Entity;
     * @ORM\Table(name="Produtos")
     */
    class Produto
    {
        /**
         * @ORM\Column(type="integer")
         * @ORM\Id
         * @ORM\GeneratedValue(strategy="AUTO")
         */
        private $id;
    
        /**
         * @ORM\ManyToMany(targetEntity="Categoria", mappedBy="produtos")
         */
        private $categorias;
    
        /**
         * @ORM\Column(type="string")
         */
        private $titulo;
    }
    

    Class Produto :

    <?php
    
    namespace AppBundle\Entity;
    
    use Doctrine\ORM\Mapping as ORM;
    
    /**
     * @ORM\Entity;
     * @ORM\Table(name="Categorias")
     */
    class Categoria
    {
        /**
         * @ORM\Column(type="integer")
         * @ORM\Id
         * @ORM\GeneratedValue(strategy="AUTO")
         */
        private $id;
    
        /**
         * @ORM\ManyToMany(targetEntity="Produto", inversedBy="categorias")
         * @ORM\JoinTable(name="ProdutosVsCategorias")
         */
        private $produtos;
    
        /**
         * @ORM\Column(type="string")
         */
        private $titulo;
    }
    

    (do not forget to match the namespaces to your project's namespaces )

    After that, just use the Categoria command to create the table schema in the database configured in your php app/console doctrine:schema:create file.

        
    30.08.2015 / 16:15