php - Symfony2 - Inject a service into SonataAdmin
I'd like to get a db connection from within a SonataAdmin class'configureFormFields()
For sure this doesn't work
protected function configureFormFields(FormMapper $formMapper)
{
$mycnx = $this->get('doctrine.dbal.mycnx_connection');
//...
I need it for my extension ofSonata/UserBundle/Admin/Entity/UserAdmin
How can I call a service from this class ?
The context:
I need to have a choice field (company) whose choices come from an other connection (from a stored procedure).
Answer
Solution:
What you have to do is:
supply the choices to the field
Then you'll be able to simply use it from
configureFormFields()
with:Answer
Solution:
How to use a second dbal connection to supply choices for a form field
You can actually have this a little easier and with less code involved...
Configuration
Your configuration looks somewhat like this if you already have setup a second connection
doctrine.dbal.mycnx_connection
.app/config/config.yml
Now if you haven't already ... configure a second entity/document manager that is using this connection:
app/config/config.yml
... this may look like much to this point but it really isn't ... as everything described above should already be present in your application if you have a second dbal connection.
I just wanted to make it really obvious for others.
You now have two services available that reflect your two entity-managers:
@doctrine.orm.entity_manager.default
( ... which is aliased to@doctrine.orm.entity_manager
)@doctrine.orm.entity_manager.company
The Entity Field-Type
Now all that's left for you to do is use the entity field-type and configure it to use the entity-manager using the
company
connection.Acme/YourBundle/Form/Type/YourType.php
That's it ... while you may still want to make your form a service ... you don't have to inject anything.
You don't need to use the
EntityRepository $er
part in'query_builder'
... you can just return a QueryBuilder instance if you don't want to create the repository for your companies.Just my few cents ... happy coding!