Doctrine 2 can't use nullable = false on manyToOne?

An User has one Package associated with it. Many users can refer to the same package. User cannot exist without Package . User must belong to a relation. The relationship is bidirectional, so Package has zero or more users.

These requirements lead to the ManyToOne relationship for the User and OneToMany Package relationships in Doctrine 2. However, the package_id table in User (that is, an external key) allows null values ​​to be used. I tried setting nullable=false , but the command:

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

Says there is no nullable attribute for the nullable . What am I 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 : resolved. note that this is wrong (note the double quotes):

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

So far this is correct:

 @ORM\JoinColumn(name="package_id", referencedColumnName="id", nullable=false) 
+75
symfony doctrine doctrine2
Mar 12 2018-12-12T00:
source share
1 answer

Use the JoinColumn annotation on ManyToOne:

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

ManyToOne itself cannot be null because it does not apply to a particular column. JoinColumn, on the other hand, identifies a column in the database. This way you can use β€œregular” attributes, such as null or unique!

+139
Mar 12 2018-12-12T00:
source share



All Articles