php 原则 2 - 多对一关系的外键不允许空值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6136601/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 23:27:01  来源:igfitidea点击:

Doctrine 2 - Disallow null value on foreign keys of ManyToOne relationships

phpdoctrine-ormrelationshipmany-to-one

提问by Tobias Gies

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

我在我的一个实体中有一个 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;
    // ...
}

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.

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

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?

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

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

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

回答by Rafael Barros

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

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

Yaml:

亚姆:

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

回答by Stanzi1791

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

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

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

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

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