twodevs/search-query-builder

1.0.5 2015-05-19 11:37 UTC

This package is auto-updated.

Last update: 2024-10-19 10:15:12 UTC


README

PHP library to create doctrine queries from json format.

Usage

1) JSON input

{
  "query" : [
      {
        "type" : "group",
        "cond" : "or",
        "query" : [
          {"cond" : "or", "type" : "eq", "field" : "q.id", "value" : 1},
          {"cond" : "or", "type" : "eq", "field" : "q.id", "value" : 2}
        ]
      },
      {"cond" : "and", "type" : "eq", "field" : "q.name", "value" : "Test" },
      {"cond" : "and", "type" : "gt", "field" : "q.date", "value" : "10.04.2015" }
  ]
}

2) Create query builder

// Create event dispatcher
$ed = new EventDispatcher();
$ed->addSubscriber(new EQBuilder());
$ed->addSubscriber(new CompareBuilder());
$ed->addSubscriber(new LikeBuilder());
$ed->addSubscriber(new InBuilder());
$ed->addSubscriber(new MemberOfBuilder());
$ed->addSubscriber(new IntTransformer());
$ed->addSubscriber(new DateTimeTransformer());

// Create query builder
$qb = new QueryBuilder($ed);

// Get doctrine query builder
$doctrineQB = $doctrineEntityManager
                ->getRepository('Test')
                ->createQueryBuilder('q')
                ->orderBy('q.updatedAt', 'desc')
            ;
            
// Define allowed fields and set field options
$allowedFields = [
    'q.id' => [ 'cast_int' => true ], 
    'q.name', 
    'q.createdAt' => [
        'datetime' => [ 'input' => 'd.m.Y', output' => 'Y-m-d H:i:s', 'reset' => false ] 
    ]
];

// Create query
$query = $qb->createQuery($queryData, $allowedFields, $doctrineQB);

3) How to create a new LineBuilder

Line builder, builds the query. If you need custom functions create your own line builder, by extending the AbstractLineQueryBuilder. Please have a look at one of the build in line builder to see a example.

If you have create your own line builder, you need just to attach your line builder to the query builder:


$ed->addSubscriber(new CustomLineBuilder());

4) How to create a new ValueTransformer

You can use ValueTransformer to transform or validate a incoming value. If you want to create your own value transformer you need just extends the AbstractValueTransformer. Please have a look at one of the build in value transformer to see a example.

If you have create your own value transformer, you need just to attach your transformer to the query builder:


$ed->addSubscriber(new CustomValueTransformer());