0

我在一个项目中使用学说(不是 symfony)。在这个项目中,我还使用了 phpstan,我同时安装了phpstan/phpstan-doctrinephpstan/extension-installer. 我的 phpstan.neon 是这样的:

parameters:
    level: 8
    paths:
        - src/
    doctrine:
        objectManagerLoader: tests/object-manager.php

在 tests/object-manager.php 中,它返回调用返回实体管理器的函数的结果。

这是创建实体管理器的代码

$database_url = $_ENV['DATABASE_URL'];
$isDevMode = $this->isDevMode();
$proxyDir = null;
$cache = null;
$useSimpleAnnotationReader = false;

$config = Setup::createAnnotationMetadataConfiguration(
    [$this->getProjectDirectory() . '/src'],
    $isDevMode,
    $proxyDir,
    $cache,
    $useSimpleAnnotationReader
);


// database configuration parameters
$conn = [
    'url' => $database_url,
];

// obtaining the entity manager
$entityManager = EntityManager::create($conn, $config);

当我运行时,vendor/bin/phpstan analyze我收到此错误:

Internal error: An exception occurred in the driver: SQLSTATE[08006] [7] could not translate host name "postgres_db" to address: nodename nor servname provided, or not known

这是因为我正在使用 docker,而我的数据库 url 是postgres://user:password@postgres_db/database postgres_db我的数据库容器的名称,因此主机名在 docker 容器内是已知的。

当我在容器内运行 phpstan 时,我没有错误。

那么有没有办法在 docker 之外运行 phpstan ?因为我很确定当我推送我的代码时,github 工作流程会因此而失败

phpstan 需要尝试访问数据库吗?

4

1 回答 1

0

我在 phpstan-doctrine 的 github 上打开了一个问题,我得到了@jlherren 的回答,解释说:

问题是 Doctrine 需要知道它正在使用的数据库服务器的版本以便实例化正确的 AbstractPlatform 实现,其中有几个可用于同一数据库供应商(例如 PostgreSQL94Platform 或 PostgreSQL100Platform 用于 postgres,类似地用于其他数据库驱动程序)。要自动检测此信息,它将简单地连接到数据库并查询版本。

我刚刚更改了我的数据库网址:

DATABASE_URL=postgres://user:password@database_ip/database

至:

DATABASE_URL=postgres://user:password@database_ip/database?serverVersion=14.2
于 2022-02-21T14:57:11.343 回答