Symfony: multiple entity manager (sonata) - symfony

I have a hard problem that wants an answear. I am working with Symfony and I installed Sonata to manage the admin area. After I completed to do that, my prompt line give me this error:
This is the error
This is my code:
parameters:
services:
app.security.user_login_form_authenticator:
class: AppBundle\Security\UserLoginFormAuthenticator
autowire: true
app.security.admin_login_form_authenticator:
class: AppBundle\Security\AdminLoginFormAuthenticator
autowire: true
Please, help me.

Autowiring feature is handy, but it has a limitations.
As you say, you have multiple instances of entity manager. So, Symfony doesn't know which of them should be injected into your services. If a service definition were available to change, you would set the autowiring_types parameter to specify a default implementation of dependency. But usualy entity manager services are defined by DoctrineBundle and you can not configure it directly. (As I know, Doctrine configuration doesn't provide options to set up that.)
So, the easiest way is to manually specify the entity manager: just pass a entity manager service ID (doctrine.orm.XXX_entity_manager) to constructor arguments of your services.
services:
app.security.user_login_form_authenticator:
class: AppBundle\Security\UserLoginFormAuthenticator
arguments: [ '#doctrine.orm.XXX_entity_manager' ]
app.security.admin_login_form_authenticator:
class: AppBundle\Security\AdminLoginFormAuthenticator
arguments: [ '#doctrine.orm.YYY_entity_manager' ]
Obviously, if services have other dependendecies, you also need to specify them.

Related

How is symfony normalizer instantiated?

I'm looking at a symfony project that is declaring a custom normalizer. It has an optional argument in the constructor which is getting ignored.
public function __construct(SomeInterface $firstArg, $secondArg = false)
{
//$secondArg is always false
}
There is some yml config:
Path/To/Custom/Normalizer:
arguments:
$secondArg: true
tags: [serializer.normalizer]
I'm trying to understand what is instantiating this class, and why is the second argument always false, despite the fact that the yml config defines it as true.
Is the normalizer instantiated using the yml config, or does symfony instantiate these using some other mechanism?
More info:
if i make the second constructor arg mandatory, then the container wont compile. complaining that it can't autowire the second arg and that i must configure its value explicitly. Which is what i am trying to do.
Autowiring of symfony doesn't know how to autowire scalar parameters type.
Your manual configuration will never be execute if you enable autowiring for this service.. So the container tries to create new instance of your service but he doesn't know how to instantiate the boolean parameters.
What you can to is to exclude this service from the autowiring
It is a bad idea and can become quickly a very big headache to have different default value on constructor and the service configuration itself.
The order in which the bundles are loaded, defined in AppKernel.php makes a difference to the configuration.
Even though there does not seem to be any other configuration of this class in any other bundle.

Public service is handled as private

I have a library used by two Symfony apps, this library defines a set of services that I want to be public (I want to be able to retrieve them directly through the container. When I try to access one service I have this:
The "Library\Service\DerivedServices\OneSpecificImplementation" service or alias has been removed or inlined when the container was compiled. You should either make it public, or stop using the container directly and use dependency injection instead.
The problem is that said service is declared public.
Basically there is:
a Library\Service\BaseService class that has two setters for the common dependencies (doctrine and a logger in this snippet);
several derived class (in the Library\Service\DerivedServices namespace), each defines a new service (with its own constructor to handle DI directly).
So here are my services definitions:
# Base: inject common dependencies
Library\Service\BaseService:
abstract: true
calls:
- [setDoctrine, ['#doctrine.orm.entity_manager']]
- [setLogger, ['#Library\Service\Logger']]
# These services are public to be retrieved directly by the container interface
Library\Service\DerivedServices\:
resource: '../vendor/company/library/src/Library/Service/DerivedServices'
public: true
autowire: true
autoconfigure: false
parent: Library\Service\BaseService
Then, the Symfony application retrieves one derived service like:
$this->get('Library\Service\DerivedServices\OneSpecificImplementation');
These didn't make any difference:
I have changed the order of the service definitions
Both apps run Symfony 4.3.3
I think this is something trivial configuration-wise, but I can't pinpoint it (and after 2 hours trying to debug why the framework compiles my service as private, I thought that someone probably had this and could probably help me).
It turns out the order of declaration of the services matter a lot. As I thought so, the problem was configuration-wise.
I had:
Library\Service\BaseService:
...
Library\Service\DerivedServices\:
...
Library\Service\:
resource: '../vendor/company/library/src/Library/Service'
The last instruction redeclared all services as private (by default).
I changed this to:
Library\Service\:
resource: '../vendor/company/library/src/Library/Service'
Library\Service\BaseService:
...
Library\Service\DerivedServices\:
...
This declared all the services private first, then redeclared them with the new declaration: use of parent + public.

Symfony nested services

Can Symfony DI config file create a service of services? By that I mean something in the spirit of the following gist :
services:
authenticator:
class: Acme\Foo
arguments: [#foo, #parser.users]
parsers: # not valid, this would not compile
users:
class: Csfd\Parsers\User
messages:
class: Csfd\Parsers\Message
I would like to define all parsers as services, but I also want to have them under a parsers branch, so it's obvious they do not actually represent the (also existing) User or Message entities.
In the example, #parser.users would resolve to instance of Csfd\Parsers\User.
This isn't possible by default. The "problem" lies here: YamlFileLoader.php.
As you can see, it doesn't support nesting and treats first level under "services" as service id.
You could create your own YamlFileLoader which would have support for this and use it in your bundle's extension class.

Register Doctrine2 entity listener as service in Symfony2

For the context, I use Symfony 2.4 & Doctrine 2.4.
I'm currently trying to move some logic inside doctrine entity listeners. For now, I have just used the #EntityListeners annotation in order to register my listeners on my entities as it does not need external dependencies. But now, I would like to inject services into my entity listeners though the Symfony2 container. As this feature is quite new, there is not related doc explaining if it is first possible & second, how it can be done.
Thanks!
This is possible in Doctrine 2.4 but requires you to create your own Entity Listener Resolver class.
See Doctrine docs for details on registering a custom resolver which takes a container (or ServiceManager in my case) as a constructor param.
I have not tried this with the Symfony2 container but I have with Zend Framework 2's ServiceManager module and the principles are likely to be extremely similar.
ZF2's DoctrineORMModule makes this whole process very elegant by using the ServiceManager to call MyEntityListenerResolverFactory which in turn injects the ServiceManager into the MyEntityListenerResolver.
Please see the Symfony docs on Entity Listeners to find out how to register entity listeners as a service:
services:
user_listener:
class: \UserListener
tags:
- { name: doctrine.orm.entity_listener }
- { name: doctrine.orm.entity_listener, entity_manager: custom }

How to extend Doctrine service in Symfony2?

I would like to add some methods to Doctrine service in Symfony2. How can I do that? Should I extend some class and register it as a service? I would prefer the service to remain under "doctrine" name.
You have to extend the Symfony\Bundle\DoctrineBundle\Registry class and add you own methods.
Then all you have to do is define a service in app/config/config.yml for example that is named "doctrine":
// app/config/config.yml
services:
doctrine:
class: MyDoctrineRegistry
arguments: [ #service_container ]
Services defined in config.yml will be loaded last, so it will override default one.
If you want to be more clean, you have to override the service in a DependencyInjection/Extension using:
$container->setAlias('doctrine', 'my_doctrine'); // my_doctrine is the name of you custom doctrine service
In this last case, you have to make sure your bundle is loaded AFTER core bundles.
You could specify a wrapper class for a connection and specify custom methods within it. I used this before to extend Doctrine's DBAL functionality and it worked pretty well for my use case.
Details here:
https://stackoverflow.com/a/8242438/312962

Resources