bipiane/criteria-builder

Doctrine Criteria Builder from HTTP parameters in PHP

v3.0.3 2019-12-13 16:18 UTC

This package is auto-updated.

Last update: 2024-11-09 21:01:43 UTC


README

Latest Stable Version Total Downloads License

Doctrine Criteria Builder from HTTP parameters in PHP

Installation

composer require bipiane/criteria-builder

Example

<?php
/**
 * GET ../api/users?
 *          limit=12&
 *          offset=1&
 *          sort=city-name&
 *          order=ASC&
 *          lastname[like]=Pian%&
 *          city-state-code[ne]=null&
 *          city-state-country=1&
 *          enabled=true&
 *          id[ge]=50
 * @param Request $request
 */
public function exampleAction(Request $request)
{
    $criteriaUser = [
        'id' => CriteriaDoctrine::CRITERIAS_NUMBER,
        'name' => CriteriaDoctrine::CRITERIAS_STRING,
        'lastname' => CriteriaDoctrine::CRITERIAS_STRING,
        'city' => [
            'id' => CriteriaDoctrine::CRITERIAS_NUMBER,
            'name' => CriteriaDoctrine::CRITERIAS_STRING,
            'state' => [
                'id' => CriteriaDoctrine::CRITERIAS_NUMBER,
                'name' => CriteriaDoctrine::CRITERIAS_STRING,
                'code' => CriteriaDoctrine::CRITERIAS_STRING,
                'country' => [
                    'id' => CriteriaDoctrine::CRITERIAS_NUMBER,
                    'name' => CriteriaDoctrine::CRITERIAS_STRING,
                    'enabled' => CriteriaDoctrine::CRITERIAS_BOOLEAN,
                ],
                'enabled' => CriteriaDoctrine::CRITERIAS_BOOLEAN,
            ],
            'enabled' => CriteriaDoctrine::CRITERIAS_BOOLEAN,
        ],
        'enabled' => CriteriaDoctrine::CRITERIAS_BOOLEAN,
    ];

    try {
        $qb = $this->getDoctrine()->getManager()
            ->getRepository('ModelBundle:User')
            ->createQueryBuilder('usr');

        $qb = CriteriaBuilder::fetchFromQuery(
            $qb,
            $request->query->all(),
            $criteriaUser
        );

        var_dump($qb->getQuery()->getArrayResult());
    } catch (CriteriaException $e) {
    }
    // ...
}

Testing

./vendor/bin/simple-phpunit