endeavourplatforms/qbjs-parser-bundle

此包是 endeavourplatforms/qbjs-parser 的 Symfony 封装。

1.0.3 2023-03-22 10:37 UTC

This package is auto-updated.

Last update: 2024-09-21 15:33:02 UTC


README

StyleCI GitHub license

这是一个 Symfony 包,可以与 jQuery QueryBuilder 一起使用。

  • 它将 解析来自前端的前端 JSON,并将其作为 Doctrine ORM 查询执行
  • 它将 基于您的 Doctrine ORM 实体和配置生成前端 JSON
  • 它基于 QBJSParser,也可以在没有 Symfony 的情况下使用。

它有两个有用的服务

  • fl_qbjs_parser.json_query_parser.doctrine_orm_parser 基于 FL\QBJSParserBundle\Service\JsonQueryParser\DoctrineORMParser
    • 此服务将解析来自 JQuery QueryBuilder 的 $jsonString$className,并将其解析为 FL\QBJSParser\Parsed\Doctrine\ParsedRuleGroup
    • ParsedRuleGroup 有两个属性,$dqlString$parameters,可通过 getter 访问。
    • 使用 ParsedRuleGroup 属性来创建 Doctrine 查询。
    • 此服务与 DoctrineORM 一起使用。
    • 此服务实现了 JsonQueryParserInterface。可以为其他 ORM/ODM 实现更多解析器。
  • fl_qbjs_parser.builders 基于 FL\QBJSParserBundle\Service\JavascriptBuilders
    • 使用服务的 getBuilders() 方法获取一个 FL\QBJSParserBundle\Model\Builder\Builder 实例数组。
    • 每个 Builder 都有五个属性,可通过 getter 访问,分别是 $className$jsonString$humanReadableName$builderId$resultColumns
    • 使用 Builder 的属性来在您的客户端中实例化 JQuery Query Builder。

安装

  • composer require fourlabs/qbjs-parser-bundle
  • 将包添加到 app/AppKernel.php
<?php

    //...
    $bundles = [
        //...
        new FL\QBJSParserBundle\FLQBJSParserBundle(),
    ];
  • 设置配置,详情如下。

配置示例

fl_qbjs_parser:
    builders: # these are used for service fl_qbjs_parser.builders
        product_report_builder:
            class: AppBundle\Entity\Product # this class must exist in doctrine_class_and_mappings
            human_readable_name: 'Product Report Builder'
            # result_columns
            # Not being used inside the bundle, but you can use them in your own way 
            # Make sure not to use OnetoMany or ManyToMany properties here. That makes no sense!
            # I.e. You can use direct properties of the class, ManyToOne, and OneToOne properties.
            result_columns: 
                -
                    column_machine_name: id
                    column_human_readable_name: ID
                -
                    column_machine_name: period.startDate
                    column_human_readable_name: Interview Start
                -
                    column_machine_name: period.endDate
                    column_human_readable_name: Interview End
            filters:
                -
                    id: specification.description
                    label: 'Product Specification: Description'
                    type: string # string, integer, double, date, time, datetime, boolean
                    # omit operators and get sensible defaults
                    # string operators [equal, not_equal, is_null, is_not_null,begins_with, not_begins_with, contains, not_contains, ends_with, not_ends_with, is_empty, is_not_empty]
                    # numeric/date operators [equal, not_equal, is_null, is_not_null, less, less_or_equal, greater, greater_or_equal, between, not_between]
                    # boolean operators [equal, not_equal, is_null, is_not_null]
                -
                    id: price
                    label: 'Product Price'
                    type: double
                    operators: [equal, not_equal, less, less_or_equal, greater, greater_or_equal, between, not_between, is_null, is_not_null]

                -
                    id: availability.startDate
                    label: 'Product Availability - Start Date'
                    type: datetime
    # these are used for service fl_qbjs_parser.json_query_parser.doctrine_orm_parser
    # if another orm is being used, omit this key
    doctrine_classes_and_mappings: 
        app_entity_product: # this key is for organizational purposes only
            class: AppBundle\Entity\Product # Class Name of a Doctrine Entity
            properties: # required
                # Keys sent by QueryBuilderJS in a jsonString
                # Values should be visible property (public or by getter) in your entity
                # They can also be associations and their properties
                # Leave the value as null (~) to use the same value as the key
                id: ~
                labels.id: ~
                labels.name: ~
                labels.authors.id: ~
                labels.authors.address.line1: ~
                author.id: ~
            association_classes:
                # Indicate the class for each of the associations in properties
                labels: AppBundle\Entity\Label
                labels.authors: AppBundle\Entity\Author
                labels.authors.address: AppBundle\Entity\Address
                author: AppBundle\Entity\Author
            # Now supporting embeddables!
            embeddables_properties:
                availability.startDate: ~
                availability.endDate: ~
                labels.availability.startDate: ~
                labels.availability.endDate: ~
                price.amount: ~
            embeddables_inside_embeddables_properties:
                price.currency.code: ~
            embeddables_association_classes:
                labels: AppBundle\Entity\Label
            embeddables_embeddable_classes:
                availability: League\Period\Period
                labels.availability: League\Period\Period
                price: Money\Money
                price.currency: Money\Currency

使用示例

fl_qbjs_parser.json_query_parser.doctrine_orm_parser

<?php
    namespace App\Controller;
    
    //...
    use Symfony\Component\HttpFoundation\Request;
    use Symfony\Bundle\FrameworkBundle\Controller\Controller;
    use AppBundle\Entity\Product;

    class ProductController extends Controller
    {
        public function reportsAction(Request $request, string $jsonString)
        {
             $parsedRuleGroup = $this->get('fl_qbjs_parser.json_query_parser.doctrine_orm_parser')->parseJsonString($jsonString, Product::class);
             
             $query = $this->get('doctrine.orm.entity_manager')->createQuery($parsedRuleGroup->getQueryString());
             $query->setParameters($parsedRuleGroup->getParameters());
             $results = $query->execute();
             
             //...
        }
    } 

fl_qbjs_parser.builders

<?php
    namespace App\Controller;
    
    //...
    use Symfony\Component\HttpFoundation\Request;
    use Symfony\Bundle\FrameworkBundle\Controller\Controller;

    class ReportController extends Controller
    {
        public function reportBuilderAction(Request $request)
        {
             $builders = $this->get('fl_qbjs_parser.builders')->getBuilders();
                     
             return $this->render('default/index.html.twig', [
                 'builders' => $builders,
             ]);
             
             //...
        }
    } 

事件

该包还提供了一个事件,允许您覆盖 fl_qbjs_parser.builders。您目前可以覆盖值、输入类型和运算符。

以下是一个此类事件的监听器配置示例。

services:
    app.listener.override_builders:
        class: AppBundle\EventListener\OverrideBuildersListener
        arguments:
        tags:
            - { name: kernel.event_listener, event: fl_qbjs_parser.filter_set_event, method: onFilterSet }