Doctrine 2在manyToOne关系中不能使用nullable = false? [英] Doctrine 2 can't use nullable=false in manyToOne relation?

查看:146
本文介绍了Doctrine 2在manyToOne关系中不能使用nullable = false?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一个用户有一个与之相关联。许多用户可以参考相同的包。 用户不能存在而没有定义用户应该拥有该关系。关系是双向的,所以中有一个或多个用户。

An User has one Package associated with it. Many users can refer to the same package. User cannot exists without a Package defined. User should own the relation. Relation is bidirectional, so a Package has zero or more users in it.

这些要求导致 User ManyToOne OneToMany 关系在Doctrine 2.然而 package_id in 用户表(这是外键)允许 null 值。我尝试设置 nullable = false 但命令:

These requirements lead to ManyToOne relation for User and OneToMany relation of Package in Doctrine 2. However package_id in user table (that is foreign-key) allows null values. I've tried setting nullable=false but command:

 php app/console doctrine:generate:entities DL --path="src" --no-backup

说对于 ManyToOne 的关系,没有属性可空值我缺少什么?

Says that there is no attribute nullable for the relation ManyToOne. What i'm missing?

class User
{

    /**
     * @ORM\Id
     * @ORM\Column(type="integer")
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

    /**
     * @ORM\ManyToOne(targetEntity="Package", inversedBy="users")
     */
    private $package;

}

class Package
{

    /**
     * @ORM\Id
     * @ORM\Column(type="integer")
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

    /**
     * @ORM\OneToMany(targetEntity="User", mappedBy="package")
     */
    private $users;

}

编辑:已解决。请注意这是错误(注意双引号):

EDIT: solved. please note that this is wrong (note double quotes):

 @ORM\JoinColumn(name="package_id", referencedColumnName="id", nullable="false")

虽然这是正确的:

@ORM\JoinColumn(name="package_id", referencedColumnName="id", nullable=false)


推荐答案

在ManyToOne关系中使用JoinColumn注释:

Use the JoinColumn annotation on your ManyToOne relation:

/**
 * @ORM\ManyToOne(targetEntity="Package", inversedBy="users")
 * @ORM\JoinColumn(name="package_id", referencedColumnName="id", nullable=false)
 */
private $package;

ManyToOne本身不能为空,因为它与特定列无关。另一方面,JoinColumn标识数据库中的列。因此,您可以使用正常属性,如可空或唯一的!

The ManyToOne itself cannot be nullable, because it doesn't relate to a specific column. The JoinColumn on the other hand identifies the column in the database. Thus, you can use "normal" attributes like nullable or unique!

这篇关于Doctrine 2在manyToOne关系中不能使用nullable = false?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆