将实体添加到父实体的 Doctrine ArrayCollection 时,我收到以下错误。
Integrity constraint violation: 1048 Column 'job_fk' cannot be null
这是父条目
use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Persistence\PersistentObject;
class Job extends \Doctrine\Common\Persistence\PersistentObject
{
/**
* @var integer $pk
* @ORM\Column(name="pk", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="IDENTITY")
*/
protected $pk;
/**
* @ORM\OneToMany(targetEntity="Ajfit\Entity\JobRequirement", mappedBy="jobFk", fetch="LAZY", cascade={"persist"})
*/
protected $requirements;
public function __construct(){
$this->requirements = new ArrayCollection();
}
}
这是子实体
use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Persistence\PersistentObject;
class JobRequirement extends \Doctrine\Common\Persistence\PersistentObject
{
/**
* @var integer $pk
* @ORM\Column(name="pk", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="IDENTITY")
*/
protected $pk;
/**
* @ORM\Column(name="job_fk", type="integer")
* @ORM\ManyToOne(targetEntity="Ajfit\Entity\Job", inversedBy="requirements" )
* @ORM\JoinColumn(name="job_fk", referencedColumnName="pk")
*/
protected $jobFk;
}
这是调用代码
$job = new Ajfit\Entity\Job();
foreach ($addedRequirements as $requirementText)
{
$requirement = new \Ajfit\Entity\JobRequirement();
$job->getRequirements()->add($requirement);
}
子实体添加得很好,但 jobFk 根本没有设置。我在加入时有 cascade={"persist"} ,当我添加所有孩子时,我坚持所有,我的问题是,在我添加孩子之前我需要坚持父母吗?还是我错过了什么?
感谢您的时间。
安德鲁