2017 © Pedro Peláez
 

library zf-doctrine-criteria

Doctrine Criteria from Array Parameters

image

api-skeletons/zf-doctrine-criteria

Doctrine Criteria from Array Parameters

  • Tuesday, July 3, 2018
  • by tom_anderson
  • Repository
  • 1 Watchers
  • 0 Stars
  • 511 Installations
  • PHP
  • 1 Dependents
  • 0 Suggesters
  • 1 Forks
  • 0 Open issues
  • 5 Versions
  • 0 % Grown

The README.md

ZF Doctrine Criteria

Build Status Coverage Gitter Patreon Total Downloads, (*1)

This library builds a Criteria object from array parameters for use in filtering collections., (*2)

Installation

Installation of this module uses composer. For composer documentation, please refer to getcomposer.org., (*3)

$ composer require api-skeletons/zf-doctrine-criteria

Once installed, add ZF\Doctrine\Criteria to your list of modules inside config/application.config.php or config/modules.config.php., (*4)

zf-component-installer

If you use zf-component-installer, that plugin will install zf-doctrine-criteria as a module for you., (*5)

Configuring the Module

Copy config/zf-doctrine-criteria.global.php.dist to config/autoload/zf-doctrine-criteria.global.php and edit the list of aliases for those you want enabled. By default all supported expressions are enabled., (*6)

Note AND and OR composite expressions are not supported yet., (*7)

Use

use Doctrine\Common\Util\ClassUtils;
use ZF\Doctrine\Criteria\Builder as CriteriaBuilder;

$filterArray = [
    [
        'type' => 'eq',
        'field' => 'name',
        'value' => 'Grateful Dead',
    ],
    [
        'type' => 'beginswith',
        'field' => 'state',
        'value' => 'UT',
    ],
];

$orderByArray = [
    [
        'type' => 'field',
        'field' => 'venue',
        'direction' => 'asc',
    ]
];

$criteriaBuilder = $container->get(CriteriaBuilder::class);
$entityClassName = ClassUtils::getRealClass(get_class($collection->first()));
$metadata = $objectManager->getClassMetadata($entityClassName);
$criteria = $criteriaBuilder->create($metadata, $filterArray, $orderByArray);

$filteredCollection = $collection->matching($criteria);

Filters

Filters are not simple key/value pairs. Filters are a key-less array of filter definitions. Each filter definition is an array and the array values vary for each filter type., (*8)

Each filter definition requires at a minimum a 'type'. A type references the configuration key such as 'eq', 'neq', 'contains'., (*9)

Each filter definition requires at a minimum a 'field'. This is the name of a field on the target entity., (*10)

Each filter definition may specify 'where' with values of either 'and', 'or'., (*11)

Format of Date Fields

When a date field is involved in a filter you may specify the format of the date using PHP date formatting options. The default date format is ISO 8601 Y-m-d\TH:i:sP If you have a date field which is just Y-m-d then add the format to the filter. For complete date format options see DateTime::createFromFormat, (*12)

[
    'format' => 'Y-m-d',
    'value' => '2014-02-04',
]

Included Filter Types

Equals:, (*13)

Doctrine Collections does not currently support DateTime Equals comparisons. Any DateTime values sent through the equals filter will always return not equals. This is a shortcoming of doctrine/collections and not this module. Other comparison operators should work as expected., (*14)

['type' => 'eq', 'field' => 'fieldName', 'value' => 'matchValue']

Not Equals:, (*15)

['type' => 'neq', 'field' => 'fieldName', 'value' => 'matchValue']

Less Than:, (*16)

['type' => 'lt', 'field' => 'fieldName', 'value' => 'matchValue']

Less Than or Equals:, (*17)

['type' => 'lte', 'field' => 'fieldName', 'value' => 'matchValue']

Greater Than:, (*18)

['type' => 'gt', 'field' => 'fieldName', 'value' => 'matchValue']

Greater Than or Equals:, (*19)

['type' => 'gte', 'field' => 'fieldName', 'value' => 'matchValue']

Contains:, (*20)

Used to search inside of a string. Comlimentary with Starts With & Ends With, contains matches a string inside any part of the value., (*21)

['type' => 'contains', 'field' => 'fieldName', 'value' => 'matchValue']

Starts With:, (*22)

['type' => 'startswith', 'field' => 'fieldName', 'value' => 'matchValue']

Ends With:, (*23)

['type' => 'endswith', 'field' => 'fieldName', 'value' => 'matchValue']

Member Of:, (*24)

Used to search inside an array field to match the matchValue to an array element., (*25)

['type' => 'memeberof', 'field' => 'fieldName', 'value' => 'matchValue']

In:, (*26)

Note: Dates in the In and NotIn filters are not handled as dates. It is recommended you use other filters instead of these filters for date datatypes., (*27)

['type' => 'in', 'field' => 'fieldName', 'values' => [1, 2, 3]]

NotIn:, (*28)

Note: Dates in the In and NotIn filters are not handled as dates. It is recommended you use other filters instead of these filters for date datatypes., (*29)

['type' => 'notin', 'field' => 'fieldName', 'values' => [1, 2, 3]]

OrderBy

Field:, (*30)

['type' => 'field', 'field' => 'fieldName', 'direction' => 'desc']

The Versions