1

這裏是我的問題:Symfony2的關係不是在子類中正確映射

我有一個類物種女巫是抽象的,我有一個擴展的物種另一個階級「RAIE」。 我與物種之間有一種關係ManyToOne物種,因爲每個物種都有一種類型。

當我使用原則來生成數據庫時,它會生成一個表格種類(因爲它是抽象的)和表格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類

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

謝謝您的幫助

回答

0

您可能需要更改繼承類型的抽象類。閱讀更多關於Doctrine 2和繼承映射的Doctrine documentation

+0

我明白了!學說需要更多的信息,我會試驗閱讀文檔。 – 0x1gene 2013-03-21 13:44:24

+0

查看有關繼承映射在教條中的文檔幫助我!謝謝 – 0x1gene 2013-03-21 13:56:32