Doctrine 2 can't use nullable=false in manyToOne relation?

前端 未结 2 1698
半阙折子戏
半阙折子戏 2020-12-23 23:49

An User has one Package associated with it. Many users can refer to the same package. User cannot exists without a Package

2条回答
  •  醉梦人生
    2020-12-24 00:50

    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;
    

    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!

提交回复
热议问题