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
Related
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.
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.
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.
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 }
Is there a way to access the configuration parameters in config.yml from the model layer? From the controller I can use $this->container->getParameter('xyz'). But how can it be done from a class in the Model layer?
In symfony2 Entities are designed as POPOs, meaning that they shouldn't really have access to anything outside of their scope.
If you need some config option in one of your entities, consider passing it as a parameter from the controller like so:
$entityName->methodName($param1, $this->container->getParameter('xyz'));
This could (will) break DIC pattern, but you could use a singleton class to "globalize" what you need.
To feed your globals, use bootmethod from Bundle class (where you can access DIC stuff hence configuration).
Or more simple, add a static field to your Entity.
Quick & dirty solution, don't abuse it ;-)
You can use Dependency Injection and add your model to your services.yml file, and like every other service you make you can provide other services as constructor parameters. The only downside is you call $derp = $this->get("your_service_name"); instead of $derp = new Derp();.
For example:
# src/Derp/LolBundle/Resources/config/services.yml
services:
derp:
class: \Derp\LolBundle\Entity\Message
arguments: [#service_container]
#service_container is a service found using php app/console container:debug. It will function identically to $this->container in your controllers and it is provided to the constructor of your class. See here for more information on how to use service containers.
As previously mentioned they are POPOs (Plain Old PHP Objects) and the previous method of dependency injection is poor choice simply because you will have to remember to provide your model entity with the same object every time you use it (which is a hassle) and Symfony2 services are a way to mitigate that pain.