使用persist插入实体:不能为null

时间:2016-02-10 08:38:34

标签: symfony doctrine-orm doctrine

我和Symfony有两个实体“OneToOne”:“charge”和“ecriture”:

收费实体:     

    /**
     * @ORM\OneToOne(targetEntity="LogicielBundle\Entity\Ecriture", inversedBy="coproprieteCharge", cascade={"persist"})
     * @ORM\JoinColumn(nullable=false)
     */
    private $ecriture;

    public function setEcriture(\LogicielBundle\Entity\Ecriture $ecriture)
    {
        $this->ecriture = $ecriture;
        $ecriture->setCoproprieteCharge($this);
        return $this;
    }

Ecriture实体:

<?php
class Ecriture
{

    /**
     * @ORM\OneToOne(targetEntity="LogicielBundle\Entity\Copropriete\Charge", mappedBy="ecriture", cascade={"persist"})
     * @ORM\JoinColumn(nullable=true)
     */
    private $coproprieteCharge;

    public function setCoproprieteCharge(\LogicielBundle\Entity\Copropriete\Charge $coproprieteCharge)
    {
        $this->coproprieteCharge = $coproprieteCharge;
        return $this;
    }

我有一个非常简单的形式与imbrication:

ChargeType:

<?php
class ChargeType extends AbstractType
{

    /**
     * @param FormBuilderInterface $builder
     * @param array $options
     */
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('ecriture', new EcritureType, array(
                'label' => false
            ))
        ;
    }

这个控制器非常简单:只需调用“New Charge()”,因为我认为Ecriture在实体评论中插入了“persist”。

但是我有这个错误“ SQLSTATE [23000]:完整性约束违规:1048列'ecriture_id'不能为空”;我不明白因为“ecriture”插入“persist”

1 个答案:

答案 0 :(得分:0)

OneToOne仅由一个连接列表示,它是您使用inversedBy的字段上的连接列。在那里你指定,这个列不可为空。由于您可能没有将Ecriture的实例设置为Charge实体的新实例,因此它保持为空并且违反了您指定的nullable=false

级联持久化并不意味着实体管理器创建了一个新的实体实例,如果它尚未设置为不可空字段。实体managaer可能不知道如何创建一个(例如,实体可以具有带有所需参数的构造函数)。

相关问题