Download the PHP package zfcampus/zf-doctrine-querybuilder without Composer
On this page you can find all versions of the php package zfcampus/zf-doctrine-querybuilder. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download zfcampus/zf-doctrine-querybuilder
More information about zfcampus/zf-doctrine-querybuilder
Files in zfcampus/zf-doctrine-querybuilder
Package zf-doctrine-querybuilder
Short Description Apigility Doctrine QueryBuilder module
License BSD-3-Clause
Homepage https://apigility.org
Informations about the package zf-doctrine-querybuilder
ZF Doctrine QueryBuilder
Repository abandoned 2019-12-31
This repository has moved to laminas-api-tools/api-tools-doctrine-querybuilder.
This library provides query builder directives from array parameters. This library was designed to apply filters from an HTTP request to give an API fluent filter and order-by dialects.
Philosophy
Given developers identified A and B: A == B with respect to ability and desire to filter and sort the entity data.
The Doctrine entity to share contains
Developer A or B writes the API. The resource is a single Doctrine Entity and the data
is queried using a Doctrine QueryBuilder $objectManager->createQueryBuilder()
.
This module gives the other developer the same filtering and sorting ability to the
Doctrine query builder, but accessed through request parameters, as the API author.
For instance, startAt between('2015-01-09', '2015-01-11');
and name like ('%arlie')
are not common API filters for hand rolled APIs and perhaps without this module the API
author would choose not to implement it for their reason(s). With the help of this
module the API developer can implement complex queryability to resources without
complicated effort thereby maintaining A == B.
Installation
Installation of this module uses composer. For composer documentation, please refer to getcomposer.org.
Once installed, add ZF\Doctrine\QueryBuilder
to your list of modules inside
config/application.config.php
.
zf-component-installer
If you use zf-component-installer, that plugin will install zf-doctrine-querybuilder as a module for you.
Configuring the Module
Copy config/zf-doctrine-querybuilder.global.php.dist
to config/autoload/zf-doctrine-querybuilder.global.php
and edit the list of aliases for orm and odm to those you want enabled by default.
Use With Apigility Doctrine
To enable all filters you may override the default query providers in zf-apigility-doctrine
.
Add this to your zf-doctrine-querybuilder.global.php
config file and filters and order-by will be applied
if they are in $_GET['filter']
or $_GET['order-by']
request. These $_GET
keys are customizable
through zf-doctrine-querybuilder-options
:
Use
Configuration example
Request example
Resource example
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.
Each filter definition requires at a minimum a 'type'. A type references the configuration key such as 'eq', 'neq', 'between'.
Each filter definition requires at a minimum a 'field'. This is the name of a field on the target entity.
Each filter definition may specify 'where' with values of either 'and', 'or'.
Embedded logic such as and(x or y) is supported through AndX and OrX filter types.
Building HTTP GET query:
Javascript Example:
Querying Relations
Single valued
It is possible to query collections by relations - just supply the relation name as fieldName
and
identifier as value
.
Assuming we have defined 2 entities, User
and UserGroup
...
find all users that belong to UserGroup id #1 by querying the user resource with the following filter:
Collection valued
To match entities A that have entity B in a collection use ismemberof
.
Assuming User
has a ManyToMany (or OneToMany) association with UserGroup
...
find all users that belong to UserGroup id #1 by querying the user resource with the following filter:
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 Y-m-d H:i:s
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
Joining Entities and Aliasing Queries
There is an included ORM Query Type for Inner Join so for every filter type there is an optional alias
.
The default alias is 'row' and refers to the entity at the heart of the REST resource.
There is not a filter to add other entities to the return data. That is, only the original target resource,
by default 'row', will be returned regardless of what filters or order by are applied through this module.
Inner Join is not included by default in the zf-doctrine-querybuilder.global.php.dist
.
This example joins the report field through the inner join already defined on the row entity then filters
for r.id = 2
:
You can inner join tables from an inner join using parentAlias
:
Inner Join is commented by default in the zf-doctrine-querybuilder.global.php.dist
.
There is also an ORM Query Type for LeftJoin. This join type is commonly used to fetch an empty right side of a relationship.
Left Join is commented by default in the zf-doctrine-querybuilder.global.php.dist
.
Included Filter Types
ORM and ODM
Equals:
Not Equals:
Less Than:
Less Than or Equals:
Greater Than:
Greater Than or Equals:
Is Null:
Is Not Null:
Note: Dates in the In and NotIn filters are not handled as dates. It is recommended you use multiple Equals statements instead of these filters for date datatypes.
In:
NotIn:
Between:
Like (%
is used as a wildcard):
ORM Only
Is Member Of:
AndX:
In AndX queries, the conditions
is an array of filter types for any of those described
here. The join will always be and
so the where
parameter inside of conditions is
ignored. The where
parameter on the AndX filter type is not ignored.
OrX:
In OrX queries, the conditions
is an array of filter types for any of those described
here. The join will always be or
so the where
parameter inside of conditions is
ignored. The where
parameter on the OrX filter type is not ignored.
ODM Only
Regex:
Included Order By Type
Field:
All versions of zf-doctrine-querybuilder with dependencies
doctrine/doctrine-module Version ^1.2 || ^2.1.8
zendframework/zend-hydrator Version ^1.1 || ^2.2.1 || ^3.0
zendframework/zend-modulemanager Version ^2.7.2
zendframework/zend-mvc Version ^2.7.13 || ^3.0.2
zendframework/zend-servicemanager Version ^2.7.6 || ^3.1.1
zfcampus/zf-api-problem Version ^1.2.2
zfcampus/zf-apigility-doctrine Version ^2.1
zfcampus/zf-hal Version ^1.4.2