PHP code example of zfcampus / zf-doctrine-querybuilder

1. Go to this page and download the library: Download zfcampus/zf-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/ */

    

zfcampus / zf-doctrine-querybuilder example snippets


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

'zf-doctrine-querybuilder-orderby-orm' => [
    'aliases' => [
        'field' => \ZF\Doctrine\QueryBuilder\OrderBy\ORM\Field::class,
    ],
    'factories' => [
        \ZF\Doctrine\QueryBuilder\OrderBy\ORM\Field::class => \Zend\ServiceManager\Factory\InvokableFactory::class,
    ],
],
'zf-doctrine-querybuilder-filter-orm' => [
    'aliases' => [
        'eq' => \ZF\Doctrine\QueryBuilder\Filter\ORM\Equals::class,
    ],
    'factories' => [
        \ZF\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'],

    ['type' => 'leftjoin', 'field' => 'report', 'alias' => 'r'],
    ['type' => 'isnull', 'alias' => 'r', 'field' => 'id']

['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']