【问题标题】:Why can I use entity manager in controller and not in Command?为什么我可以在控制器而不是命令中使用实体管理器?
【发布时间】:2018-07-02 09:50:49
【问题描述】:

我已经创建了一个用于在命令中获取实体管理器的服务

我不明白为什么我可以在常规控制器中使用实体管理器而不是在我的命令中。

我已经在“service.yml”中声明了我的服务

common.doctrine:
        class: AppBundle\Services\GetDoctrineService
        arguments: [ '@doctrine.orm.entity_manager' ]
        public: true

我写在“GetDoctrineService.php”中

<?php

namespace AppBundle\Services;

use Doctrine\ORM\EntityManager;

class GetDoctrineService
{
  protected $em;

  public function __construct(\Doctrine\ORM\EntityManager $em)
  {
    $this->em = $em;
  }

  public function getRepository(string $repo) {
      return $this->em->getRepository($repo);
  }
}

在我的命令中,我正在导入它并以“命令方式”调用它

$em = $this->getApplication()->getKernel()->getContainer()->get('common.doctrine');
$foo = $em->getRepository(Entity::FOO)->findAll();

我在“parameters.yml”中将 db 主机更改为“localhost

# This file is auto-generated during the composer install
parameters:
    #database_host: db-name
    database_host: localhost

当我使用命令安装 postgres 时,没有创建文件“pg_hba.conf”(我正在使用 Ubuntu)

我在使用命令时遇到此错误:

消息:“驱动程序中发生异常:SQLSTATE[08006] [7] 无法连接到服务器:连接被拒绝服务器是否在主机“localhost”(127.0.0.1) 上运行并接受 TCP/IP 连接在 5432 端口上?”

【问题讨论】:

  • 为什么不直接在你的命令中注入entitymanager,把它声明为服务呢? symfony.com/doc/3.4/console/commands_as_services.html。尽管如此,您的数据库似乎无法访问,它是否可以在您的常规代码中工作?
  • 我的命令不需要很复杂,它只用于一次用途。当我在控制器中使用实体管理器时,它可以完美运行
  • 你检查postgresql的错误日志了吗?
  • 我没有 postgres 日志当我尝试访问 repo Entity::FOO 时出现错误
  • 您应该在 /var/log/postgresql 中有日志文件?您提供的错误消息表明 localhost:5432 上没有运行任何服务,或者它不接受传入连接。所以首先你应该确保 Postgresql 运行正常。

标签: postgresql symfony ubuntu doctrine-orm


【解决方案1】:

问题出在配置文件中: 使用 'localhost' 是一个错误:我们应该使用主机的 dn 名称

【讨论】:

    猜你喜欢
    • 2011-11-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-25
    相关资源
    最近更新 更多