-
-
Notifications
You must be signed in to change notification settings - Fork 269
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Automatically inject ObjectManager in form elements through FormElementManager #175
Comments
@danizord see doctrine/DoctrineORMModule#142 @bakura10 wouldn't that be problematic for the ODM folks btw? |
If you implement ObjectManagerProviderInterface or add the traits ProvidesObjectManager it should already do it. (well, when this thing will be merged: #169). Why would it be problamtic for OMD ? |
Initializers are bad for performance, why do not factories? |
Since I'm running PHP 5.3. I couldn't implement the traits ProvidesObjectManager. Alternatively when using ObjectManagerProviderInterface an exception is thrown: no object manager was set. |
@bakura10 : I implemented the ObjectManagerAwareInterface successfully. But calling the objectManager returns NULL. Do I have to config my DI elsewhere our should it work from scratch? I am using ZF 2.1:
|
@webdevilopers currently we do not inject the ObjectManager automatically for you, this is a proposal that has not been merged yet. As a temporary workaround, you can put it in your use DoctrineModule\Persistence\ObjectManagerAwareInterface;
...
public function getFormElementConfig()
{
return array(
'initializers' => array(
'ObjectManagerInitializer' => function ($element, $formElements) {
if ($element instanceof ObjectManagerAwareInterface) {
$services = $formElements->getServiceLocator();
$entityManager = $services->get('Doctrine\ORM\EntityManager');
$element->setObjectManager($entityManager);
}
},
),
);
} |
@danizord : I tried but the initializers are not called. Do I have to change the key 'ObjectManagerInitializer' or anything else to make it work with Fieldsets? |
@webdevilopers Sure you're using the FormElementManager to get this fieldset? |
@danizord : But how to? When using the FormElementManager to get my parent form I add my fieldset in the constructor. As far as I know the serviceLocator is only loaded in the init() method. |
@webdevilopers Make sure you are doing the following in your controller: $forms = $this->getServiceLocator()->get('FormElementManager');
$myForm = $forms->get('Application\Form\MyForm'); then add your fieldset inside the This should work (: |
@danizord That is what I tried. But the init function in my Form is never called. :/ |
@webdevilopers Please paste your form here. |
Controller:
Form:
Results in:
|
@webdevilopers Sounds like a problem with config of FormElementManager, Can you also paste your FormElementManager configuration? |
You mean the configuration you recommended, @danizord (see above)? Or is there another place for configuring?
The getFormElementConfig method indeed gets called but none of the initializers. |
@webdevilopers Strange, it should work, try adding the following key: 'invokables' => array(
'CreateCostcenter' => 'Application\Form\CreateCostcenter',
), And in your controller, get the form like this: $forms->get('CreateCostcenter'); @Ocramius can you comment on this one? |
@danizord can't comment now since I didn't follow |
@Ocramius just read the above comments 😓 |
I added the invokables in the getFormElementConfig method, right @danizord ?
will make the error disapear but not call the init() function on the Form. Where does your "$forms" come from? A question general regardless objectManager and others: |
@webdevilopers is the FormElementManager who calls the $forms = $this->getServiceLocator()->get('FormElementManager');
$myForm = $forms->get('CreateCostcenterForm'); Assuming you defined CreateCostcenterForm as invokable in your |
This again leads me to my first error: |
@webdevilopers I was with the same problem, but i did as explain by @danizord and it works (Muito Obrigado @danizord). @webdevilopers did you update your zend framework version to 2.1.* or you're still using 2.0.? Because as i know this FormElementManager functionality was not present in 2.0. |
You're welcome, @pauloelr (: |
Thank you guys, I finally got it working! I was using the wrong Alias for my Form inside the invokables. My final result inside my Module.php now looks like:
And nothing else. Furthermore I removed the ObjectManagerAwareInterface from my form and used it for my fieldset since this is the only place I need it. A) Anyway, if I had a form with several Fieldsets that need the objectManager. Should each Fieldset implement the ObjectManagerAwareInterface? Or is there a good way to inherit the Fieldsets from inside the main form? B) What is best practice to switch between the ORM entityManager and ODM objectManager? I hope this doesn't go beyond the scope of this issue. |
A) You must implement And yes, you're going beyond the scope of this issue. |
Sorry for delete the comment. I configured the initializer :D I already had resolved. I'm not sure if was this, but I in my form class I wasn't calling |
Closing. I also closed the PR for the initializer provided by DoctrineModule since the approach was basically broken. Implementing an initializer is quite trivial, I don't think we should over-complicate things by providing own magic |
@Ocramius I'm not talking about Initializers, I'm talking about factories in DoctrineORMModule and DoctrineMongoODMModule. Initializers are bad, but factories are good! |
Argh, my bad - lost topic because of the entire initializers chatting going on. No, I don't think that would work since we'd have custom names for orm and odm... That would start becoming really messy... |
@Ocramius We don't need custom names. I'll make a PR in DoctrineORMModule to make more clearer my approach. |
@danizord thank you :) |
Inject the ObjectManager within my forms and fieldsets because
DoctrineModule\Form\Element\ObjectSelect
needs an ObjectManager is not very cool :/And since ZF 2.1 was released, we have an FormElementManager.
I Think that DoctrineORMModule and DoctrineMongoODMModule can register factories for
DoctrineModule\Form\Element\*
that inject their own ObjectManager instance.What do you think?
The text was updated successfully, but these errors were encountered: