PHP code example of laminas-api-tools / api-tools-doctrine-querybuilder
1. Go to this page and download the library: Download laminas-api-tools/api-tools-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/ */
laminas-api-tools / api-tools-doctrine-querybuilder example snippets
'api-tools-doctrine-query-provider' => [
'aliases' => [
'default_orm' => \Laminas\ApiTools\Doctrine\QueryBuilder\Query\Provider\DefaultOrm::class,
'default_odm' => \Laminas\ApiTools\Doctrine\QueryBuilder\Query\Provider\DefaultOdm::class,
],
'factories' => [
\Laminas\ApiTools\Doctrine\QueryBuilder\Query\Provider\DefaultOrm::class => \Laminas\ApiTools\Doctrine\QueryBuilder\Query\Provider\DefaultOrmFactory::class,
\Laminas\ApiTools\Doctrine\QueryBuilder\Query\Provider\DefaultOdm::class => \Laminas\ApiTools\Doctrine\QueryBuilder\Query\Provider\DefaultOdmFactory::class,
],
],
'api-tools-doctrine-querybuilder-orderby-orm' => [
'aliases' => [
'field' => \Laminas\ApiTools\Doctrine\QueryBuilder\OrderBy\ORM\Field::class,
],
'factories' => [
\Laminas\ApiTools\Doctrine\QueryBuilder\OrderBy\ORM\Field::class => \Laminas\ServiceManager\Factory\InvokableFactory::class,
],
],
'api-tools-doctrine-querybuilder-filter-orm' => [
'aliases' => [
'eq' => \Laminas\ApiTools\Doctrine\QueryBuilder\Filter\ORM\Equals::class,
],
'factories' => [
\Laminas\ApiTools\Doctrine\QueryBuilder\Filter\ORM\Equals::class => \Laminas\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('LaminasDoctrineQueryBuilderFilterManagerOrm');
$orderByManager = $serviceLocator->get('LaminasDoctrineQueryBuilderOrderByManagerOrm');
$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']
namespace My\Custom;
class TypeCaster implements \Laminas\ApiTools\Doctrine\QueryBuilder\Filter\TypeCastInterface
{
public function typeCastField($metadata, $field, $value, $format = null, $doNotTypecastDatetime = false)
{
// implement your type casting logic
}
}
// config/autoload/api-tools-doctrine-querybuilder-global.php
use Laminas\ApiTools\Doctrine\QueryBuilder\Filter\TypeCastInterface;
use Laminas\ServiceManager\Factory\InvokableFactory;
use My\Custom\TypeCaster;
return [
'service_manager => [
'aliases' => [
TypeCastInterface::class => TypeCaster::class,
],
'factories' => [
TypeCaster::class => InvokableFactory::class,
],
],
];