gnugat/pomm-foundation-bundle

symfony 中 pomm-project/foundation 集成

安装数: 10,883

依赖项: 0

建议者: 0

安全: 0

星标: 6

关注者: 2

分支: 1

开放问题: 0

类型:symfony-bundle

v0.7.0 2022-02-28 13:34 UTC

This package is auto-updated.

Last update: 2024-08-28 19:12:55 UTC


README

pomm-project/foundation 中集成 Symfony.

此包为您的数据库提供控制台命令,以及一个 Pomm 的 QueryManagerInterface 服务,以便在 Symfony 应用程序中执行 SQL 查询。

提供的 QueryManagerInterface 实现与一个连接绑定

  1. 第一次调用时,打开一个新的连接
  2. 后续调用时,使用现有的连接
  3. 如果调用 shutdown,关闭连接并回到步骤 1

这允许在 HTTP 请求和数据库连接之间保持 1 对 1 的关系,这对于长时间运行的应用程序(例如测试、FastCGI、AMQP 消费者等)非常重要。

它还提供了对以下参数的转换支持

  • 布尔值:从 false 转换为 'f',从 true 转换为 't'
  • \DateTime:字符串格式为 Y-m-d H:i:s T

集成

此包提供了以下内容

  • 服务 PommProject\Foundation\QueryManager\QueryManagerInterface
  • 事件监听器 Gnugat\PommFoundationBundle\EventListener\ClosingConnectionListener
  • 控制台命令
    • gnugat-pomm-foundation:database:check-existence (g:p:ch)
    • gnugat-pomm-foundation:database:close-connections (g:p:cl)
    • gnugat-pomm-foundation:database:create (g:p:cr)
    • gnugat-pomm-foundation:database:drop (g:p:dr)
    • gnugat-pomm-foundation:database:dump (g:p:du)
    • gnugat-pomm-foundation:database:execute-file (g:p:ex)
    • gnugat-pomm-foundation:database:launch-console (g:p:la)
    • gnugat-pomm-foundation:database:query (g:p:qu)

安装

首先提供数据库配置

# config/parameters.yaml.dist
parameters:
    database_host: 127.0.0.1
    database_port: 5432
    database_name: gnugat_pomm_foundation
    database_user: postgres
    database_password: ~

然后使用 Composer 安装 gnugat/pomm-foundation-bundle

composer require gnugat/pomm-foundation-bundle:^0.6

最后在您的 config/bundles.php 中添加以下内容

<?php

return [
    // Your other bundles...

    \Gnugat\PommFoundationBundle\GnugatPommFoundationBundle::class => ['all' => true],
];

用法

PommProject\Foundation\QueryManager\QueryManagerInterface 服务注入到您自己的服务中

<?php

use PommProject\Foundation\QueryManager\QueryManagerInterface;

class MyService
{
    private const NO_RESULTS = [];

    private $queryManager;

    public function __construct(QueryManagerInterface $queryManager)
    {
        $this->queryManager = $queryManager;
    }

    public function myMethod(int $id, bool $isDeleted): array
    {
        $results = iterator_to_array($this->queryManager->query(
            'SELECT * FROM my_table WHERE id = $* AND is_deleted = $*',
            [
                $id,
                $isDeleted,
            ]
        ));
        if (self::NO_RESULTS === $results) {
            throw new \Exception("Nothing found in my_table for ID \"$id\"");
        }

        return $results;
    }
}

进一步文档

您可以使用以下方式查看当前和过去的版本

您可以在以下链接中找到更多文档