I try to inject the symfony service container into a dcotrine dynamic connection wrapper_class
use Doctrine\DBAL\Connection;
class DynamicConnection extends Connection
{
public $container;
/**
* #required
* #param $container
*/
public function setContainer(ContainerInterface $container)
{
$this->container = $container;
}
}
I also tried to inject it with the service.yaml
App\Service\Database\DynamicConnection:
calls:
- [setContainer, ['#service_container']]
But this is also not working. How can i inject the service container here?
My goal here is to get a variable of the service container:
$this->container->get('my.string.variable')
You can do this by adding a CompilerPass. For simple CompilerPass, you can add it directly in your application Kernel class by implementing CompilerPassInterface:
class Kernel extends BaseKernel implements CompilerPassInterface
{
use MicroKernelTrait;
...
public function process(ContainerBuilder $container)
{
$container
->getDefinition('doctrine.dbal.default_connection')
->addMethodCall('setContainer', [
new Reference('service_container')
]);
}
}
Note however that as mentioned by other users, this is not a very good practice. You should inject what you need precisely instead of Container service.
Related
I have a repository class called EmailRepository
class EmailRepository extends EntityRepository implements ContainerAwareInterface { ... }
I need to get a parameter injected into this repository class but I dont know how...
This is what I currently have inside of the repository, which is being called from my controller:
Controller:
$em->getRepository(Email::class)->getEmailApi();
Repository
class EmailRepository extends EntityRepository implements ContainerAwareInterface {
protected $container;
public function setContainer(ContainerInterface $container = null) {
$this->container = $container;
}
/**
* #param $array
*/
public function getEmailApi($array)
{
echo $this->container->getParameter('email_api');
}
}
I always get this error:
Call to a member function getParameter() on null
The parameter is not null, it does have a value. I know it's telling me that $this->container is null. How do I fix this?
If I run this inside of my controller, it works fine and returns Google
echo $this->getParameter('email_api');
Inject container not a good idea. Try this
services.yaml
App\Repository\EmailRepository:
arguments:
$emailApi: '%env(EMAIL_API)%'
Repository
class EmailRepository
{
protected $emailApi;
public function __construct(string $emailApi)
{
$this->emailApi = $emailApi;
}
/**
* #param $array
*/
public function getEmailApi($array)
{
return $this->emailApi;
}
}
Or via setter injection
services.yaml
App\Repository\EmailRepository:
calls:
- method: setEmailApi
arguments:
$emailApi: '%env(EMAIL_API)%'
Repository
class EmailRepository extends EntityRepository implements ContainerAwareInterface
{
protected $emailApi;
public function setEmailApi(string $emailApi)
{
$this->emailApi = $emailApi;
}
/**
* #param $array
*/
public function getEmailApi($array)
{
return $this->emailApi;
}
}
Your original code is not going to work because there is nothing calling EmailRepository::setContainer. Furthermore, using ContainerAware and injecting the full container is discouraged.
Fortunately, the Doctrine bundle has a new base repository class that the entity manager can use to pull the repository from container and allow you to inject additional dependencies as needed. Something like:
namespace App\Repository;
use App\Entity\Email;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Symfony\Bridge\Doctrine\RegistryInterface;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
class EmailRepository extends ServiceEntityRepository // Different class to extend from
{
private $emailApi;
public function __construct(RegistryInterface $registry, ParameterBagInterface $parameterBag)
{
parent::__construct($registry, Email::class);
$this->emailApi = $parameterBag->get('email_api');
}
So in this case we inject all the parameters and then store the ones we need.
Even injecting the parameter bag is a bit frowned upon. Better to inject individual parameters though this takes just a bit more configuration as we need to use services.yaml to explicitly inject the needed parameters:
public function __construct(RegistryInterface $registry, string $emailApi)
{
parent::__construct($registry, Email::class);
$this->emailApi = $emailApi;
}
#services.yaml
App\Repository\EmailRepository:
$emailApi: 'email_api_value'
I am trying to inject public services like entityManager in the constructor of a service I created but I keep having this error :
Too few arguments to function App\Services\BillingInterface::__construct(), 0 passed in /var/www/.../src/Controller/TestController.php on line 144 and exactly 1 expected.
In my controllers, services are correctly injected in different methods but in the service I created it's not injected in the constructor.
I didn't change anything in the services.yaml as the documentation says autowire is automatic in Symfony 4.2
PS : I recently updated from Symfony 4.1 to 4.2 and I'm not sure but I think it worked before.
Maybe a library didn't updated correctly but I don't find any errors.
Here are the informations for the service
Service code :
#/src/Services/BillingInterface
namespace App\Services;
use Doctrine\ORM\EntityManagerInterface;
class BillingInterface {
private $em;
public function __construct(EntityManagerInterface $entityManager)
{
$this->em = $entityManager;
}
}
Controller code :
namespace App\Controller;
use App\Services\BillingInterface;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
class TestController extends AbstractController {
public function teest(EntityManagerInterface $entityManager)
{
$billing = new BillingInterface();
}
}
And If I instantiate BillingInterface with $entityManager parameter of Controller, it works but I would like it injected directly in the BillingInterface class constructor.
And finally, here is what is written in Symfony's documentation :
// src/Service/MessageGenerator.php
// ...
use Psr\Log\LoggerInterface;
class MessageGenerator
{
private $logger;
public function __construct(LoggerInterface $logger)
{
$this->logger = $logger;
}
public function getHappyMessage()
{
$this->logger->info('About to find a happy message!');
// ...
}
}
Link : https://symfony.com/doc/current/service_container.html
Chapter : Injecting Services/Config into a Service
So, I don't know what's wrong with my Service.
Thank you for your answers.
Since your BillingInterface is a service - you need to use its instance that is provided by Symfony container instead of attempting to instantiate it by yourself. Your controller needs to inject this service in order to be able to use it:
namespace App\Controller;
use App\Services\BillingInterface;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
class TestController extends AbstractController
{
/**
* #var BillingInterface
*/
private $billing;
/**
* #param BillingInterface $billing
*/
public function __construct(BillingInterface $billing)
{
$this->billing = $billing;
}
public function teest(EntityManagerInterface $entityManager)
{
// Use $this->billing ...
}
}
I have custom Service and I would like to use it in Twig templates.
In Symfony < 3 I can do:
use Symfony\Component\DependencyInjection\Container;
//...
public function __construct(Container $container)
{
$this->container = $container;
}
public function getView()
{
$this->container->get('templating')->render('default/view.html.twig');
}
But in Symfony 3.3 I have error:
Cannot autowire service "AppBundle\Service\ViewService": argument
"$container" of method "__construct()" references class
"Symfony\Component\DependencyInjection\Container" but no such service
exists. Try changing the type-hint to one of its parents: interface
"Psr\Container\ContainerInterface", or interface
"Symfony\Component\DependencyInjection\ContainerInterface".
It's not good idea to inject whole container. Better is to inject single dependencies:
use Symfony\Bundle\FrameworkBundle\Templating\EngineInterface;
class MyService
{
private $templating;
public function __construct(EngineInterface $templating)
{
$this->templating = $templating;
}
public function getView()
{
$this->templating->render('default/view.html.twig');
}
}
I working on a web socket app using ratchet and symfony 2.8 to connect to database and changing value in a certain column if someone connect to the server but I get error in this line
$sql = $this->container->get('database_connection');
the full error message
An error has occurred: Notice: Undefined property: check\roomsBundle\Sockets\Chat::$container
my injection in the services.yml code
services:
database_connection:
class: check\roomsBundle\Sockets\Chat
arguments: ["#service_container"]
my Chat.php code
<?php
namespace check\roomsBundle\Sockets;
use tuto\testBundle\Entity\Users;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
use Ratchet\MessageComponentInterface;
use Ratchet\ConnectionInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class Chat implements MessageComponentInterface {
protected $clients;
//protected $db;
public function __construct() {
$this->clients = new \SplObjectStorage;
}
public function onOpen(ConnectionInterface $conn) {
$this->clients->attach($conn);
echo "New connection! ({$conn->resourceId})\n";
$sql = $this->container->get('database_connection');
$users = $sql->query("UPDATE user SET ONoff= '1' WHERE UserId='2'");
}
}
Ok so there's a few things that you'll need to fix in order to solve your problem.
services:
database_connection:
class: check\roomsBundle\Sockets\Chat
arguments: ["#service_container"]
What this is doing is when it calls the constructor it's going to pass in the service container, however using the constructor to pass in your container isn't favorable, but rather instead you should implement the Symfony\Component\DependencyInjection\ContainerAwareInterface interface and then implement the method setContainer and optional a getContainer method.
/**
* #param ContainerInterface|NULL $container
*/
public function setContainer(
ContainerInterface $container = NULL
)
{
$this->container = $container;
return $this;
}
/**
* #return ContainerInterface
*/
protected function getContainer()
{
return $this->container;
}
And then after that update your service to call this method when initializing it.
services:
chat_service: # renamed because this is your chat service, not your database connection
class: check\roomsBundle\Sockets\Chat
calls:
- [setContainer, ["#service_container"]]
Your service is fine you just need little changes in your chat.php class
use Symfony\Component\DependencyInjection\ContainerInterface as Container;
class Chat implements MessageComponentInterface {
protected $clients;
private $container;
//protected $db;
public function __construct(Container $container) {
$this->clients = new \SplObjectStorage;
$this->container = $container;
}
Now u can use $this->container
Updated
Try to inject entity manager
services:
database_connection:
class: check\roomsBundle\Sockets\Chat
arguments:
- #doctrine.orm.default_entity_manager
in chat.php do like this
use Doctrine\ORM\EntityManager;
class Chat implements MessageComponentInterface {
protected $clients;
protected $em;
public function __construct(EntityManager $em) {
$this->clients = new \SplObjectStorage;
$this->em = $em;
}
$this->em->getRepository('yorrepo')->updateFuntion();
Now try to call from some repo to update
Injecting the service container is generally considered as a bad idea.
you should consider to inject database_connection service .
There are few ways to do that. Have a look at Types of Injection.
services:
chat_service:
class: check\roomsBundle\Sockets\Chat
arguments: ["#database_connection"]
your class
protected $connection;
public function __construct($connection) {
$this->connection = $connection;
}
Even if you want to go ahead with injecting the service container, the above link has relavent documentation that will help you to solve the issue you are facing.
I've created my own service and I need to inject doctrine EntityManager, but I don't see that __construct() is called on my service, and injection doesn't work.
Here is the code and configs:
<?php
namespace Test\CommonBundle\Services;
use Doctrine\ORM\EntityManager;
class UserService {
/**
*
* #var EntityManager
*/
protected $em;
public function __constructor(EntityManager $entityManager)
{
var_dump($entityManager);
exit(); // I've never saw it happen, looks like constructor never called
$this->em = $entityManager;
}
public function getUser($userId){
var_dump($this->em ); // outputs null
}
}
Here is services.yml in my bundle
services:
test.common.userservice:
class: Test\CommonBundle\Services\UserService
arguments:
entityManager: "#doctrine.orm.entity_manager"
I've imported that .yml in config.yml in my app like that
imports:
# a few lines skipped, not relevant here, i think
- { resource: "#TestCommonBundle/Resources/config/services.yml" }
And when I call service in controller
$userservice = $this->get('test.common.userservice');
$userservice->getUser(123);
I get an object (not null), but $this->em in UserService is null, and as I already mentioned, constructor on UserService has never been called
One more thing, Controller and UserService are in different bundles (I really need that to keep project organized), but still: everyting else works fine, I can even call
$this->get('doctrine.orm.entity_manager')
in same controller that I use to get UserService and get valid (not null) EntityManager object.
Look like that I'm missing piece of configuration or some link between UserService and Doctrine config.
Your class's constructor method should be called __construct(), not __constructor():
public function __construct(EntityManager $entityManager)
{
$this->em = $entityManager;
}
For modern reference, in Symfony 2.4+, you cannot name the arguments for the Constructor Injection method anymore. According to the documentation You would pass in:
services:
test.common.userservice:
class: Test\CommonBundle\Services\UserService
arguments: [ "#doctrine.orm.entity_manager" ]
And then they would be available in the order they were listed via the arguments (if there are more than 1).
public function __construct(EntityManager $entityManager) {
$this->em = $entityManager;
}
Note as of Symfony 3.3 EntityManager is depreciated. Use EntityManagerInterface instead.
namespace AppBundle\Service;
use Doctrine\ORM\EntityManagerInterface;
class Someclass {
protected $em;
public function __construct(EntityManagerInterface $entityManager)
{
$this->em = $entityManager;
}
public function somefunction() {
$em = $this->em;
...
}
}
Since 2017 and Symfony 3.3 you can register Repository as service, with all its advantages it has.
Check my post How to use Repository with Doctrine as Service in Symfony for more general description.
To your specific case, original code with tuning would look like this:
1. Use in your services or Controller
<?php
namespace Test\CommonBundle\Services;
use Doctrine\ORM\EntityManagerInterface;
class UserService
{
private $userRepository;
// use custom repository over direct use of EntityManager
// see step 2
public function __constructor(UserRepository $userRepository)
{
$this->userRepository = $userRepository;
}
public function getUser($userId)
{
return $this->userRepository->find($userId);
}
}
2. Create new custom repository
<?php
namespace Test\CommonBundle\Repository;
use Doctrine\ORM\EntityManagerInterface;
class UserRepository
{
private $repository;
public function __construct(EntityManagerInterface $entityManager)
{
$this->repository = $entityManager->getRepository(UserEntity::class);
}
public function find($userId)
{
return $this->repository->find($userId);
}
}
3. Register services
# app/config/services.yml
services:
_defaults:
autowire: true
Test\CommonBundle\:
resource: ../../Test/CommonBundle