过去几天我一直在审查Stackoverflow上的完整性约束违规问题,试图解决这个问题,但我没有成功推断出正确的学说逻辑。
我有两个表达一对多和一对多关系的实体,但是当我尝试添加并保留一个项目时,我得到一个插入错误:
(在控制器中)
class Users implements UserInterface
{
/**
* @ORM\OneToMany(targetEntity="AppBundle\Entity\Charts",cascade={"persist"}, mappedBy="charts")
*/
private $userscharts;
public function __construct()
{
$this->userscharts = new ArrayCollection();
//parent::__construct();
}
/**
* Add userschart
*
* @param \AppBundle\Entity\Charts $userschart
*
* @return Users
*/
public function addUserschart(\AppBundle\Entity\Charts $userschart)
{
$this->userscharts->add($userschart);
return $this;
}
.......
}
class Charts
{
/**
* @ORM\ManyToOne(targetEntity="AppBundle\Entity\Users", inversedBy="userscharts")
* @ORM\JoinColumn(name="id_user", referencedColumnName="id_user")
*/
private $charts;
/**
* @var integer
*
* @ORM\Column(name="chart_no", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="NONE")
*/
private $chartNo;
/**
* @var integer
*
* @ORM\Column(name="id_user", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="NONE")
*/
private $idUser;
/**
* Set chartNo
*
* @param integer $chartNo
*
* @return Charts
*/
public function setChartNo($chartNo)
{
$this->chartNo = $chartNo;
return $this;
}
/**
* Get chartNo
*
* @return integer
*/
public function getChartNo()
{
return $this->chartNo;
}
/**
* Set idUser
*
* @param integer $idUser
*
* @return Charts
*/
public function setIdUser($idUser)
{
$this->idUser = $idUser;
return $this;
}
/**
* Get idUser
*
* @return integer
*/
public function getIdUser()
{
return $this->idUser;
}
/**
* Set charts
*
* @param \AppBundle\Entity\Users $charts
*
* @return Charts
*/
public function setCharts(\AppBundle\Entity\Users $charts = null)
{
$this->charts = $charts;
return $this;
}
/**
* Get charts
*
* @return \AppBundle\Entity\Users
*/
public function getCharts()
{
return $this->charts;
}
}
使用参数[9999,null]执行'INSERT INTO charts(chart_no,id_user)VALUES(?,?)'时发生异常:
SQLSTATE [23000]:完整性约束违规:1048列'id_user'不能为空
getOrCreate()