如何将两个实体与ManyToOne相关联?

时间:2018-12-06 17:51:07

标签: symfony doctrine entity many-to-one

在我的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;
}

但是我对此有疑问,我收到一条错误消息:

  

执行“ 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来自页面t0   在哪里t0.slug =?带有参数[[页面]]的LIMIT 1':

     

SQLSTATE [42S22]:找不到列:1054中的未知列't0.icon_id'   “字段列表”

1 个答案:

答案 0 :(得分:1)

Doctrine将在[relationed_one]_id表上搜索名为relation_many的列。 如果Icon和Page的关系是由页面表上的icon_id的另一个外键建立的,则可以用@JoinColumn(name="another_foreign_id", referencedColumnName="another_primary_id")

表示

https://www.doctrine-project.org/projects/doctrine-orm/en/2.6/reference/association-mapping.html#one-to-many-bidirectional