Download the PHP package adt/base-query without Composer
On this page you can find all versions of the php package adt/base-query. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download adt/base-query
More information about adt/base-query
Files in adt/base-query
Package base-query
Short Description Doctrine components
License MIT
Homepage https://www.appsdevteam.com
Informations about the package base-query
Doctrine Components
Install
Creating a QueryObject class
Method getEntityClass
The getEntityClass
method must be specified and return your entity class.
Method setDefaultOrder
The setDefaultOrder
method must be specified and set the default order.
Method init
The init method is used to specify default filters and order. You have to always call parent::init()
when you use it.
Callback array filter
filter
is array of callbacks which will be applied on QueryBuilder
when created.
Similarly you can use order
callback for setting the query object order.
Method by
and orderBy
Method by
is a shortcut for creating filter
callbacks. It offers some useful features:
-
When there are more columns,
orWhere
is used among them. -
If a
$value
is type of 'string',LIKE %$value%
is used. You can change it by parameterfilterType
with valueFilterTypeEnum::STRICT
. - If you would like get all value in certain range, you can use parameter
filterType
with valueFilterTypeEnum::RANGE
.
Method orderBy
is a shortcut for setting order
callback.
- You can use a column name as first parameter and ASC/DESC as a second parameter instead of an array, if you need to sort only by one column.
You can use dot notation to auto join other entities (left join is used).
Basic usage
Creating an instance
or better with the use of a factory:
`
together with neon:
Fetch results
Count results
Disable default filters
Pagination
`
Advanced features
Manul joins
For manual joins you should use innerJoin
and leftJoin
methods:
Unlike QueryBuilder::innerJoin
and QueryBuilder::leftJoin
, this ensures that same joins are not used multiple times and don't throw an error.
More columns
Don't use addSelect
inside a filter
callback. Use initSelect
method instead:
Or create your own fetching method:
When there are more columns specified, default fetch*
method won't work.
More complex sorting
You can create your own sorting callback instead of using orderBy
method:
Don't forget to use AS HIDDEN
in your addSelect
method, otherwise the fetch*
methods won't work.
Method orById
If you want to get all active records plus a specific one, you can use orById
method to bypass default filters:
It's especially useful for <select>
.
Use in batch processing
If you want to iterate large number of records, you can use https://github.com/Ocramius/DoctrineBatchUtils together with query object:
You should always use new EntityManager
instance, not the default one (because of EntityManager::clear
).
Tips
-
Always have all logic inside a
filter
ororder
callback. This will ensure that all dependencies (like a logged user etc.) are already set. -
Always use
and*
methods (andWhere
,andSelect
, ...) on QueryBuilder (instead ofwhere
,select
, ...). -
Don't use
QueryBuilder::resetDQLPart
method, because it's againt basic idea of QueryObject -
Parameters in
andWhere
method should by named by method name and parametr name to avoid collision. -
Methods
by
andorderBy
are public methods, but it's always better to create ownby*
ororderBy*
methods. - You should always specify a deterministic order, ideally with usage of primary key.