Doctrine 2 adapter for Zend_Form model form generator

Tags:

I recently updated the ZF modelform generator to include an adapter compatible with Doctrine 2.

It’s otherwise completely functional, but it ignores many-to-many relations when generating forms (because I didn’t need this feature yet ;) ).

Usage example after the jump.

Basic usage:

$adapter = new CU_Form_Model_Adapter_Doctrine2($entityManager);
$form = new CU_Form_Model(array(
    'adapter' => $adapter,
    'model' => 'My\Entity\Example'
));
 
//Rendering form now would produce a form with fields for My\Entity\Example.

Saving:

//The form returns a new entity, which is persisted
$newEntity = $form->save();
 
//It isn't flushed, so you need to manually flush the EM
$entityManager->flush();

Editing existing entities:

$adapter = new CU_Form_Model_Adapter_Doctrine2($entityManager);
$form = new CU_Form_Model(array(
    'adapter' => $adapter,
    'model' => 'My\Entity\Example'
));
 
$form->setRecord($someEntity);
 
//The form would now be populated with values from $someEntity
//Saving works similar to new entity example, but instead $someEntity will get updated

For examples on other features of CU_Model_Form, see this post