You can override the default automatic behavior of the ORMAdapter by providing custom query or criteria processors.
Custom Queries
Use the query option to provide a QueryBuilder processor. This is useful if you need specific joins or selections that the AutomaticQueryBuilder doesn't handle.
Custom Criteria
Use the criteria option to define how data is filtered. Note that providing your own criteria overrides the defaults (searching and sorting). To keep default searching, you must manually include SearchCriteriaProvider in your array of processors.
Events
You can listen to ORMAdapterEvents::PRE_QUERY to configure the QueryBuilder (e.g., enabling result or query caching) after it is built but before iteration starts.
// Custom Query
$table->createAdapter(ORMAdapter::class, [
'entity' => Employee::class,
'query' => function (QueryBuilder $builder) {
$builder
->select('e')
->addSelect('c')
->from(Employee::class, 'e')
->leftJoin('e.company', 'c')
;
},
]);
// Custom Criteria
$table->createAdapter(ORMAdapter::class, [
'entity' => Employee::class,
'criteria' => [
function (QueryBuilder $builder) {
$builder->andWhere($builder->expr()->like('c.name', ':test'))->setParameter('test', '%ny 2%');
},
new SearchCriteriaProvider(),
],
]);
// Event Listener
$table->addEventListener(ORMAdapterEvents::PRE_QUERY, function(ORMAdapterQueryEvent $event) {
$event->getQuery()->useResultCache(true)->useQueryCache(true);
});