我一直在坚持了几天处理这个问题。我一直在评论其他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”不能为空
每个咨询会欢迎和赞赏。
预先感谢您。
我刚刚注意到你在@ payment中有@JMS \ Exclude()。 _“这个注解可以在一个属性上定义,以表明这个属性不应该被序列化/非序列化,只能和NoneExclusionPolicy结合使用。”_ dunno如果这个原因 – oskr 2014-11-05 16:37:00
@oskr这是因为当我通过API返回这个支付,我不想回复那个。感谢您的通知。 – 2014-11-05 16:38:56
听起来像这样:(看看你的'公共函数添加paymentLine'在你的支付实体)http://stackoverflow.com/questions/26725679/symfony2-referencedcolumnname-id-is-null – webdev 2014-11-05 20:34:56