Relationship between 3 entities

Asked

Viewed 460 times

4

I have the following tables:

+----------+  +----------------------+ +------------+
| Produtos |  | ProdutosVsCategorias | | Categorias |
+----------+  +----------------------+ +------------+
- ID          - ID                     - ID
- TITULO      - ID_PRODUTO             - TITULO
              - ID_CATEGORIA
  1. The table Produtos contains all my products
  2. The table ProdutosVsCategorias has a relationship with the table Produtos and Categorias.
  3. The relationship between the tables Produtos and ProdutosVsCategorias occurs through the fields ID and ID_PRODUTO respectively and is Onetomany.
  4. The relationship between the tables ProdutosVsCategorias and Categorias occurs through the fields ID_CATEGORIA and ID respectively and is Manytoone.

For each table, I created an entity, but I don’t know how to make the relationship between the entities in Symfony using Doctrine.

By instantiating the entity Produto, 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?

1 answer

0


It depends on the case. You can create a relationship Many-to-Many between entities Produto and Categoria, or two relationships Many-to-one, being one among Produto and ProdutoVsCategoria, and another between ProdutoVsCategoria and Categoria. The difference is that when you make the relationship using two relationships Many-to-one, it is possible to customize the intermediate entity (more attributes, for example).

This explanation is in the documentation of Doctrine:

Why are Many-to-Many Associations Less common? Because Frequently you 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 between the 3 Participating classes.

http://doctrine-orm.readthedocs.org/en/latest/reference/association-mapping.html

But I will explain how to do it using two entities.

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

Class Produto:

<?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 Categoria:

<?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 adapt the namespaces to namespaces of your project.)

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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.