Getting "Integrity constraint violation: 1048 Column" payment_id "cannot be" null "using Doctrine & Symfony

I have been dealing with this problem for a couple of days. I have looked at other StackOverflow questions and various forums but I couldn't get this to work, so that's the reason for this question.

I am developing a system that contains payments, so I created the Payment class as follows:

/**
 * 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;

}

      

And I created another class called PaymentLine to store the details of each payment line:

/**
  * 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;
}

      

As you can see, this is a OneToMany association, this association is processed by the form to validate the data. After validating the data, I want to save it to the database to process it afterwards, so I use these lines of code to do this:

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

 $paymentForm->handleRequest($request);

 if ($paymentForm->isValid()) {            

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

      

This code processes a form that receives all payment data (payment data and payline data) to be verified and stored in the database. When I execute this code, I get this error:

SQLSTATE [23000]: Integrity constraint violation: 1048 Column "payment_id" cannot be null

Every advice will be appreciated and appreciated.

Thanks in advance.

+3


source to share


1 answer


Thanks to Keefe Kwan I solved my problem. As he said in a comment, my problem was that the code generated by Doctrine's "addPaymentLine" method was more specific, this method was as follows:

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

    return $this;
}

      

So, I edited it by adding this line:

$paymentLines->setPayment($this);

      

But just adding that it didn't work, so I looked at this other question and I didn't have a "by_reference" parameter on the forms, so my form was like this:



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

      

So by adding this parameter, my form now looks something like this:

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

      

So finally my problem is solved. Thanks guys for your help.

Hello

+4


source







All Articles