定制 oro-flex/doctrine-utils 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

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

oro-flex/doctrine-utils

Composer 安装命令:

composer require oro-flex/doctrine-utils

包简介

`Oro Doctrine Utils Component` provides some useful classes meant to make using Doctrine components easier.

README 文档

README

Oro Doctrine Utils Component provides some useful classes meant to make using Doctrine components easier.

QueryHintResolver class

The QueryHintResolver can be used to make Doctrine query hints more flexible.

Descriptions of the class methods are below:

  • addTreeWalker - Maps a query hint to a tree walker.
  • addOutputWalker - Maps a query hint to an output walker.
  • resolveHints - Resolves query hints.
  • addHint - Adds a hint to a query object.
  • addHints - Adds hints to a query object.

SqlWalker and TranslatableSqlWalker Output SQL Walkers

SqlWalker and TranslatableSqlWalker are Doctrine output walkers used to modify queries to be more vendor-optimized. TranslatableSqlWalker should be used instead of the Gedmo TranslationWalker.

Both walkers utilize DecoratedSqlWalkerTrait which is responsible for decoration of all SQL output walker calls with calls to OutputAstWalkerInterface before result building, and OutputResultModifierInterface after the result is ready. OutputAstWalkerInterface and OutputResultModifierInterface are added to the query with two hints HINT_AST_WALKERS and HINT_RESULT_MODIFIERS. These hints are automatically filled with an array of classes during container building. To add your own AST walker or Output Result Modifier to the decoration, use the oro_entity.sql_walker DI tag.

OutputAstWalkerInterface should be used to modify AST tree, but not to generate SQL. To change the resulting SQL, use OutputResultModifierInterface (it has access to AST but should not modify it).

oro_entity.sql_walker.union:
    class: Oro\Component\DoctrineUtils\ORM\Walker\UnionOutputResultModifier
    public: false
    abstract: true
    tags:
        - { name: oro_entity.sql_walker }

Out-of-the-box, there are several Output Result modifiers registered:

PostgreSqlOrderByNullsOutputResultModifier - NULLs Sorting

One of the class goals is to align NULLs sorting logic for MySQL and PostgreSQL. By default when ASC sorting is performed NULLs are sorted first in MySQL and last in PostgreSQL. SqlWalker applies NULLS FIRST instruction for nullable columns when them appears in ORDER BY clause. For DESC sorting NULLS LAST instruction is applied as well.

This behavior may be turned off by setting special query hint HINT_DISABLE_ORDER_BY_MODIFICATION_NULLS

$query->setHint('HINT_DISABLE_ORDER_BY_MODIFICATION_NULLS', true);

MySqlUseIndexOutputResultModifier - force to use index for MySQL

MySqlUseIndexOutputResultModifier provides functionality of specifying concrete indexes that should be used by MySQL. Indexes are fetched from query hint HINT_USE_INDEX

$query->setHint('HINT_USE_INDEX', 'my_custom_idx');

UnionOutputResultModifier - Union SELECT to a given query

UnionOutputResultModifier provides union functionality. Union is added to a placeholder which is saved under HINT_UNION_KEY. The value of UNION query is fetched from the HINT_UNION_VALUE

PreciseOrderByWalker class

The PreciseOrderByWalker is an Doctrine tree walker that is used to modify ORDER BY clause of a query to make sure that records will be returned in the same order independent from a state of SQL server and from values of OFFSET and LIMIT clauses. This is achieved by adding the primary key column of the first root entity to the end of ORDER BY clause.

Example of usage:

$query->setHint(Query::HINT_CUSTOM_TREE_WALKERS, [PreciseOrderByWalker::class]);

TransactionWatcherInterface interface

Sometimes it is required to perform some work only after data are committed to the database. For instance, sending notifications to users or to external systems. In this case the TransactionWatcherInterface can be helpful.

To be able to register DBAL transaction watchers you need to register the AddTransactionWatcherCompilerPass compiler pass and class loader for the transaction watcher aware connection proxy in your application, for example:

class AppBundle extends Bundle
{
    public function __construct(KernelInterface $kernel)
    {
        TransactionWatcherConfigurator::registerConnectionProxies($kernel->getCacheDir());
    }

    public function build(ContainerBuilder $container)
    {
        $container->addCompilerPass(
            new AddTransactionWatcherCompilerPass('oro.doctrine.connection.transaction_watcher')
        );
    }
}

QueryBuilderUtil class

Constructing DQL queries dynamically may make them vulnerable for injections. To be sure that data, passed ans field name or table alias is safe QueryBuilderUtil contains a set of methods: sprintf - should be as query safe replacement instead of sprintf checkIdentifier - when there is a need to safely pass variable into query as part of identifier getField - is a shortcut for QueryBuilderUtil::sprintf('%s.%s', $alias, $field)

oro-flex/doctrine-utils 适用场景与选型建议

oro-flex/doctrine-utils 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 746 次下载、GitHub Stars 达 0, 最近一次更新时间为 2022 年 01 月 08 日, 在 PHP 生态内属于活跃度较高的组件。

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

围绕 oro-flex/doctrine-utils 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2022-01-08