按自定义列

时间:2018-02-28 22:21:03

标签: php mysql symfony doctrine

我在OneToMany关系中映射了2个实体。

  

国家实体

class Countries
{

    public function __construct() {
        $this->areas = new ArrayCollection();
    }

    /**
     * @ORM\Column(type="string", length=100, nullable=false, unique=true)
     */
    private $country

    /**
     * @ORM\OneToMany(targetEntity="App\Entity\Areas", mappedBy="country")
     */
    private $areas;

    /**
     * @return Collection\Area[]
     */
    public function getAreas() {
        return $this->areas;
    }

}
  

区域实体

class Areas
{
    /**
     * @ORM\Column(type="string", length=100, nullable=false, unique=false)
     */
    private $area;

    /**
     * @ORM\ManyToOne(targetEntity="App\Entity\Countries", inversedBy="area")
     * @ORM\JoinColumn(nullable=true)
     */
    private $country;

    public function getCountry() : Countries {
        return $this->country;
    }

    public function setCountry(Countries $countries) {
        $this->country = $countries;
    }

}

在我的MySQL中,我看到它们是按国家/地区ID映射的,因此在db 1中,记录将显示为

  

1 2 Ile-De-France

我想插入国家/地区名称

  

1法国Ile-De-France

在哪里可以设置我想在映射中使用的列? 我知道我总是可以做$ country-> getName($ id),但我更喜欢在DB中有清晰的视图。 感谢所有回复。

1 个答案:

答案 0 :(得分:0)

这就是它的完成方式,但无论如何你应该保持id ...
否则,当您在控制台中检查关联映射时,它将显示“错误”

区域实体

/**
 * @ORM\ManyToOne(targetEntity="App\Entity\Countries", inversedBy="area")
 * @ORM\JoinColumn(name="country", referencedColumnName="country", nullable=true)
 */
private $country;