使用 Doctrine & Symfony 获取“违反完整性约束:1048 列‘payment_id’不能为空”

Vic*_*NWD 3 php symfony doctrine-orm

我已经被困在这个问题上几天了。我一直在审查其他 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;

}
Run Code Online (Sandbox Code Playgroud)

我创建了另一个名为 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;
}
Run Code Online (Sandbox Code Playgroud)

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

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

 $paymentForm->handleRequest($request);

 if ($paymentForm->isValid()) {            

    $entityManager = $this->getDoctrine()->getManager();
    $entityManager->persist($payment);
    $entityManager->flush();
 }
Run Code Online (Sandbox Code Playgroud)

此代码处理接收所有付款数据(付款数据和付款行数据)的表单,以验证它们并将其存储在数据库中。当我执行此代码时,出现此错误:

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

每一个建议都将受到欢迎和赞赏。

先感谢您。

Vic*_*NWD 5

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

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

    return $this;
}
Run Code Online (Sandbox Code Playgroud)

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

$paymentLines->setPayment($this);
Run Code Online (Sandbox Code Playgroud)

但只是补充说它不起作用,所以我查看了另一个问题,并且表单中没有“by_reference”参数,所以我的表单是这样的:

$builder
            ->add('payment_lines', 'collection',
                    [
                'type' => new PaymentLineType(),
                'allow_add' => true
                    ]
            )
Run Code Online (Sandbox Code Playgroud)

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

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

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

问候