【发布时间】:2017-01-11 14:36:23
【问题描述】:
我有实体 OutboundInvoice for OneToMany 到实体 OutboundInvoiceRow
我使用 CollectionType 为 OutboundInvoice 创建表单,当我在我的数据库中添加一些 OutboundInvoiceRow 时,在 OutboundInvoiceRow 中看不到 invoice_id,但是当在调试中看到 $form->handleRequest($request); 时,我看到带有集合 OutboundInvoiceRow 的 OutboundInvoice,在里面看到我的 OutboundInvoiceRow。我尝试调试而不是在函数 addRows 或 addRow 中的实体 OutboundInvoice 中输入。现在我有带有集合 OutboundInvoiceRow 的实体 OutboundInvoice,但在没有 OutboundInvoice 的情况下刷新 DB OutboundInvoiceRow 之后。在哪里将 OutboundInvoice 设置为 OutboundInvoiceRow 或使用表单有多正确???
class OutboundInvoice
{
//
/**
* @var \OutboundInvoiceRow
*
* @ORM\OneToMany(targetEntity="OutboundInvoiceRow", mappedBy="invoice", cascade={"persist"})
*/
private $rows;
//
/**
* Add row
*
* @param \AppBundle\Entity\OutboundInvoiceRow $row
*
* @return OutboundInvoice
*/
public function addRow(\AppBundle\Entity\OutboundInvoiceRow $row)
{
$this->rows[] = $row;
$row->setInvoice($this);
return $this;
}
/**
* @param OutboundInvoiceRow[] $rows
* @return $this
*/
public function addRows($rows)
{
foreach ($rows as $row) {
if ($row instanceof OutboundInvoiceRow) {
$this->addRow($row);
}
}
return $this;
}
我的实体 OutboundInvoiceRow
class OutboundInvoiceRow
{
/**
* @var \OutboundInvoice
*
* @ORM\ManyToOne(targetEntity="OutboundInvoice", inversedBy="rows", cascade={"persist"})
* @ORM\JoinColumns({
* @ORM\JoinColumn(name="invoice_id", referencedColumnName="id", onDelete="CASCADE")
* })
*/
private $invoice;
//
/**
* Set invoice
*
* @param \AppBundle\Entity\OutboundInvoice $invoice
*
* @return OutboundInvoiceRow
*/
public function setInvoice(\AppBundle\Entity\OutboundInvoice $invoice = null)
{
$this->invoice = $invoice;
return $this;
}
我的行动
public function createAction(Request $request)
{
$entity = new OutboundInvoice();
$form = $this->createCreateForm($entity);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$em = $this->getDoctrine()->getManager();
$em->persist($entity);
$em->flush();
return $this->redirect($this->generateUrl('new_outbound_invoices'));
}
return array(
'entity' => $entity,
'form' => $form->createView(),
);
}
private function createCreateForm(OutboundInvoice $entity)
{
$form = $this->createForm(new OutboundInvoiceForm(), $entity, array(
'validation_groups' => ['post_out_bound_invoice'],
'cascade_validation' => true,
'action' => $this->generateUrl('outbound_invoices_create'),
'method' => 'POST',
));
$form->add('submit', 'submit', array('label' => 'Create'));
return $form;
}
我的表格
class OutboundInvoiceForm extends AbstractType
{
/**
* @param FormBuilderInterface $builder
* @param array $options
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
////
->add('rows', CollectionType::class, array(
'entry_type' => OutBoundInvoiceRowType::class,
'allow_add' => true,
'allow_delete' => true,
'prototype' => true,
'prototype_name' => 'rows__name__',
'error_bubbling' => true
))
;
}
/**
* @param OptionsResolverInterface $resolver
*/
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
'data_class' => OutboundInvoice::class,
'csrf_protection' => false,
));
}
这是我在提交和有效表单后调试时的实体
$entity = {AppBundle\Entity\OutboundInvoice} [27]
id = null
invoiceNumber = null
receiver = null
streetAddress = null
postal = null
postOffice = null
invoicingEmail = null
edi = null
dueDate = null
reversedVat = true
invoiceDate = null
referenceNumber = null
customerReference = "qqqq"
companyReference = "qqqq"
message = "ddd"
notes = "dddd"
termsOfPayment = 22
status = "draft"
currencyCode = "EUR"
languageCode = "FI"
netvisorId = null
crmCustomer = null
customer = {AppBundle\Entity\Customer} [17]
inboundInvoice = null
invoicingType = null
serviceCompany = null
rows = {Doctrine\Common\Collections\ArrayCollection} [1]
elements = {array} [1]
rows0 = {AppBundle\Entity\OutboundInvoiceRow} [13]
id = null
description = "qqq"
unitPrice = "22"
amount = "22"
vat = 3
unit = "22"
accountingAccount = null
contract = null
crmContract = null
costObject = null
taskExecution = null
invoice = null //why not out bound invoice id ?? after flush still empty
location = {AppBundle\Entity\Location} [38]
【问题讨论】: