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.
Related
I have an error on a symfony 3.4 project.
I'm trying to manage the display of a notification in the menu of my application.
So I created a CustomController which extends Controller.
Then I made all my other controllers inherit from CustomController.
But when I make a call to getDoctrine() to reach a repository I get the following error:
"Call to a member function has() on null"
Here is my CustomController:
<?php
namespace AppBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
class CustomController extends Controller
{
public $data = [];
protected $em;
public function __construct()
{
$this->em = $this->getDoctrine()->getManager();
$countAttente = $this->em->getRepository('AppBundle:Commandes')->tailleEnAttente("En attente");
$this->data['countAttente'] = $countAttente;
}
}
I tried to pass the controller as a service in service.yml but it did not change anything
AppBundle\Controller\CustomController:
class: AppBundle\Controller\CustomController
arguments: ["#doctrine.orm.entity_manager"]
calls:
- [setContainer, ["#service_container"]]
I found many similar topics on this type of error but none of them allowed me to skip this error
Any help is welcome
Autowire your EntityManager directly inside your constructor:
use Doctrine\ORM\EntityManagerInterface;
private $em;
public function __construct(EntityManagerInterface $em)
{
$this->em = $em;
}
Or if you need a specific repository, and autowired is set up with the default configuration you can do the same as well with the repository:
private $repository;
public function __construct(CommandesRepository $repository)
{
$this->repository = $repository;
}
I'm new in Symfony,
I try to use Dependency injection to get User in a service (i think)
services.yaml :
App\Service\Test\RESTAuthenticatedService:
calls:
- method: getTrigramme
arguments:
- '#security.token_storage'
In my RESTAuthenticatedService.php :
namespace App\Service\Test;
....
class RESTAuthenticatedService extends AbstractController {
protected $session;
private $user;
....
public function getTrigramme(){
$user = $this->token_storage->getToken()->getUser();
ERROR :
Notice: Undefined property: App\Service\Test\PrestataireService::$token_storage
Can you help me please ?
Ok, first thanks everyone now I try what you said and i have this error :
Too few arguments to function App\Service\Test\ClientService::__construct(), 0 passed in D:\www\Interface_SAT\src\Controller\RecherchePrestataires.php on line 60 and exactly 2 expected
In my Controller RecherchePrestataires.php I have :
.....
public function rechercher(Request $request) {
....
$recherchePresta = new PrestataireService();
In the file class PrestataireService I just have :
class PrestataireService extends ClientService {
In ClientService :
use Symfony\Component\HttpFoundation\Session\SessionInterface;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
class ClientService extends RESTAuthenticatedService
{
public $user;
public function __construct(SessionInterface $session, TokenStorageInterface $tokenStorage)
{
parent::__construct($session, $tokenStorage);
$this->setSession($session);
}
And in RESTAuthenticatedService : I've done :
public function __construct(SessionInterface $session, TokenStorageInterface $tokenStorage)
{
$this->token_storage = $tokenStorage;
Sorry , but i try so many things.
This seems that you didn't created the constructor in the class.
You have defined at your services.yaml that your class has a dependency, but you haven't done anything with that dependcy. You need to create the constructor and add the dependency as a parameter, and assign to a local variable.
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
class RESTAuthenticatedService extends AbstractController
/**
* #var TokenStorageInterface
*/
private $token_storage;
public function __construct(TokenStorageInterface $tokenStorage)
{
$this->token_storage = $tokenStorage;
}
}
Then you will be able to access $this->token_storage.
Edit: Change your services.yaml to inject the depenency in the constructor.
App\Service\Test\RESTAuthenticatedService:
class: App\Service\Test\RESTAuthenticatedService
arguments:
- '#security.token_storage'
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 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.
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