symfony2关系未在子类中正确映射

时间:2013-03-21 13:19:50

标签: inheritance symfony doctrine foreign-keys entity-relationship

这是我的问题:

我有一个类物种女巫是抽象的,我有另一个类“Raie”,它扩展了物种。 物种和种类之间我有一个关系ManyToOne,因为每个物种都有一种类型。

当我使用doctrine来生成数据库时,它会生成一个表格物种(因为它是抽象的而很奇怪)和一个表Raie。

为了避免生成物种表,我应该从实体定义中删除@ORM\Table()吗?它应该吗?

主要问题是表Raie在物种 exept 中定义的每个属性与typeSpecies实体的关系!

以下是我的两个课程的定义,以帮助您理解:

<?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;
    }
}

感谢您的帮助

1 个答案:

答案 0 :(得分:0)

您可能需要更改抽象类的继承类型。阅读Doctrine documentation

中有关Doctrine 2和继承映射的更多信息