Why can I use entity manager in controller and not in Command?

Multi tool use
Why can I use entity manager in controller and not in Command?
I have created a service for getting the entity manager in a command
I don't understand why I can use the entity manager in regular Controller and not in my Command.
I have declared my service in "service.yml"
common.doctrine:
class: AppBundleServicesGetDoctrineService
arguments: [ '@doctrine.orm.entity_manager' ]
public: true
I wrote it in "GetDoctrineService.php"
<?php
namespace AppBundleServices;
use DoctrineORMEntityManager;
class GetDoctrineService
{
protected $em;
public function __construct(DoctrineORMEntityManager $em)
{
$this->em = $em;
}
public function getRepository(string $repo) {
return $this->em->getRepository($repo);
}
}
In my command, I am importing it and calling it in the "Command way"
$em = $this->getApplication()->getKernel()->getContainer()->get('common.doctrine');
$foo = $em->getRepository(Entity::FOO)->findAll();
I changed the db host to "localhost" in "parameters.yml"
# This file is auto-generated during the composer install
parameters:
#database_host: db-name
database_host: localhost
When I installed postgres with a command, the file "pg_hba.conf" wasn't created (I am working with Ubuntu)
I have this error while i use my command :
Message: "An exception occured in driver: SQLSTATE[08006] [7] could not connect to server: Connection refused Is the server running on host "localhost" (127.0.0.1) and accepting TCP/IP connections on port 5432?"
My command doesn't need to be sophisticated, it's for only one use. When I use entity manager in the controllers, it works perfectly
– JeanJacques
Jul 2 at 10:13
Did you check the error log of postgresql ?
– Jeroen
Jul 2 at 10:21
I don't have postgres logs The error come when I i am trying to access the repo Entity::FOO
– JeanJacques
Jul 2 at 11:16
You should have log files in /var/log/postgresql ? The error message you provided states that there is no service running on on localhost:5432 or that it is no accepting incoming connections. So first you should be sure Postgresql is running correctly.
– Jeroen
Jul 2 at 11:26
1 Answer
1
The problem was in the configuration files:
using 'localhost' was a mistake: we should have use the dn-name for the host
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
Why not directly inject the entitymanager in your command by declaring it as a service? symfony.com/doc/3.4/console/commands_as_services.html. Nevertheless it looks like your database is unreachable, does it work in your regular code ?
– Jeroen
Jul 2 at 10:07