如何将两个实体与 ManyToOne 相关联?
How can I relate two entities with ManyToOne?
在我的 pages
实体中,我通过 "ManyToOne" 添加了图标:
/**
* @ORM\ManyToOne(targetEntity="App\Entity\Icons", inversedBy="pages")
* @ORM\JoinColumn(nullable=false)
*/
private $icon;
public function getIcon(): ?Icons
{
return $this->icon;
}
public function setIcon(?Icons $icon): self
{
$this->icon = $icon;
return $this;
}
但是我对此有疑问,我收到一条错误消息:
An exception occurred while executing 'SELECT t0.id AS id_1,
t0.unique_id AS unique_id_2, t0.name AS name_3, t0.template AS
template_4, t0.slug AS slug_5, t0.icon_id AS icon_id_6 FROM pages t0
WHERE t0.slug = ? LIMIT 1' with params ["pages"]:
SQLSTATE[42S22]: Column not found: 1054 Unknown column 't0.icon_id' in
'field list'
Doctrine 将在 relation_many
table 上搜索名为 [relationed_one]_id
的列。
如果图标和页面的关系是由 icon_id 在页面 table 上的另一个外键建立的,您可以用 @JoinColumn(name="another_foreign_id", referencedColumnName="another_primary_id")
表示
在我的 pages
实体中,我通过 "ManyToOne" 添加了图标:
/**
* @ORM\ManyToOne(targetEntity="App\Entity\Icons", inversedBy="pages")
* @ORM\JoinColumn(nullable=false)
*/
private $icon;
public function getIcon(): ?Icons
{
return $this->icon;
}
public function setIcon(?Icons $icon): self
{
$this->icon = $icon;
return $this;
}
但是我对此有疑问,我收到一条错误消息:
An exception occurred while executing 'SELECT t0.id AS id_1, t0.unique_id AS unique_id_2, t0.name AS name_3, t0.template AS template_4, t0.slug AS slug_5, t0.icon_id AS icon_id_6 FROM pages t0 WHERE t0.slug = ? LIMIT 1' with params ["pages"]:
SQLSTATE[42S22]: Column not found: 1054 Unknown column 't0.icon_id' in 'field list'
Doctrine 将在 relation_many
table 上搜索名为 [relationed_one]_id
的列。
如果图标和页面的关系是由 icon_id 在页面 table 上的另一个外键建立的,您可以用 @JoinColumn(name="another_foreign_id", referencedColumnName="another_primary_id")