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

前端 未结 2 1699
半阙折子戏
半阙折子戏 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:42

    Only

    @ORM\JoinColumn(nullable=false)

    is required

    0 讨论(0)
  • 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!

    0 讨论(0)
提交回复
热议问题