Adding an “indirectly associated” entity as a memb

2019-07-17 18:17发布

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条回答
贼婆χ
2楼-- · 2019-07-17 18:43

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();
}
查看更多
登录 后发表回答