gpt4 book ai didi

php - 获取 "Integrity constraint violation: 1048 Column ' payment_id' cannot be null"using Doctrine & Symfony

转载 作者:可可西里 更新时间:2023-11-01 01:00:58 25 4
gpt4 key购买 nike

我已经被困了几天来处理这个问题。我一直在查看其他 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”不能为空

我们欢迎并感谢您的每一条建议。

提前谢谢你。

最佳答案

感谢 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
]
)

终于,我的问题解决了。谢谢你们的帮助。

问候

关于php - 获取 "Integrity constraint violation: 1048 Column ' payment_id' cannot be null"using Doctrine & Symfony,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26762132/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com