【问题标题】:Doctrine foreign key constraint教义外键约束
【发布时间】:2019-05-08 14:51:01
【问题描述】:

我有 2 张 XXXX 和 YYYY 两张表。在第一个中,我有一个指向第二个的外键。

create table XXXXXX
(
    id             int   not null,
    yyyyy_id       int   not null,
)

create table YYYYY
(
    id             int   not null,
)

所以在我的 Doctrine Entity 中,我想用 Doctrine Annotation 来表示这种关系。但是,所有关联(OneToMany、ManyToOne、...)都需要 targetEntity 参数。

是否可以有一个像$yyyyId 这样的自定义字段并对其进行映射?

可能是这样的:

    /**
     * @ORM\Column(name="yyyyy_id", type="integer")
     * @ORM\@JoinTable(
     *      name="YYYYY",
     *      joinColumns={@ORM\JoinColumn(name="yyyyy_id", referencedColumnName="id", fieldName="yyyyyId")}
     * )
     */
    private $yyyyyId;

并且有一个等价于:

alter table XXXXX
    add constraint fk foreign key (yyyyy_id) references YYYYY (id) on delete cascade;

【问题讨论】:

标签: symfony doctrine-orm doctrine


【解决方案1】:

你可以试试这个吗?

/**
* @ORM/ManyToOne(targetEntity="App\Entity\")
*/
private $yyyyyId;

【讨论】:

  • 你能进一步解释一下吗?为什么要试一试?是什么让您认为这可以解决问题?请记住,其他人应该能够从您的回答中学习
【解决方案2】:

如果您需要使用 Doctrine 从联接表中检索数据,则需要为此表创建实体。 Documentation.

否则,您需要手动添加此关系并使用Native SQL 进行查询

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-09-23
  • 2014-01-17
  • 2017-04-20
  • 2018-03-05
  • 2013-01-30
  • 2014-01-16
相关资源
最近更新 更多