原则 2 - 不允许 ManyToOne 关系的外键为空值
我的一个实体中有一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
https://www.doctrine -project.org/projects/doctrine-orm/en/2.6/reference/annotations-reference.html#annref_joincolumn
添加
nullable = false
到JoinColumn
注释:https://www.doctrine-project.org/projects/doctrine-orm/en/2.6/reference/annotations-reference.html#annref_joincolumn
Add
nullable = false
to theJoinColumn
annotation:只是发帖,因为@zim32 没有告诉我们应该把声明放在哪里,所以我不得不进行反复试验。
Yaml:
Just posting because @zim32 didn't tell where we should put the statement, so i had to make a trial and error.
Yaml:
我找不到如何执行此操作的 XML 示例,因此我将在此保留此代码片段,以防其他人正在寻找此内容:
名称 和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:
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