dongxili9934 2017-02-13 12:50
浏览 72
已采纳

使用Symfony2中的Doctrine ORM注释将“间接关联”实体添加为成员

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()
{

}

My question is:

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?)

  • 写回答

1条回答 默认 最新

  • duanbai1370 2017-02-13 13:21
    关注

    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();
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 网络设备配置与管理这个该怎么弄
  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器