Tell doctrine that a field can be null

2019-03-05 15:55发布

问题:

What do I have to enter in the annotation of a doctrine entity.

Its actually like this...

/**
 * @ORM\Column(type="string", length=255)
 *
 * @Assert\Length(
 *     min=3,
 *     max=255,
 *     minMessage="The name is too short.",
 *     maxMessage="The name is too long.",
 *     groups={"Registration", "Profile"}
 * )
 */
protected $name;

I have to tell doctrine something like canBeNull=true. Otherwhise I always get this error SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'name' cannot be null

But whats the code?

回答1:

Define the attribute nullable in the COLUMN property with true.

Example:

/**
 * @ORM\Column(type="string", length=255, nullable=true)
 *
 * @Assert\Length(
 *     min=3,
 *     max=255,
 *     minMessage="The name is too short.",
 *     maxMessage="The name is too long.",
 *     groups={"Registration", "Profile"}
 * )
 */
protected $name;