adnanmula/criteria

0.1.0 2024-02-18 19:45 UTC

This package is auto-updated.

Last update: 2024-11-25 09:25:40 UTC


README

Useful classes to build dynamic queries, made for postgres.

Installation

Install via composer

composer require adnanmula/criteria

Usage

//Use the criteria class to build queries

$criteria = new Criteria(
    10, //Offset
    20, //Limit
    new Sorting(
        new Order(
            new FilterField('name'),
            OrderType::ASC,
        ),
        new Order(
            new FilterField('name'),
            OrderType::DESC,
        ),
    ),
    new AndFilterGroup(
        FilterType::OR,
        new Filter(new FilterField('id'), new StringFilterValue('id'), FilterOperator::EQUAL),
        new Filter(new FilterField('field'), new StringArrayFilterValue('value1', 'value2', 'value3'), FilterOperator::IN),
        ...$moreFilters
    ),
    new OrFilterGroup(
        FilterType::AND,
        new Filter(new FilterField('json_field'), new ArrayElementFilterValue('value'), FilterOperator::IN_ARRAY),
        new Filter(new FilterField('amount'), new IntFilterValue(3), FilterOperator::LESS_OR_EQUAL),
    ),
    ...$moreFilterGroups,
);
//Example of repository

$query = $this->connection->createQueryBuilder()->select('a.fields')
    ->from('table', 'a');

(new DbalCriteriaAdapter($builder))->execute($criteria);

$result = $query->execute()->fetchAllAssociative();