fourlabs/qbjs-parser-bundle

此包是用于fourlabs/qbjs-parser的Symfony包装器。

1.0.2 2019-11-29 07:50 UTC

This package is auto-updated.

Last update: 2024-09-22 06:48:00 UTC


README

StyleCI GitHub license

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

  • 它可以解析前端传来的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
  • 将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 }