中阅读更多关于Doctrine 2和继承映射的信息
我的问题是:
我有一个抽象的类"物种",我有另一个扩展物种的类"雷"。我在物种和类型之间有一个多对一的关系,因为每个物种都有一个类型。
当我使用doctrine生成数据库时,它生成了一个表species(很奇怪,因为它是抽象的)和一个表Raie。
为了避免生成物种表,我是否应该从实体定义中删除@ORMTable()
?这够吗?
主要问题是表Raie具有在species 中定义的所有属性,除了与typeSpecies实体的关系!
下面是我的两个类的定义来帮助你理解:
<?php
namespace AileronsBackendBundleEntity;
use DoctrineORMMapping as ORM;
use DoctrineCommonCollectionsArrayCollection;
/**
* Species
*
* @ORMTable()
* @ORMEntity
*/
abstract class Species
{
/**
* @var integer
*
* @ORMColumn(name="id", type="integer")
* @ORMId
* @ORMGeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @var Type
*
* @ORMManyToOne(targetEntity="TypeSpecies", inversedBy="species")
*/
private $type;
/**
* @var string
*
* @ORMColumn(name="remarque", type="text")
*/
private $remarque;
/**
* @var boolean
*
* @ORMColumn(name="sexe", type="boolean")
*/
private $sexe;
/**
* @var float
*
* @ORMColumn(name="length", type="float")
*/
private $length;
/**
* @var Observation
*
* @ORMManyToMany(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 AileronsBackendBundleEntityObservation $observations
* @return Species
*/
public function addObservation(AileronsBackendBundleEntityObservation $observations)
{
$this->observations[] = $observations;
return $this;
}
/**
* Remove observations
*
* @param AileronsBackendBundleEntityObservation $observations
*/
public function removeObservation(AileronsBackendBundleEntityObservation $observations)
{
$this->observations->removeElement($observations);
}
/**
* Get observations
*
* @return DoctrineCommonCollectionsCollection
*/
public function getObservations()
{
return $this->observations;
}
/**
* Set type
*
* @param AileronsBackendBundleEntityTypeSpecies $type
* @return Species
*/
public function setType(AileronsBackendBundleEntityTypeSpecies $type = null)
{
$this->type = $type;
return $this;
}
/**
* Get type
*
* @return AileronsBackendBundleEntityTypeSpecies
*/
public function getType()
{
return $this->type;
}
}
Raie类
<?php
namespace AileronsBackendBundleEntity;
use DoctrineORMMapping as ORM;
/**
* Raie
*
* @ORMTable()
* @ORMEntity
*/
class Raie extends Species
{
/**
* @var integer
*
* @ORMColumn(name="id", type="integer")
* @ORMId
* @ORMGeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @var float
*
* @ORMColumn(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;
}
}
谢谢你的帮助
您可能需要更改抽象类的继承类型。在Doctrine文档