What I want to know is the best way to create a form which matches an entity.
- Manually Create the form either in code or using the form annotations?
- Using Annotations from the entity?
I’ve see a few different examples, some using @Annotation and others using @Form? Could somebody please explain the difference?
In the case of entities having related/nested entities do I need to provide a custom hydrator for every entity? I’m presuming Doctrine may already have one that implements the hydrator interfaces?
To Summarise:
– Best way to create a form from an Entity.
– Difference between @Form and @Annotation
– Does Doctrine have a Hydrator for it’s Entities?
First off: Annotations are a Speed-Killer. If you want to use the annotation builder, please ALWAYS Cache the created objects. But annotations are also the easiest way to get a form running 😉
Second: The Hydrator. When using ZF2 Forms in conjunction with Doctrine 2 you most likely want to use the DoctrineEntity Hydrator located inside. Consider the following code:
Zend\Form\Form ObjectDoctrineORMModule\Stdlib\Hydrator\DoctrineEntityWhen not using annotations and you’re referencing another Entity, then make sure you use the appropriate form element (in most cases this would be a select-element (like selecting a CategoryEntity for a BlogEntity or something)
As you can see, the Form element needs to know about the entityManager, too. This is why ideally you’d want to extend the first Code-Example with another setter to inject the entityManager into your form object.
What’s the best approach in general? I’d say there is none. For speed purposes, annotations solely are a killer. Using cached versions should help out, though i have no personal experience with caching in ZF2 just yet. I like to create my forms from hand outside of annotations, simply because my IDE supports a lot of stuff, but certainly not form annotations 😀
Hope this could help you a bit and i didn’t write too much out of context 😛