2
votes

I'm working on a Zend Framework 2 project where a user must be able to add/edit invoices. An invoice consists of some customer information (e.g. name, address) and one or more invoice lines. For this I'm using two Doctrine 2 entities: Invoice and InvoiceLine. An invoice contains an Doctrine ArrayCollection with all its lines.

I also do have a form for an invoice and a fieldset for lines. This form contains a Form Collection for all lines:

$form->add(array(
    'type' => 'Zend\Form\Element\Collection',
     'name' => 'lines',
     'options' => array(
        'should_create_template' => true
     )
));
$form->get('lines')->setTargetElement($this->createLineFieldset($form->get('lines')-  >getTemplatePlaceholder()));

After posting the form I bind the invoice entity to the form and I validate the form. For a new (non-existing) invoice this works perfect, my invoice contains all the information including lines:

$form->bind($invoice);
$form->setData($request->getPost());

if ($form->isValid()) {
    // Form is valid and invoice contains all information (including lines)
}

If I want to edit an existing invoice things start going wrong. When I populate the form with my existing invoice (and lines) everything seems fine; all fields and fieldsets are populated with the values. But when I post this form all the fieldsets (either existing or new lines) aren't validated and my entity doesn't contain the correct values; all lines are missing.

In short: adding works just fine, but editing an invoice does't.

Am I missing something or is this a bug? Thanks in advance!

2

2 Answers

3
votes

Look at this article Form with relationship One-to-One, One-to-Many http://zf2cheatsheet.com/#form

2
votes

After a long time of debugging I finally fixed the problem :). At first I was using Zend's ObjectProperty hydrator. Changing this to Doctrine's DoctrineObject hydrator solved my problem. I created getters and setters for all properties and changed all hydrators to DoctrineObject and right now everything works fine.