PHP code example of jackdpeterson / psr7-doctrine-querybuilder

1. Go to this page and download the library: Download jackdpeterson/psr7-doctrine-querybuilder library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

jackdpeterson / psr7-doctrine-querybuilder example snippets


'zf-apigility-doctrine-query-provider' => [
    'aliases' => [
        'default_orm' => \jackdpeterson\Doctrine\QueryBuilder\Query\Provider\DefaultOrm::class,
        'default_odm' => \jackdpeterson\Doctrine\QueryBuilder\Query\Provider\DefaultOdm::class,
    ],
    'factories' => [
        \jackdpeterson\Doctrine\QueryBuilder\Query\Provider\DefaultOrm::class => \jackdpeterson\Doctrine\QueryBuilder\Query\Provider\DefaultOrmFactory::class,
        \jackdpeterson\Doctrine\QueryBuilder\Query\Provider\DefaultOdm::class => \jackdpeterson\Doctrine\QueryBuilder\Query\Provider\DefaultOdmFactory::class,
    ],
],

'zf-doctrine-querybuilder-orderby-orm' => [
    'aliases' => [
        'field' => \jackdpeterson\Doctrine\QueryBuilder\OrderBy\ORM\Field::class,
    ],
    'factories' => [
        \jackdpeterson\Doctrine\QueryBuilder\OrderBy\ORM\Field::class => \Zend\ServiceManager\Factory\InvokableFactory::class,
    ],
],
'zf-doctrine-querybuilder-filter-orm' => [
    'aliases' => [
        'eq' => \jackdpeterson\Doctrine\QueryBuilder\Filter\ORM\Equals::class,
    ],
    'factories' => [
        \jackdpeterson\Doctrine\QueryBuilder\Filter\ORM\Equals::class => \Zend\ServiceManager\Factory\InvokableFactory::class,
    ],
],

$_GET = [
    'filter' => [
        [
            'type'  => 'eq',
            'field' => 'name',
            'value' => 'Tom',
        ],
    ],
    'order-by' => [
        [
            'type'      => 'field',
            'field'     => 'startAt',
            'direction' => 'desc',
        ],
    ],
];

$serviceLocator = $this->getApplication()->getServiceLocator();
$objectManager = $serviceLocator->get('doctrine.entitymanager.orm_default');

$filterManager = $serviceLocator->get('ZfDoctrineQueryBuilderFilterManagerOrm');
$orderByManager = $serviceLocator->get('ZfDoctrineQueryBuilderOrderByManagerOrm');

$queryBuilder = $objectManager->createQueryBuilder();
$queryBuilder->select('row')
    ->from($entity, 'row')
;

$metadata = $objectManager->getMetadataFactory()->getMetadataFor(ENTITY_NAME); // $e->getEntity() using doctrine resource event
$filterManager->filter($queryBuilder, $metadata, $_GET['filter']);
$orderByManager->orderBy($queryBuilder, $metadata, $_GET['order-by']);

$result = $queryBuilder->getQuery()->getResult();

/**
 * @Entity
 */
class User {
    /**
     * @ManyToOne(targetEntity="UserGroup")
     * @var UserGroup
     */
    protected $group;
}

/**
 * @Entity
 */
class UserGroup {}

['type' => 'eq', 'field' => 'group', 'value' => '1']

/**
 * @Entity
 */
class User {
    /**
     * @ManyToMany(targetEntity="UserGroup")
     * @var UserGroup[]|ArrayCollection
     */
    protected $groups;
}

['type' => 'ismemberof', 'field' => 'groups', 'value' => '1']

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

    ['type' => 'innerjoin', 'field' => 'report', 'alias' => 'r'],
    ['type' => 'eq', 'alias' => 'r', 'field' => 'id', 'value' => '2']

    ['type' => 'innerjoin', 'parentAlias' => 'r', 'field' => 'owner', 'alias' => 'o'],

'zf-doctrine-querybuilder-filter-orm' => [
    'aliases' => [
        'innerjoin' => \jackdpeterson\Doctrine\QueryBuilder\Filter\ORM\InnerJoin::class,
    ],
    'factories' => [
        \jackdpeterson\Doctrine\QueryBuilder\Filter\ORM\InnerJoin => \Zend\ServiceManager\Factory\InvokableFactory::class,
    ],
],

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

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

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

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

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

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

['type' => 'isnull', 'field' => 'fieldName']

['type' => 'isnotnull', 'field' => 'fieldName']

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

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

['type' => 'between', 'field' => 'fieldName', 'from' => 'startValue', 'to' => 'endValue']

['type' => 'like', 'field' => 'fieldName', 'value' => 'like%search']

['type' => 'ismemberof', 'field' => 'fieldName', 'value' => 1]

[
    'type' => 'andx',
    'conditions' => [
        ['field' =>'name', 'type'=>'eq', 'value' => 'ArtistOne'],
        ['field' =>'name', 'type'=>'eq', 'value' => 'ArtistTwo'],
    ],
    'where' => 'and',
]

[
    'type' => 'orx',
    'conditions' => [
        ['field' =>'name', 'type'=>'eq', 'value' => 'ArtistOne'],
        ['field' =>'name', 'type'=>'eq', 'value' => 'ArtistTwo'],
    ],
    'where' => 'and',
]

['type' => 'regex', 'field' => 'fieldName', 'value' => '/.*search.*/i']

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