【问题标题】:Getting "Integrity constraint violation: 1048 Column 'payment_id' cannot be null" using Doctrine & Symfony使用 Doctrine & Symfony 获取“违反完整性约束:1048 列 'payment_id' 不能为空”
【发布时间】:2014-11-05 16:26:00
【问题描述】:

我已经被困了几天来处理这个问题。我一直在查看其他 StackOverflow 问题和不同的论坛,但我无法让它工作,所以这就是这个问题的原因。

我正在开发一个包含付款的系统,因此我创建了一个“付款”类,如下所示:

/**
 * Payment
 *
 * @ORM\Table()
 * @ORM\Entity(repositoryClass="PaymentRepository")
 */
 class Payment
 {

   /**
     * @var integer
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     * @JMS\Groups({"public"})
     * @JMS\Type("integer")
     */
     protected $id;

   /**
     * @var ArrayCollection
     *
     * @ORM\OneToMany(targetEntity="PaymentLine", mappedBy="payment", cascade={"persist",    "remove"})
     * @Assert\Valid()
     * @JMS\Groups({"public","create"})
     * @JMS\Type("ArrayCollection<JensenTech\PaymentBundle\Entity\PaymentLine>")
     */
     protected $paymentLines;

     /**
       * @var string
       *
       * @ORM\Column(name="total_net", type="decimal", precision=5, scale=2)
       * @Assert\NotBlank(message="Invalid Net Amount")
       * @JMS\Groups({"public","create"})
       */
       protected $totalNet;

     /**
       * @var string
       * @ORM\Column(name="total_vat", type="decimal", precision=5, scale=2)
       * @Assert\NotBlank(message="Invalid VAT Amount")
       * @JMS\Groups({"public","create"})
       */
       protected $totalVat;

      /**
        * @var string
        * @ORM\Column(name="total_gross", type="decimal", precision=5, scale=2)
        * @Assert\NotBlank(message="Invalid Gross Amount")
        * @JMS\Groups({"public","create"})
        */
        protected $totalGross;

}

我还创建了另一个名为 PaymentLine 的类来存储有关每条支付线的详细信息:

/**
  * PaymentLine
  *
  * @ORM\Table()
  * @ORM\Entity
  * @ORM\HasLifecycleCallbacks()
  */
  class PaymentLine
  {

   /**
     * @var integer
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     * @JMS\Exclude()
     */
     protected $id;

   /**
     * @var Payment
     * @ORM\ManyToOne(targetEntity="Payment", inversedBy="paymentLines")
     * @ORM\JoinColumn(nullable=false)
     * @JMS\Exclude()
     */
     protected $payment;

   /**
     * @var string
     *
     * @ORM\Column(name="concept", type="string", length=255)
     * @JMS\Groups({"public", "create"})
     */
     protected $concept;

   /**
     * @var integer
     *
     * @ORM\Column(name="quantity", type="smallint")
     * @JMS\Groups({"public", "create"})
     */
     protected $quantity;

   /**
     * @var float
     *
     * @ORM\Column(name="unit_price", type="decimal", precision=5, scale=2)
     * @JMS\Groups({"public", "create"})
     */
    protected $unitPrice;
}

如您所见,这是一个 OneToMany 关联,该关联由表单处理以验证数据。数据验证后,我想将其存储在数据库中以进行处理,因此我使用这些代码行来做到这一点:

 $payment = new Payment();
 $paymentForm = $this->createForm('payment', $payment);

 $paymentForm->handleRequest($request);

 if ($paymentForm->isValid()) {            

    $entityManager = $this->getDoctrine()->getManager();
    $entityManager->persist($payment);
    $entityManager->flush();
 }

此代码处理接收所有支付数据(支付数据和支付行数据)的表单,以验证它们并存储在数据库中。当我执行这段代码时,我得到了这个错误:

SQLSTATE[23000]:违反完整性约束:1048 列“payment_id”不能为空

欢迎和赞赏每一个建议。

提前谢谢你。

【问题讨论】:

  • 我刚刚注意到您在 $payment 中有 @JMS\Exclude()。 “可以在属性上定义此注释以指示该属性不应序列化/反序列化。仅与 NoneExclusionPolicy 结合使用。”不知道是不是这个原因
  • @oskr 之所以存在,是因为当我通过 API 退回这笔付款时,我不想退回该款项。感谢您的通知。
  • 听起来与此类似:(查看您的付款实体中的public function add paymentLinestackoverflow.com/questions/26725679/…

标签: php symfony doctrine-orm


【解决方案1】:

感谢 Keefe Kwan,我已经解决了我的问题。正如他在评论中所说,我的问题是Doctrine生成的代码,更具体的“addPaymentLine”方法,该方法如下:

/**
 * Add paymentLines
 *
 * @param PaymentLine $paymentLines
 * @return Payment
 */
public function addPaymentLine(PaymentLine $paymentLines)
{
    $this->paymentLines[] = $paymentLines;

    return $this;
}

所以我编辑它添加了这一行:

$paymentLines->setPayment($this);

但是只是补充说不行,所以我看了一下this other question,我的表单中没有“by_reference”参数,所以我的表单是这样的:

$builder
            ->add('payment_lines', 'collection',
                    [
                'type' => new PaymentLineType(),
                'allow_add' => true
                    ]
            )

所以添加该参数我的表单现在是这样的:

$builder
            ->add('payment_lines', 'collection',
                    [
                'type' => new PaymentLineType(),
                'allow_add' => true,
                'by_reference' => false
                    ]
            )

所以最后,我的问题已经解决了。谢谢大家的帮助。

问候

【讨论】:

  • 'by_reference' =&gt; false 行帮我做了,很好!没有它,在我的代码中,addPaymentLine() setter 将永远不会被调用。
猜你喜欢
  • 1970-01-01
  • 2020-03-29
  • 2018-03-04
  • 2018-05-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-11-20
相关资源
最近更新 更多