添加“间接相关”的内容。实体作为成员使用Symfony2中的Doctrine ORM注释 [英] Adding an "indirectly associated" entity as a member using Doctrine ORM annotations in Symfony2
问题描述
考虑以下Symfony实体:
Considering the following Symfony entities:
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()
{
}
我的问题是:
有没有一种方法,使用注释,向 City 实体类添加 $ continent 成员,该成员表示后向/间接关系(即城市所在国家/地区)
Is there a way, using annotation, to add a $continent member to the City Entity class that represents a step-behind/indirect relationship (i.e the continent of the country of the city)
如果无法使用注释,什么是解决此问题的好方法(例如自定义存储库?)
If it is not possible using annotations, what would be a good practice to solve this (custom repository for example?)
推荐答案
我不知道要这样做的任何教义标准注释。
I don't know any doctrine standard annotation to do this.
如果您的目的只是为了获得与该国家有关的大洲您为什么不简单做一下:
If your purpose is just to get the continent related to the country why don't you simply do :
public function getContinent()
{
return $this->country->getContinent();
}
这篇关于添加“间接相关”的内容。实体作为成员使用Symfony2中的Doctrine ORM注释的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!