我正在Symfony做一个项目。为了插入文章,我设置了这些变量:
$article->setCategoryId($category_id);
$article->setName($name);
$article->setContent($description);
$article->setStatus($publicity);
$article->setCreatedOn(new \DateTime());
$article->setCreatedBy($user->getId());
//print_r($article); die;
$em->persist($article);
$em->flush();
当我尝试打印$article
对象时,我得到category_id
值就好了。但是当我尝试保存它时,它会返回一个错误,如:
" SQLSTATE [23000]:完整性约束违规:1048列' category_id'不能为空"
这是我实体的设定者:
/**
* Set categoryId
*
* @param integer $categoryId
*
* @return HelpCenterArticle
*/
public function setCategoryId($categoryId)
{
$this->categoryId = $categoryId;
return $this;
}
在执行print_r($article)
时,这是我的实体:
lpCenterBundle\Entity\HelpCenterArticle Object
(
[id:HelpCenterBundle\Entity\HelpCenterArticle:private] =>
[name:HelpCenterBundle\Entity\HelpCenterArticle:private] => fbfdb
[slug:HelpCenterBundle\Entity\HelpCenterArticle:private] =>
[content:HelpCenterBundle\Entity\HelpCenterArticle:private] => fbdsbf
[categoryId:HelpCenterBundle\Entity\HelpCenterArticle:private] => 4
[createdOn:HelpCenterBundle\Entity\HelpCenterArticle:private] =>
DateTime Object
(
[date] => 2018-06-11 09:58:32
[timezone_type] => 3
[timezone] => Europe/London
)
)
答案 0 :(得分:0)
非常简单
$article->setCategoryId($category_id);
参数 $ category_id 为空或空
如果您想设置空值,请更新ORM 类别
nullable: true
答案 1 :(得分:0)
在您的实体中声明可为空
nullable=true
在实体
中 /**
* @ORM\Column(name="category_id", type="integer", nullable=true)
*/
private $categoryId;