在 Heroku 上打开 Symfony 应用程序时出现 SQLite 错误

时间:2021-07-21 00:19:35

标签: php sqlite symfony 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
            )
        );
    }
}

我已将“pdo_sqlite”和“sqlite3”扩展名添加到“composer.json”。 Composer 更新运行没有问题。

在将本地项目存储库推送到 Heroku 之前,我同时提交了“composer.json”和“composer.lock”,Heroku 运行也没有问题,并显示添加了两个扩展。

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 不是生产数据库的正确选择,我正在学习一门课程,我想继续使用它提供的内容。

在此先感谢您的帮助!

1 个答案:

答案 0 :(得分:1)

正如评论中所提到的,问题是学说/缓存的弃用。我切换到 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
                    )
                );
            }
        }