简体   繁体   English

SonataAdmin Bundle 的非空字段

[英]Not Null field with SonataAdmin Bundle

Hi I have a problem with SonataAdminBundle.嗨,我有 SonataAdminBundle 的问题。
I've created "Job" table in DB and I use in backend of my website.我在数据库中创建了“作业”表,并在我的网站后端使用。

When I insert data, I have an error with "not null" fields in my table Job.当我插入数据时,我的表 Job 中出现“非空”字段错误。

For example I have "nb_comment" that is the number of comments of each job,so when I insert in backend all information about Job I don't use a NOT NULL field "nb_comment",and I have the following error :例如,我有“nb_comment”,这是每个作业的评论数,所以当我在后端插入有关作业的所有信息时,我不使用 NOT NULL 字段“nb_comment”,并且出现以下错误:

PDOException: SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'nb_comment' cannot be null  

I had the same problem I think and it works for me.我有同样的问题,我认为它对我有用。 You have to initialize the variable nb_comment in the model with null , like this:您必须使用null初始化模型中的变量nb_comment ,如下所示:

/**
 * @var integer $nb_comment
 */
private $nb_comment = null;

add in @ORM annotation nullable=true添加@ORM 注释nullable=true

use Doctrine\ORM\Mapping as ORM;

...

/**
 * @var integer $nb_comment
 * @ORM\Column(name="nb_comment", type="integer", nullable=true)
 */
private $nb_comment;

or add Constraint validator with @Assert declaration and initialize in construct或使用@Assert 声明添加约束验证器并在构造中初始化

use Symfony\Component\Validator\Constraints as Assert;

/**
 * @var integer $nb_comment
 * @ORM\Column(name="nb_comment", type="integer")
 * @Assert\NotNull()
 */
private $nb_comment;

public function __construct()
{
    $this->nb_comment = 0;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM