1

这是我的问题:

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

当我使用教义生成数据库时,它会生成一个表种类(因为它是抽象的而奇怪)和一个表 Raie。

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

主要问题是 Raie 表具有在种类中定义的所有属性,除了与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;
    }
}

雷类

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

感谢您的帮助

4

1 回答 1

0

您可能需要更改抽象类的继承类型。在Doctrine 文档中阅读有关 Doctrine 2 和继承映射的更多信息

于 2013-03-21T13:41:26.817 回答