SQLite 在 Heroku 上打开 Symfony 应用程序时出错
SQLite error opening Symfony app on Heroku
错误本身:
2021-07-20T23:43:33.993462+00:00 app[web.1]: [20-Jul-2021 23:43:33 UTC] [critical] Uncaught PHP Exception Symfony\Component\ErrorHandler\Error\ClassNotFoundError: "Attempted to load class "SQLite3Cache" from namespace "Doctrine\Common\Cache".
2021-07-20T23:43:33.993688+00:00 app[web.1]: Did you forget a "use" statement for another namespace?" at /app/src/Utils/FilesCache.php line 23
“FilesCache.php”的文件内容与 Symfony 文档here中提供的内容相似,但有一些补充。
<?php
namespace App\Utils;
use App\Utils\Interfaces\CacheInterface;
use Symfony\Component\Cache\Adapter\FilesystemAdapter;
use Symfony\Component\Cache\Adapter\TagAwareAdapter;
use Doctrine\Common\Cache\CacheProvider;
use Doctrine\Common\Cache\SQLite3Cache;
use Symfony\Component\Cache\Adapter\DoctrineAdapter;
class FilesCache implements CacheInterface
{
public $cache;
public function __construct()
{
//this is error line 23
$provider = new SQLite3Cache(new \SQLite3(__DIR__ . '/cache/data.db'), 'TableName');
$this->cache = new TagAwareAdapter(
new DoctrineAdapter(
$provider,
$namespace = '',
$defaultLifetime = 0
)
);
}
}
我已经为“composer.json”添加了“pdo_sqlite”和“sqlite3”扩展。
Composer 更新运行没有问题。
在将本地项目回购推送到 Heroku 之前,我同时提交了“composer.json”和“composer.lock”,它运行也没有问题,并显示两个扩展都已添加。
remote: -----> Installing platform packages...
remote: - php (8.0.8)
remote: - ext-intl (bundled with php)
remote: - ext-pdo_sqlite (bundled with php)
remote: - ext-sqlite3 (bundled with php)
remote: - composer (2.1.3)
remote: - apache (2.4.48)
remote: - nginx (1.20.1)
我知道 SQLite 不是生产数据库的正确选择,我正在学习一门课程,我想继续使用它提供的内容。
提前感谢您的帮助!
如评论中所述,问题在于 doctrine/cache 的弃用。我切换到 PDOAdapter,这解决了问题。
<?php
namespace App\Utils;
use App\Utils\Interfaces\CacheInterface;
use Symfony\Component\Cache\Adapter\TagAwareAdapter;
use Symfony\Component\Cache\Adapter\PdoAdapter;
use Doctrine\DBAL\Driver\Connection;
class FilesCache implements CacheInterface
{
public $cache;
public function __construct()
{
$connection = \Doctrine\DBAL\DriverManager::getConnection([
'url' => 'sqlite:////%kernel.project_dir%/var/cache/data.db'
]);
$this->cache = new TagAwareAdapter(
new PdoAdapter(
$connection,
$namespace = '',
$defaultLifetime = 0
)
);
}
}
错误本身:
2021-07-20T23:43:33.993462+00:00 app[web.1]: [20-Jul-2021 23:43:33 UTC] [critical] Uncaught PHP Exception Symfony\Component\ErrorHandler\Error\ClassNotFoundError: "Attempted to load class "SQLite3Cache" from namespace "Doctrine\Common\Cache".
2021-07-20T23:43:33.993688+00:00 app[web.1]: Did you forget a "use" statement for another namespace?" at /app/src/Utils/FilesCache.php line 23
“FilesCache.php”的文件内容与 Symfony 文档here中提供的内容相似,但有一些补充。
<?php
namespace App\Utils;
use App\Utils\Interfaces\CacheInterface;
use Symfony\Component\Cache\Adapter\FilesystemAdapter;
use Symfony\Component\Cache\Adapter\TagAwareAdapter;
use Doctrine\Common\Cache\CacheProvider;
use Doctrine\Common\Cache\SQLite3Cache;
use Symfony\Component\Cache\Adapter\DoctrineAdapter;
class FilesCache implements CacheInterface
{
public $cache;
public function __construct()
{
//this is error line 23
$provider = new SQLite3Cache(new \SQLite3(__DIR__ . '/cache/data.db'), 'TableName');
$this->cache = new TagAwareAdapter(
new DoctrineAdapter(
$provider,
$namespace = '',
$defaultLifetime = 0
)
);
}
}
我已经为“composer.json”添加了“pdo_sqlite”和“sqlite3”扩展。 Composer 更新运行没有问题。
在将本地项目回购推送到 Heroku 之前,我同时提交了“composer.json”和“composer.lock”,它运行也没有问题,并显示两个扩展都已添加。
remote: -----> Installing platform packages...
remote: - php (8.0.8)
remote: - ext-intl (bundled with php)
remote: - ext-pdo_sqlite (bundled with php)
remote: - ext-sqlite3 (bundled with php)
remote: - composer (2.1.3)
remote: - apache (2.4.48)
remote: - nginx (1.20.1)
我知道 SQLite 不是生产数据库的正确选择,我正在学习一门课程,我想继续使用它提供的内容。
提前感谢您的帮助!
如评论中所述,问题在于 doctrine/cache 的弃用。我切换到 PDOAdapter,这解决了问题。
<?php
namespace App\Utils;
use App\Utils\Interfaces\CacheInterface;
use Symfony\Component\Cache\Adapter\TagAwareAdapter;
use Symfony\Component\Cache\Adapter\PdoAdapter;
use Doctrine\DBAL\Driver\Connection;
class FilesCache implements CacheInterface
{
public $cache;
public function __construct()
{
$connection = \Doctrine\DBAL\DriverManager::getConnection([
'url' => 'sqlite:////%kernel.project_dir%/var/cache/data.db'
]);
$this->cache = new TagAwareAdapter(
new PdoAdapter(
$connection,
$namespace = '',
$defaultLifetime = 0
)
);
}
}