Question

I have this three tables in my model:

enter image description here

And I need to create the relationship between them in order to get valid entities but I need some help here. This is what I have right now:

Proyectos.php

<?php

namespace PI\ProyectoBundle\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * @ORM\Entity
 * @ORM\Table(name="proyectos")
 */
class Proyectos {

    /**
     * @ORM\Id
     * @ORM\Column(type="integer")
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $id;

    /**
     * @ORM\Column(type="string", length=45, unique=true, nullable=false)
     */
    protected $nombre;

    /**
     * @ORM\Column(type="boolean", nullable=true)
     */
    protected $estado;

    /**
     * @ORM\Column(type="string", length=45, nullable=false)
     */
    protected $pais;

    /**
     * @ORM\ManyToOne(targetEntity="PI\ClienteBundle\Entity\Clientes", cascade={"all"})
     * @ORM\JoinColumn(name="cliente", referencedColumnName="id")
     */
    protected $clientes;

    public function getId() {
        return $this->id;
    }

    public function setNombre($nombre) {
        $this->nombre = $nombre;
    }

    public function getNombre() {
        return $this->nombre;
    }

    public function setEstado($estado) {
        $this->estado = $estado;
    }

    public function getEstado() {
        return $this->estado;
    }

    public function setPais($pais) {
        $this->pais = $pais;
    }

    public function getPais() {
        return $this->pais;
    }

    public function setClientes(\PI\ClienteBundle\Entity\Clientes $clientes) {
        $this->clientes = $clientes;
    }

    public function getClientes() {
        return $this->clientes;
    }

}

And Centros.php

<?php

namespace PI\CentroBundle\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * @ORM\Entity
 * @ORM\Table(name="centros")
 */
class Centros {

    /**
     * @ORM\Id
     * @ORM\Column(type="integer")
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $id;

    /**
     * @ORM\Column(type="text", unique=true, nullable=false)
     */
    protected $descripcion;

    /**
     * @ORM\ManyToMany(targetEntity="PI\UnidadBundle\Entity\Unidades", inversedBy="centros", cascade={"persist"})
     * @ORM\JoinTable(name="unidades_has_centros",
     *      joinColumns={@ORM\JoinColumn(name="centros_id", referencedColumnName="id")},
     *      inverseJoinColumns={@ORM\JoinColumn(name="unidades_id", referencedColumnName="id")}
     *      )
     */
    protected $unidades;

    public function __construct() {
        $this->unidades = new \Doctrine\Common\Collections\ArrayCollection();
    }

    public function getId() {
        return $this->id;
    }

    public function setDescripcion($descripcion) {
        $this->descripcion = $descripcion;
    }

    public function getDescripcion() {
        return $this->descripcion;
    }

    public function setUnidades(\PI\UnidadBundle\Entity\Unidades $unidades) {
        $this->unidades[] = $unidades;
    }

    public function getUnidades() {
        return $this->unidades;
    }

}

How I add the n:m relationship in both sides to get valid entities and to make CRUD operations more easy than create a new Entity ProyectosHasCentros.php and put fields inside it?

Was it helpful?

Solution

If you don't add proyectos_cliente to the second table you can use the ManyToMany relationships in your entities. If you want extra fields, then you have to create an extra entity which contains the relationships with Proyectos and Centros. Beside the relationships you add extra properties, like proyectos_cliente.

Try to use one PK and use UNIQUE for the cliente field

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top