无法使用docker和symfony 7在postgresql数据库中插入数据

问题描述 投票:0回答:1

我在docker上有Symfony postgresql,安装了数据库, 使用 adminer 作为数据库的 ui。

作为实体(类/模型),我有三个表:User、UserProfile 和 PostEvent。

在主页上进行测试时,其定义为路由:“/”。我创建了一个方法,当到达主页时,它将创建一个我创建的默认用户并将其发送到要插入的数据库。

class MainController extends AbstractController
{

    #[Route('/', name : "new_app")]
    public function index(EntityManager $manager): Response
    // public function index(): Response
    {
        $user = new User();
        $user->setEmail('[email protected]');
        $user->setPassword('12345678');

        $profile = new PostEvent();
        $profile->setTitle("Test Data");
        $profile->setDescription("Data testing here!");

        $email = $user->getEmail();
        // $prf_id = $profile->getUserUniq();
        
        $manager->persist($profile);
        $manager->persist($user);
        $manager->flush();

        // return new Response($profiles, 200);
        return $this->render(
            'main/index.html.twig',
            [
                'user' => $email,
                'profile_id' => $profile->getDescription()
            ]
        );
    }

如代码块中所示,我用 twig 渲染它,还安装了一个探查器包,用于显示页面上正在运行/执行的查询。 一切看起来都很好,页面上的状态代码为 200,在探查器应用程序中,查询已处理并“已提交”。

在数据库中,PostEvent 表仍然是空的, 还尝试在其他实体 User 和 UserProfile 中插入数据,但这也不起作用。

我尝试配置PostEvent和UserProfile的实体类。 当我创建这些实体时,我将它们创建为可为空, 我认为如果我让它们不可为空,它会插入数据, 这没用。

然后在“var/log/dev.log”文件中显示以下内容:

- php.DEBUG: Warning: filemtime(): stat failed for /app/vendor/symfony/validator/Resources/translations/validators.ro.xlf [] []       <- X1000

- doctrine.INFO: Connecting with parameters array{"use_savepoints":true,"driver":"pdo_pgsql","host":"database","port":5432,"user":"test","password":"<redacted>","driverOptions":[],"defaultTableOptions":[],"dbname":"martial_arts_db","serverVersion":"16","charset":"utf8"} {"params":{"use_savepoints":true,"driver":"pdo_pgsql","host":"database","port":5432,"user":"test","password":"<redacted>","driverOptions":[],"defaultTableOptions":[],"dbname":"martial_arts_db","serverVersion":"16","charset":"utf8"}} []

- doctrine.DEBUG: Executing query: SELECT 1 {"sql":"SELECT 1"} []

- doctrine.INFO: Disconnecting [] []

- deprecation.INFO: User Deprecated: Relying on non-optimal defaults for ID generation is deprecated, and IDENTITY results in SERIAL, which is not recommended. Instead, configure identifier generation strategies explicitly through configuration. We currently recommend "SEQUENCE" for "Doctrine\DBAL\Platforms\PostgreSQLPlatform", when using DBAL 3, and "IDENTITY" when using DBAL 4, so you should probably use the following configuration before upgrading to DBAL 4, and remove it after deploying that upgrade:  $configuration->setIdentityGenerationPreferences([     "Doctrine\DBAL\Platforms\PostgreSQLPlatform" => ClassMetadata::GENERATOR_TYPE_SEQUENCE, ]); 

我不确定 PostEvent 未在数据库中插入数据的问题是什么,但我确实看到用户类可能已被弃用。我用谷歌搜索了这个并看到了使用“composer update”的可能解决方案,我尝试了这个并清除应用程序缓存并清理docker卷,图像,容器。 Unfotunatley 它没有改变插入数据的问题

我真的很高兴能参与这个项目,也是我的第一个 symfony 和 postgresqk。 所以我需要帮助。

还有什么问题请追问?

github:linkToProjectGitHubPage

更新:

我编辑了 docker-compose.yaml

我确实发现我是否使用以下命令:

symfony console dbal:run-sql 'SELECT * FROM post_event'

它确实在数据库中显示了一些内容......

我发布的数据:D .....

id | title    | description | created
- - - - - - - - - - - - - - - - - - -
1 | Test Data | Data testing here! 
- - - - - - - - - - - - - - - - - - - 
2 | Test Data | Data testing here!

但是为什么我在数据库中看不到它。我在管理面板中的模式“公共”中没有看到它有人有线索吗?

但是当我搜索用户命令时:

symfony console dbal:run-sql 'SELECT * FROM user'

我看到一列,有两行,包含第一个“用户”和第二行“测试”

 user (column name)
 - - - - - - - - - - 
 test (1)

我没有看到电子邮件和密码,但它看起来像我用来登录管理面板的用户。

我现在正在寻找如何解决这个问题,我可能必须自定义创建 User,因为在日志中..我不断看到 User 类已被弃用。

但还是很奇怪,我在数据库中看不到数据。

我还更改了以下内容:

database:
  volumes:
    - db: /var/lib/mysql/data:rw

至:

database:
  volumes:
    - db: /var/lib/postgresql/data:rw

当我在浏览器中打开数据库端口时

http://localhost:5432/
。 它表明该页面无法正常工作,并且在资源选项卡中显示
failed error empty response
。 当我进入管理面板 url-port
http://localhost:8000/
我可以看到管理面板。不确定数据库 url 路径是否不给出响应。

我希望能够创建(、更新、读取和删除)数据并在管理面板或其他数据库管理面板(docker 容器)中查看它。并继续我正在从事的项目。

postgresql docker symfony
1个回答
0
投票

我不明白为什么我在管理面板上看不到数据。

但是我已经连接了我的视觉。 code IDE,登录后安装

vs code extension: Database Client
到数据库就可以看到数据了。

我通过插入带有“数据库客户端扩展”的新行来测试数据库连接,并且它有效。一切都是可见且可管理的。

所以我将继续,不使用管理员来管理数据。

我要感谢 @ArleighHix 的努力支持和其他人的关注。

© www.soinside.com 2019 - 2024. All rights reserved.