多个嵌入形式的 symfony 2 错误
symfony 2 error on multiple embedded form
我在使用 symfony2 的多重嵌入表单上遇到一个奇怪的错误。
这是我的结构:
被许可人 <- OneToMany -> 注册 <- OneToMany -> 付款
被许可方:
/**
* @ORM\OneToMany(targetEntity="cM\ManagementBundle\Entity\Registration", mappedBy="licensee", cascade={"persist" })
*/
private $registrations;
public function __construct()
{
$this->registrations = new ArrayCollection();
}
public function addRegistration(\cM\ManagementBundle\Entity\Registration $registration)
{
$this->registrations[] = $registration;
$registration->setLicensee($this);
return $this;
}
public function removeRegistration(\cM\ManagementBundle\Entity\Registration $registration)
{
$this->registrations->removeElement($registration);
}
public function setRegistrations(ArrayCollection $registrations)
{
foreach ($registrations as $registration) {
$registration->addLicensee($this);
}
$this->registrations = $registrations;
}
public function getRegistrations() {
return $this->registrations;
}
报名:
/**
* @ORM\ManyToOne(targetEntity="cM\ManagementBundle\Entity\Licensee", inversedBy="registrations")
* @ORM\JoinColumn(nullable=false)
*/
private $licensee;
/**
* @ORM\OneToMany(targetEntity="cM\ManagementBundle\Entity\Payment", mappedBy="registration", cascade={"ALL"}, orphanRemoval=true)
* @ORM\JoinColumn(nullable=false)
*/
private $payments;
public function __construct() {
$this->payments = new ArrayCollection();
}
/**
* Set licensee
*
* @param \cM\ManagementBundle\Entity\Licensee $licensee
* @return Registration
*/
public function setLicensee(\cM\ManagementBundle\Entity\Licensee $licensee)
{
$this->licensee = $licensee;
return $this;
}
/**
* Get licensee
*
* @return \cM\ManagementBundle\Entity\Licensee
*/
public function getLicensee()
{
return $this->licensee;
}
/**
* Add payments
*
* @param \cM\ManagementBundle\Entity\Payment $payment
* @return Registration
*/
public function addPayment(\cM\ManagementBundle\Entity\Payment $payment)
{
$payment->setRegistration($this);
$this->payments[] = $payment;
return $this;
}
/**
* Remove payments
*
* @param \cM\ManagementBundle\Entity\Payment $payment
*/
public function removePayment(\cM\ManagementBundle\Entity\Payment $payment)
{
$this->payments->removeElement($payment);
}
/**
* Get payments
*
* @return \Doctrine\Common\Collections\Collection
*/
public function getPayments()
{
return $this->payments;
}
付款:
/**
* @ORM\ManyToOne(targetEntity="cM\ManagementBundle\Entity\Registration", inversedBy="payments")
* @ORM\JoinColumn(nullable=false, onDelete="CASCADE")
*/
private $registration;
/**
* Set registration
*
* @param \cM\ManagementBundle\Entity\Registration $registration
* @return Payment
*/
public function setRegistration(\cM\ManagementBundle\Entity\Registration $registration)
{
$this->registration = $registration;
return $this;
}
/**
* Get registration
*
* @return \cM\ManagementBundle\Entity\Registration
*/
public function getRegistration()
{
return $this->registration;
}
被许可人类型:
$builder
->add('registrations', 'collection', array(
'type' => new RegistrationType(),
'allow_add' => true,
'by_reference' => false,
))
注册类型:
$builder
->add('payments', 'collection', array(
'type' => new PaymentType(),
'allow_add' => true,
'allow_delete' => true,
'by_reference' => false,
'required' => false,
))
支付类型:
$builder
->add('amount', 'text')
我的主控制器:LicenseeController
$form = $this->createForm('cm_managementbundle_licensee', $licensee);
$form->handleRequest($this->getRequest());
if ($this->getRequest()->getMethod() === 'POST') {
if ($form->isValid()) {
$em = $this->getDoctrine()->getManager();
$em->persist($licensee);
$em->flush();
return $this->redirect($this->generateUrl('homepage'));
}
}
在被许可方表格中,注册是强制性的,但付款是可选的。
- 当我使用注册信息创建新的被许可人并且没有付款时:没问题,信息很好地保存在数据库中
- 当我编辑被许可人并添加一些付款时:没问题
当我使用注册信息和使用一笔或多笔付款创建新的被许可人时,出现原则错误,指出注册不能为空:
使用参数 [null, null, ...] 执行 'INSERT INTO Registration (registrationDate, registrationNumber, ...) VALUES (?, ?, ...)' 时发生异常:
SQLSTATE[23000]:违反完整性约束:1048 列 'registrationDate' 不能为空
当我查看 symfony 分析器时,我看到该学说启动了 2 个注册查询 table :
- "START TRANSACTION"
- INSERT INTO Licensee (lastname, firstname, ...) VALUES (?, ?, ...)
Parameters: { 1: Simpson, 2: Homer, ... }
- INSERT INTO Registration (registrationDate, registrationNumber, ...) VALUES (?, ?, ...)
Parameters: { 1: '2015-01-29 00:00:00', 2: '1234', ... }
- INSERT INTO Registration (registrationDate, registrationNumber, ...) VALUES (?, ?, ...)
Parameters: { 1: null, 2: null, ... }
那为什么有第二个注册查询,所有参数都为空,而第一个是正确的?
我错过了什么?
感谢各位大王的帮助。
好的,成功解决了这个问题。
我必须更改 Payment Collection 的原型名称
->add('payments', 'collection', array(
'type' => new PaymentType(),
'allow_add' => true,
'prototype_name' => 'payments_name'
))
所以当我在添加新付款时使用 javascript 动态更改名称时,我不会在此过程中更改父名称(注册)(默认情况下,所有原型集合都具有相同的名称(姓名)
感谢
Waiting for Dev... 提示
我在使用 symfony2 的多重嵌入表单上遇到一个奇怪的错误。
这是我的结构:
被许可人 <- OneToMany -> 注册 <- OneToMany -> 付款
被许可方:
/**
* @ORM\OneToMany(targetEntity="cM\ManagementBundle\Entity\Registration", mappedBy="licensee", cascade={"persist" })
*/
private $registrations;
public function __construct()
{
$this->registrations = new ArrayCollection();
}
public function addRegistration(\cM\ManagementBundle\Entity\Registration $registration)
{
$this->registrations[] = $registration;
$registration->setLicensee($this);
return $this;
}
public function removeRegistration(\cM\ManagementBundle\Entity\Registration $registration)
{
$this->registrations->removeElement($registration);
}
public function setRegistrations(ArrayCollection $registrations)
{
foreach ($registrations as $registration) {
$registration->addLicensee($this);
}
$this->registrations = $registrations;
}
public function getRegistrations() {
return $this->registrations;
}
报名:
/**
* @ORM\ManyToOne(targetEntity="cM\ManagementBundle\Entity\Licensee", inversedBy="registrations")
* @ORM\JoinColumn(nullable=false)
*/
private $licensee;
/**
* @ORM\OneToMany(targetEntity="cM\ManagementBundle\Entity\Payment", mappedBy="registration", cascade={"ALL"}, orphanRemoval=true)
* @ORM\JoinColumn(nullable=false)
*/
private $payments;
public function __construct() {
$this->payments = new ArrayCollection();
}
/**
* Set licensee
*
* @param \cM\ManagementBundle\Entity\Licensee $licensee
* @return Registration
*/
public function setLicensee(\cM\ManagementBundle\Entity\Licensee $licensee)
{
$this->licensee = $licensee;
return $this;
}
/**
* Get licensee
*
* @return \cM\ManagementBundle\Entity\Licensee
*/
public function getLicensee()
{
return $this->licensee;
}
/**
* Add payments
*
* @param \cM\ManagementBundle\Entity\Payment $payment
* @return Registration
*/
public function addPayment(\cM\ManagementBundle\Entity\Payment $payment)
{
$payment->setRegistration($this);
$this->payments[] = $payment;
return $this;
}
/**
* Remove payments
*
* @param \cM\ManagementBundle\Entity\Payment $payment
*/
public function removePayment(\cM\ManagementBundle\Entity\Payment $payment)
{
$this->payments->removeElement($payment);
}
/**
* Get payments
*
* @return \Doctrine\Common\Collections\Collection
*/
public function getPayments()
{
return $this->payments;
}
付款:
/**
* @ORM\ManyToOne(targetEntity="cM\ManagementBundle\Entity\Registration", inversedBy="payments")
* @ORM\JoinColumn(nullable=false, onDelete="CASCADE")
*/
private $registration;
/**
* Set registration
*
* @param \cM\ManagementBundle\Entity\Registration $registration
* @return Payment
*/
public function setRegistration(\cM\ManagementBundle\Entity\Registration $registration)
{
$this->registration = $registration;
return $this;
}
/**
* Get registration
*
* @return \cM\ManagementBundle\Entity\Registration
*/
public function getRegistration()
{
return $this->registration;
}
被许可人类型:
$builder
->add('registrations', 'collection', array(
'type' => new RegistrationType(),
'allow_add' => true,
'by_reference' => false,
))
注册类型:
$builder
->add('payments', 'collection', array(
'type' => new PaymentType(),
'allow_add' => true,
'allow_delete' => true,
'by_reference' => false,
'required' => false,
))
支付类型:
$builder
->add('amount', 'text')
我的主控制器:LicenseeController
$form = $this->createForm('cm_managementbundle_licensee', $licensee);
$form->handleRequest($this->getRequest());
if ($this->getRequest()->getMethod() === 'POST') {
if ($form->isValid()) {
$em = $this->getDoctrine()->getManager();
$em->persist($licensee);
$em->flush();
return $this->redirect($this->generateUrl('homepage'));
}
}
在被许可方表格中,注册是强制性的,但付款是可选的。
- 当我使用注册信息创建新的被许可人并且没有付款时:没问题,信息很好地保存在数据库中
- 当我编辑被许可人并添加一些付款时:没问题
当我使用注册信息和使用一笔或多笔付款创建新的被许可人时,出现原则错误,指出注册不能为空:
使用参数 [null, null, ...] 执行 'INSERT INTO Registration (registrationDate, registrationNumber, ...) VALUES (?, ?, ...)' 时发生异常:
SQLSTATE[23000]:违反完整性约束:1048 列 'registrationDate' 不能为空
当我查看 symfony 分析器时,我看到该学说启动了 2 个注册查询 table :
- "START TRANSACTION"
- INSERT INTO Licensee (lastname, firstname, ...) VALUES (?, ?, ...)
Parameters: { 1: Simpson, 2: Homer, ... }
- INSERT INTO Registration (registrationDate, registrationNumber, ...) VALUES (?, ?, ...)
Parameters: { 1: '2015-01-29 00:00:00', 2: '1234', ... }
- INSERT INTO Registration (registrationDate, registrationNumber, ...) VALUES (?, ?, ...)
Parameters: { 1: null, 2: null, ... }
那为什么有第二个注册查询,所有参数都为空,而第一个是正确的?
我错过了什么?
感谢各位大王的帮助。
好的,成功解决了这个问题。
我必须更改 Payment Collection 的原型名称
->add('payments', 'collection', array(
'type' => new PaymentType(),
'allow_add' => true,
'prototype_name' => 'payments_name'
))
所以当我在添加新付款时使用 javascript 动态更改名称时,我不会在此过程中更改父名称(注册)(默认情况下,所有原型集合都具有相同的名称(姓名)
感谢 Waiting for Dev... 提示