Reputation: 4051
I have entity OutboundInvoice for OneToMany to entity OutboundInvoiceRow
And I create form with CollectionType for OutboundInvoice and when I add some OutboundInvoiceRow in my DB not see invoice_id
in OutboundInvoiceRow but when $form->handleRequest($request);
in debug I see OutboundInvoice with collection OutboundInvoiceRow and in inside see my OutboundInvoiceRow. I try debug and not entry in entity OutboundInvoice in function addRows or addRow. Now I have entity OutboundInvoice with collection OutboundInvoiceRow but after flush in DB OutboundInvoiceRow without OutboundInvoice. Where set OutboundInvoice to OutboundInvoiceRow or how correct using form ???
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;
}
my entity 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;
}
my action
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;
}
my 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,
));
}
this my entity when debug after submit and valid form
$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]
Upvotes: 0
Views: 745
Reputation: 1055
'type' => new OutBoundInvoiceRowType()
is wrong, you should use entry_type option instead
You don't have removeRow
method in your entity
Doctrine will check only owning side for cascade operations so you don't have to declare cascade operations in your OutboundInvoiceRow
entity
And add 'by_reference' => false
to your form (among collection options).
Upvotes: 1