2014-11-05 93 views
3

我一直在坚持了几天处理这个问题。我一直在评论其他StackOverflow问题和不同的论坛,但我无法得到这个工作,所以这就是这个问题的原因。获取“完整性约束违规:1048列'payment_id'不能为空”使用Doctrine&Symfony

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

/** 
* 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关联,这个关联由form处理来验证数据。之后的数据验证,我想将其存储在数据库中后对其进行处理,所以我使用这些代码行做到这一点:

$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”不能为空

每个咨询会欢迎和赞赏。

预先感谢您。

+0

我刚刚注意到你在@ payment中有@JMS \ Exclude()。 _“这个注解可以在一个属性上定义,以表明这个属性不应该被序列化/非序列化,只能和NoneExclusionPolicy结合使用。”_ dunno如果这个原因 – oskr 2014-11-05 16:37:00

+1

@oskr这是因为当我通过API返回这个支付,我不想回复那个。感谢您的通知。 – 2014-11-05 16:38:56

+2

听起来像这样:(看看你的'公共函数添加paymentLine'在你的支付实体)http://stackoverflow.com/questions/26725679/symfony2-referencedcolumnname-id-is-null – webdev 2014-11-05 20:34:56

回答

4

感谢Keefe Kwan,我解决了我的问题。就像他的评论说,我的问题是由学说生成的代码,“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 
        ] 
      ) 

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

Regards

+0

''by_reference' =>假'行为我做了,很好的抓住!没有它,在我的代码中,'addPaymentLine()'setter将永远不会被调用。 – DelphiLynx 2017-11-17 09:00:45

相关问题