我在 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 法兰西岛
我想插入国家名称
1 法国 法兰西岛
在哪里可以设置要在映射中使用的列?我知道我总是可以做 $country->getName($id),但我更喜欢在 DB 中有清晰的视图。感谢所有回复。
这是它的完成方式,但id
无论如何你应该保留......
否则当你在控制台中检查关联映射时,它会显示“错误”
Area Entity
/**
* @ORM\ManyToOne(targetEntity="App\Entity\Countries", inversedBy="area")
* @ORM\JoinColumn(name="country", referencedColumnName="country", nullable=true)
*/
private $country;
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句