zjkiza/sql-twig-bundle 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

zjkiza/sql-twig-bundle

Composer 安装命令:

composer require zjkiza/sql-twig-bundle

包简介

The bundle executes raw SQL queries with the flexibility to embed Twig extensions, enabling the dynamic creation of queries using Twig syntax.

README 文档

README

The bundle executes raw SQL queries with the flexibility to embed Twig extensions, enabling the dynamic creation of queries using Twig syntax.

About the bundle

  • You can use Twig syntax when creating queries.
  • You place queries in separate files. (Ex: all_media.sql.twig).
  • Execute your queries using Zjk\SqlTwig\Contract\SqlTwigInterface service.
  • Result of execution Zjk\SqlTwig\Contract\SqlTwigInterface->executeQuery(..) is instance of Doctrine\DBAL\Driver\Result, use their methods to get results.
  • Query execution via transaction Zjk\SqlTwig\Contract\SqlTwigInterface->transaction(..)
  • Create Zjk\SqlTwig\Contract\SqlTwigInterface->registerTempTable(..) and execute Zjk\SqlTwig\Contract\SqlTwigInterface->executeQueryWithTempTable(..) queries with temporary table.

Installation

Add "zjkiza/sql-twig-bundle" to your composer.json file:

composer require zjkiza/sql-twig-bundle

Symfony integration

Bundle wires up all classes together and provides method to easily setup.

  1. Register bundle within your configuration (i.e: bundles.php).

    <?php
    
    declare(strict_types=1);
    
    return [
        // other bundles
        Zjk\SqlTwig\ZJKizaSqlTwigBundle::class =>  ['all' => true],
    ];

Working with the bundle

It is necessary to define which directory/directories will be used for storing files with sql queries.

    twig:
      paths:
        '%kernel.project_dir%/src/sql/media': 'media'
        '%kernel.project_dir%/src/sql/expert': 'expert'

Create a sql query. Example (all_media.sql.twig):

   SELECT
       m.id
   {% if true == user  %}
       , u.name as user_name
   {% endif %}
   
   FROM media as m
   
       {% if true == user  %}
           INNER JOIN user as u ON m.user_id = u.id
       {% endif %}
   
   WHERE m.id in (:ids)
   
   ORDER BY m.id

Example (create_temp_table.sql.twig)

CREATE TEMPORARY TABLE IF NOT EXISTS tmp_table
select id from user
where id not in ('787e9568-0ebf-4cd1-825f-063ded2a6588');

Example (execute_with_tmp_table.sql.twig)

SELECT * FROM tmp_table
ORDER BY id ;

Working in php, Example:

namespace App\Example;

use Zjk\SqlTwig\Contract\SqlTwigInterface;

class MyRepository {
    
    private SqlTwigInterface $sqlTwig;
    
    public function __construct(SqlTwigInterface $sqlTwig) 
    {
        $this->sqlTwig = $sqlTwig;
    }
    
    public function allMedia(): array
    {
        return $this->sqlTwig->executeQuery('@media/all_media.sql.twig', [
                'user' => true,
                'ids' => ['60b16643-d5e0-468a-8823-499fcf07684a', '60b16643-d5e0-468a-8823-499fcf07684b'],
               ],[
                 'ids' => ArrayParameterType::STRING,
              ])->fetchAllAssociative()
    }
    
    public function withTransaction(): array
    {
        $result = $this->manager->transaction(
            static fn (SqlTwigInterface $manager): Result => $manager->executeQuery('@query/media_id_title.sql.twig'),
            TransactionIsolationLevel::READ_UNCOMMITTED
        );
        
        return $result->fetchAllAssociative();
    }    
    
    public function withTempTable(): void
    {
        $this->manager->registerTempTable('@query/create_temp_table.sql.twig');

        $response = $this->manager->executeQueryWithTempTable(
            '@query/execute_with_tmp_table.sql.twig'
        )
        
        return $result->fetchAllAssociative();
    }
}

Additional Notes

  • The $args parameter is passed directly to the Twig template and can be used as dynamic variables inside the SQL query.

  • The $types parameter defines Doctrine parameter types, compatible with ParameterType and ArrayParameterType.

  • The $queryPath argument follows the Twig namespace notation (e.g. @media/all_media.sql.twig).

  • The transaction() method allows query execution within a transaction context, supporting different isolation levels such as READ_COMMITTED, READ_UNCOMMITTED, etc.

  • When working with temporary tables, you must call registerTempTable() before executing executeQueryWithTempTable().

  • Each query execution returns a Doctrine\DBAL\Result instance, which provides methods like fetchAllAssociative(), fetchAssociative(), and others for result handling.

zjkiza/sql-twig-bundle 适用场景与选型建议

zjkiza/sql-twig-bundle 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 11 次下载、GitHub Stars 达 2, 最近一次更新时间为 2024 年 05 月 09 日, 在 PHP 生态内属于活跃度较高的组件。

我们在过去多个企业项目中使用过 zjkiza/sql-twig-bundle 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 zjkiza/sql-twig-bundle 我们能提供哪些服务?
定制开发 / 二次开发

基于 zjkiza/sql-twig-bundle 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 11
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 2
  • 点击次数: 12
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 2
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2024-05-09