我在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;
}
LicenseeType :
$builder
->add('registrations', 'collection', array(
'type' => new RegistrationType(),
'allow_add' => true,
'by_reference' => false,
))
RegistrationType :
$builder
->add('payments', 'collection', array(
'type' => new PaymentType(),
'allow_add' => true,
'allow_delete' => true,
'by_reference' => false,
'required' => false,
))
PaymentType :
$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'));
}
}
在被许可方表格中,注册是强制性的,但付款是可选项。
当我创建一个包含注册信息且 一个或多个付款的新被许可人时,存在一个说明注册不能为空的原则错误:
使用params [null,null,...]执行'INSERT INTO Registration(registrationDate,registrationNumber,...)VALUES(?,?,...)'时发生异常:
SQLSTATE [23000]:完整性约束违规:1048列'registrationDate'不能为空
当我查看symfony profiler时,我看到该doctrine启动2个查询注册表:
- "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, ... }
播种为什么第二个注册查询的所有参数都为null而第一个是正确的?
我缺少什么?
感谢任何帮助之王。
答案 0 :(得分:0)
好的,设法解决了这个问题。
我必须更改付款收集的原型名称
->add('payments', 'collection', array(
'type' => new PaymentType(),
'allow_add' => true,
'prototype_name' => 'payments_name'
))
所以当我在添加新的付款时用javascript动态更改名称时,我不会在过程中更改父名称(注册)(默认情况下,所有原型集合都具有相同的名称(名称)
谢谢 Waiting for Dev...提示