【问题标题】:How can I relate two entities with ManyToOne?如何将两个实体与 ManyToOne 关联?
【发布时间】:2018-12-06 17:51:07
【问题描述】:

在我的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 FROM pages t0 在哪里 t0.slug = ? LIMIT 1' with params ["pages"]:

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

【问题讨论】:

  • 向实体添加新列后是否更新了数据库架构?

标签: symfony doctrine entity many-to-one


【解决方案1】:

Doctrine 将在 relation_many 表上搜索名为 [relationed_one]_id 的列。 如果 Icon 和 Page 的关系是由 pages 表上 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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-04-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-12-02
    • 1970-01-01
    相关资源
    最近更新 更多