[英]Symfony form not set entity relation
我有一個實體OutboundInvoice for OneToMany到實體OutboundInvoiceRow,並且為OutboundInvoice創建了具有CollectionType的表單,當我在數據庫中添加一些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]
'type' => new OutBoundInvoiceRowType()
錯誤,應改用entry_type選項
您的實體中沒有removeRow
方法
Doctrine將僅檢查擁有方的級聯操作,因此您不必在OutboundInvoiceRow
實體中聲明級聯操作
然后在'by_reference' => false
添加'by_reference' => false
(在收集選項中)。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.