2017-02-13 51 views
2

學說ORM上的註釋的「間接相關」實體作爲成員考慮以下Symfony的實體:添加使用Symfony2的

class Continent 
{ 
/** 
* @ORM\Id 
* @ORM\Column(type="integer", name="id") 
* @ORM\GeneratedValue(strategy="IDENTITY") 
*/ 
private $id; 

/** 
* @ORM\Column(type="string", length=20, nullable=true, name="text") 
*/ 
private $text; 

/** 
* @ORM\OneToMany(targetEntity="AppBundle\Entity\Country", mappedBy="continent") 
*/ 
private $countries; 
/** 
* Constructor 
*/ 
public function __construct() 
{ 
    $this->countries= new \Doctrine\Common\Collections\ArrayCollection(); 
} 


class Country 
{ 
/** 
* @ORM\Id 
* @ORM\Column(type="integer", name="id") 
* @ORM\GeneratedValue(strategy="IDENTITY") 
*/ 
private $id; 

/** 
* @ORM\Column(type="string", length=20, nullable=true, name="text") 
*/ 
private $text; 

/** 
* @ORM\OneToMany(targetEntity="AppBundle\Entity\City", mappedBy="country") 
*/ 
private $cities; 

/** 
* @ORM\ManyToOne(targetEntity="AppBundle\Entity\Continent", inversedBy="country") 
* @ORM\JoinColumn(name="continentt_id", referencedColumnName="id") 
*/ 
private $continent; 
/** 
* Constructor 
*/ 
public function __construct() 
{ 
    $this->cities= new \Doctrine\Common\Collections\ArrayCollection(); 
} 

class City 
{ 
/** 
* @ORM\Id 
* @ORM\Column(type="integer", name="id") 
* @ORM\GeneratedValue(strategy="IDENTITY") 
*/ 
private $id; 

/** 
* @ORM\Column(type="string", length=30, nullable=true, name="text") 
*/ 
private $text; 

/** 
* @ORM\ManyToOne(targetEntity="AppBundle\Entity\Country", inversedBy="city") 
* @ORM\JoinColumn(name="country_id", referencedColumnName="id") 
*/ 
private $country; 
/** 
* Constructor 
*/ 
public function __construct() 
{ 

} 

我的問題是:

有沒有一種方法,使用註釋,將$大陸成員添加到城市表示後退/間接關係的實體類(即該城市的國家/地區的大陸)

如果使用註釋是不可能的,這將是解決這一好的做法(例如自定義庫?)

回答

3

我不知道任何學說標準批註做到這一點。

如果你的目的只是爲了獲得有關國家的大陸爲什麼你不只是做:

public function getContinent() 
{ 
    return $this->country->getContinent(); 
}