I still haven't come around to write the tutorial for that :S
I don't know if this is working with the annotationbuilder though! As the DoctrineModuleFormElementObjectSelect
needs the EntityManager
to work. The options for the ObjectSelect
are as follows:
$this->add(array(
'name' => 'formElementName',
'type' => 'DoctrineModuleFormElementObjectSelect',
'attributes' => array(
'required' => true
),
'options' => array(
'label' => 'formElementLabel',
'empty_option' => '--- choose formElementName ---',
'object_manager' => $this->getEntityManager(),
'target_class' => 'MynamespaceEntityEntityname',
'property' => 'nameOfEntityPropertyAsSelect'
)
));
In this case i make use of $this->getEntityManager()
. I set up this dependency when calling the form from the ServiceManager. Personally i always do this from FactoryClasses. My FormFactory looks like this:
public function createService(ServiceLocatorInterface $serviceLocator)
{
$em = $serviceLocator->get('DoctrineORMEntityManager');
$form = new ErgebnishaushaltProduktForm('ergebnisform', array(
'entity_manager' => $em
));
$classMethodsHydrator = new ClassMethodsHydrator(false);
// Wir fügen zwei Strategien, um benutzerdefinierte Logik w?hrend Extrakt auszuführen
$classMethodsHydrator->addStrategy('produktBereich', new StrategyProduktbereichStrategy())
->addStrategy('produktGruppe', new StrategyProduktgruppeStrategy());
$hydrator = new DoctrineEntity($em, $classMethodsHydrator);
$form->setHydrator($hydrator)
->setObject(new ErgebnishaushaltProdukt())
->setInputFilter(new ErgebnishaushaltProduktFilter())
->setAttribute('method', 'post');
return $form;
}
And this is where all the magic is happening. Magic, that is also relevant to your other Thread here on SO. First, i grab the EntityManager
. Then i create my form, and inject the dependency for the EntityManager
. I do this using my own Form, you may write and use a Setter-Function to inject the EntityManager
.
Next i create a ClassMethodsHydrator
and add two HydrationStrategies
to it. Personally i need to apply those strategies for each ObjectSelect
-Element. You may not have to do this on your side. Try to see if it is working without it first!
After that, i create the DoctrineEntity
-Hydrator, inject the EntityManager
as well as my custom ClassMethodsHydrator
. This way the Strategies will be added easily.
The rest should be quite self-explanatory (despite the german classnames :D)
Why the need for strategies
Imo, this is something missing from the DoctrineEntity
currently, but things are still in an early stage. And once DoctrineModule-Issue#106 will be live, things will change again, probably making it more comfortable.
A Strategy looks like this:
<?php
namespace HaushaltportalStdlibHydratorStrategy;
use ZendStdlibHydratorStrategyStrategyInterface;
class ProduktbereichStrategy implements StrategyInterface
{
public function extract($value)
{
if (is_numeric($value) || $value === null) {
return $value;
}
return $value->getId();
}
public function hydrate($value)
{
return $value;
}
}
So whenever the $value
is not numeric or null, meaning: it should be an Object, we will call the getId()
function. Personally i think it's a good idea to give each Element it's own strategy, but if you are sure you won't be needing to change the strategy at a later point, you could create a global Strategy for several elements like DefaultGetIdStrategy
or something.
All this is basically the good work of Michael Gallego aka Bakura! In case you drop by the IRC, just hug him once ;)
Edit An additional resource with a look into the future - updated hydrator-docs for a very likely, soon to be included, pull request