Doctrine 2 - 禁止ManyToOne关系的外键上的空值

时间:2022-10-04 10:11:20

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。

2 个解决方案

#1


63  

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

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

Add nullable = false to the JoinColumn annotation:

将nullable = false添加到JoinColumn批注:

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

#2


1  

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

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

Yaml:

YAML:

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

#1


63  

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

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

Add nullable = false to the JoinColumn annotation:

将nullable = false添加到JoinColumn批注:

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

#2


1  

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

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

Yaml:

YAML:

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