Search code examples
inheritancesymfonydoctrineforeign-keysentity-relationship

symfony2 relation not mapped correctly in child class


Here is my problem:

I have a class species witch is abstract and I have another class "Raie" that extends species. I have a relation ManyToOne between species and typeSpecies, because every species has a type.

When I use doctrine to generate the database it generate a table species (wierd as it is abstract) and a table Raie.

In order to avoid the generation of species table should I remove the @ORM\Table() from the entity definition ? is It enought ?

The main issue is that the table Raie has every attributes defined in species exept the relation to typeSpecies entity !

Here are the definition of my two classes to help you understand:

<?php

namespace Ailerons\BackendBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection;

/**
 * Species
 *
 * @ORM\Table()
 * @ORM\Entity
 */
abstract class Species
{
    /**
    * @var integer
    *
    * @ORM\Column(name="id", type="integer")
    * @ORM\Id
    * @ORM\GeneratedValue(strategy="AUTO")
    */
    private $id;

    /**
    * @var Type
    * 
    * @ORM\ManyToOne(targetEntity="TypeSpecies", inversedBy="species")
    */
    private $type;

    /**
    * @var string
    *
    * @ORM\Column(name="remarque", type="text")
    */
    private $remarque;


    /**
    * @var boolean
    *
    * @ORM\Column(name="sexe", type="boolean")
    */
    private $sexe;

    /**
    * @var float
    *
    * @ORM\Column(name="length", type="float")
    */
    private $length;


    /**
    * @var Observation
    * 
    * @ORM\ManyToMany(targetEntity="Observation", inversedBy="species")
    */
    private $observations;

    /**
    * Constructor
    */
    public function __construct()
    {
       $this->observations = new ArrayCollection();
    }

    /**
    * Get id
    *
    * @return integer 
    */
    public function getId()
    {
       return $this->id;
    }

    /**
    * Set remarque
    *
    * @param string $remarque
    * @return Species
    */
    public function setRemarque($remarque)
    {
       $this->remarque = $remarque;

       return $this;
    }

    /**
    * Get remarque
    *
    * @return string 
    */
    public function getRemarque()
    {
       return $this->remarque;
    }

    /**
    * Set sexe
    *
    * @param boolean $sexe
    * @return Species
    */
    public function setSexe($sexe)
    {
       $this->sexe = $sexe;

       return $this;
    }

    /**
    * Get sexe
    *
    * @return boolean 
    */
    public function getSexe()
    {
       return $this->sexe;
    }

    /**
    * Set length
    *
    * @param float $length
    * @return Species
    */
    public function setLength($length)
    {
       $this->length = $length;

       return $this;
    }

    /**
    * Get length
    *
    * @return float 
    */
    public function getLength()
    {
       return $this->length;
    }

    /**
    * Add observations
    *
    * @param \Ailerons\BackendBundle\Entity\Observation $observations
    * @return Species
    */
    public function addObservation(\Ailerons\BackendBundle\Entity\Observation $observations)
    {
       $this->observations[] = $observations;

       return $this;
    }

    /**
    * Remove observations
    *
    * @param \Ailerons\BackendBundle\Entity\Observation $observations
    */
    public function removeObservation(\Ailerons\BackendBundle\Entity\Observation $observations)
    {
       $this->observations->removeElement($observations);
    }

    /**
    * Get observations
    *
    * @return \Doctrine\Common\Collections\Collection 
    */
    public function getObservations()
    {
       return $this->observations;
    }

    /**
    * Set type
    *
    * @param \Ailerons\BackendBundle\Entity\TypeSpecies $type
    * @return Species
    */
    public function setType(\Ailerons\BackendBundle\Entity\TypeSpecies $type = null)
    {
       $this->type = $type;

       return $this;
    }

    /**
    * Get type
    *
    * @return \Ailerons\BackendBundle\Entity\TypeSpecies 
    */
    public function getType()
    {
       return $this->type;
    }
}

Raie class

    <?php

namespace Ailerons\BackendBundle\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * Raie
 *
 * @ORM\Table()
 * @ORM\Entity
 */
class Raie extends Species
{
    /**
    * @var integer
    *
    * @ORM\Column(name="id", type="integer")
    * @ORM\Id
    * @ORM\GeneratedValue(strategy="AUTO")
    */
    private $id;

    /**
    * @var float
    *
    * @ORM\Column(name="wingspan", type="float")
    */
    private $wingspan;


    /**
    * Get id
    *
    * @return integer 
    */
    public function getId()
    {
       return $this->id;
    }

    /**
    * Set wingspan
    *
    * @param float $wingspan
    * @return Raie
    */
    public function setWingspan($wingspan)
    {
       $this->wingspan = $wingspan;

       return $this;
    }

    /**
    * Get wingspan
    *
    * @return float 
    */
    public function getWingspan()
    {
       return $this->wingspan;
    }
}

Thank you for your help


Solution

  • You may need to change the inheritance type of the abstract class. Read more on Doctrine 2 and Inheritance mapping in the Doctrine documentation