原则 2 - 不允许 ManyToOne 关系的外键为空值

发布于 2024-11-09 23:40:26 字数 1234 浏览 2 评论 0原文

我的一个实体中有一个 ManyToOne 关系,如下所示:

class License {
    // ...
    /**
     * Customer who owns the license
     * 
     * @var \ISE\LicenseManagerBundle\Entity\Customer
     * @ORM\ManyToOne(targetEntity="Customer", inversedBy="licenses")
     * @ORM\JoinColumn(name="customer_id", referencedColumnName="id")
     */
    private $customer;
    // ...
}

class Customer {
    // ...
    /**
     * Licenses that were at one point generated for the customer
     * 
     * @var \Doctrine\Common\Collections\ArrayCollection
     * @ORM\OneToMany(targetEntity="License", mappedBy="customer")
     */
    private $licenses;
    // ...
}

这会生成一个数据库模式,其中许可证表的“customer_id”字段允许为空,这正是我不想要的。

下面是一些代码,我在其中创建了一条记录,以证明它确实允许引用字段使用空值:

$em = $this->get('doctrine')->getEntityManager();
$license = new License();
// Set some fields - not the reference fields though
$license->setValidUntil(new \DateTime("2012-12-31"));
$license->setCreatedAt(new \DateTime());
// Persist the object
$em->persist($license);
$em->flush();

基本上,我不希望在没有为其分配客户的情况下保留许可证。是否需要设置一些注释,或者我应该只需要将 Customer 对象传递给我的许可证的构造函数?

我使用的数据库引擎是 MySQL v5.1,我在 Symfony2 应用程序中使用 Doctrine 2。

I have a ManyToOne relationship in one of my entities, like so:

class License {
    // ...
    /**
     * Customer who owns the license
     * 
     * @var \ISE\LicenseManagerBundle\Entity\Customer
     * @ORM\ManyToOne(targetEntity="Customer", inversedBy="licenses")
     * @ORM\JoinColumn(name="customer_id", referencedColumnName="id")
     */
    private $customer;
    // ...
}

class Customer {
    // ...
    /**
     * Licenses that were at one point generated for the customer
     * 
     * @var \Doctrine\Common\Collections\ArrayCollection
     * @ORM\OneToMany(targetEntity="License", mappedBy="customer")
     */
    private $licenses;
    // ...
}

This generates a database schema where the "customer_id" field of the license table is allowed to be null, which is exactly what I do not want.

Here's some code where I create a record to prove that it indeed allows null values for the reference fields:

$em = $this->get('doctrine')->getEntityManager();
$license = new License();
// Set some fields - not the reference fields though
$license->setValidUntil(new \DateTime("2012-12-31"));
$license->setCreatedAt(new \DateTime());
// Persist the object
$em->persist($license);
$em->flush();

Basically, I don't want a License to be persisted without having a Customer assigned to it. Is there some annotation that needs to be set or should I just require a Customer object to be passed to my License's constructor?

The database engine I use is MySQL v5.1, and I am using Doctrine 2 in a Symfony2 application.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

淡写薰衣草的香 2024-11-16 23:40:26

https://www.doctrine -project.org/projects/doctrine-orm/en/2.6/reference/annotations-reference.html#annref_joincolumn

添加 nullable = falseJoinColumn 注释:

@ORM\JoinColumn(..., nullable=false)

https://www.doctrine-project.org/projects/doctrine-orm/en/2.6/reference/annotations-reference.html#annref_joincolumn

Add nullable = false to the JoinColumn annotation:

@ORM\JoinColumn(..., nullable=false)
滥情空心 2024-11-16 23:40:26

只是发帖,因为@zim32 没有告诉我们应该把声明放在哪里,所以我不得不进行反复试验。

Yaml:

manyToOne:
    {field}:
        targetEntity: {Entity}
        joinColumn:
            name: {field}
            nullable: false
            referencedColumnName: {id}
        cascade: ['persist']

Just posting because @zim32 didn't tell where we should put the statement, so i had to make a trial and error.

Yaml:

manyToOne:
    {field}:
        targetEntity: {Entity}
        joinColumn:
            name: {field}
            nullable: false
            referencedColumnName: {id}
        cascade: ['persist']
逆光下的微笑 2024-11-16 23:40:26

我找不到如何执行此操作的 XML 示例,因此我将在此保留此代码片段,以防其他人正在寻找此内容:

<many-to-one field="author" target-entity="User">
    <join-column name="author_id" referenced-column-name="id" nullable="false" />
</many-to-one>

名称referenced-column-name 是必需的,请参阅文档:https:// www.doctrine-project.org/projects/doctrine-orm/en/2.6/reference/xml-mapping.html#join-column-element

I couldn't find an XML example of how to do this, so I'm going to leave this snippet here in case anyone else is looking for this:

<many-to-one field="author" target-entity="User">
    <join-column name="author_id" referenced-column-name="id" nullable="false" />
</many-to-one>

The name and referenced-column-name are required, see the docs: https://www.doctrine-project.org/projects/doctrine-orm/en/2.6/reference/xml-mapping.html#join-column-element

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文