Objectiphy
  • Introduction
  • Quick Start
  • Basic Fetching
  • Basic Saving
  • Deleting
  • Mapping Providers
    • Attributes
    • Objectiphy Mapping
    • Doctrine Mapping
  • Defining Relationships
    • One to one
    • One to Many
    • Many to one
    • Many to Many
    • Scalar Joins
  • Query Builder
    • Running a Query
    • Select Queries
    • Update Queries
    • Insert Queries
    • Delete Queries
    • Criteria
      • Operators
      • Filtering by Child Objects
      • Filtering by Aggregates
    • Joins
  • Pagination and Sorting
  • Embedded Value Objects
  • Serialization Groups
  • Late Binding and Lazy Loading
  • Streaming Results
  • Custom Repositories
  • Optimisation
  • Configuration Options
  • Mapping Overrides
  • Caching
  • Comparison with Doctrine
  • Code Generation etc.
  • Troubleshooting
  • Licence, Copyright, Credits
Powered by GitBook
On this page

Was this helpful?

  1. Query Builder

Joins

You can use custom joins to narrow down which records are returned, even for relationships that are not defined in your mapping definitions.

The query builder can also be used to specify custom joins with aliases that can be used in the criteria. Normally, joins are handled automatically based on the relationships you define between your entities, however, sometimes you will want to limit the records that are returned by adding a custom join (perhaps back to the same entity), and specifying criteria based on any joined records. To do this, just call the leftJoin or innerJoin method on the query builder like this:

$query = QueryBuilder::create()
    ->leftJoin(Policy::class, 'p2')
    ->on('p2.policyNo', '=', 'policyNo')
        ->and('p2.id', '>', 'id')
        ->and('p2.status', '=', Policy::STATUS_INFORCE)
        ->and('p2.modification', '=', 'CANCELLED')
    ->where('modification', QB::NOT_EQ, 'CANCELLED')
    ->and('p2.id', 'IS', null)
    ->buildSelectQuery();

The above would be translated into SQL such as this:

SELECT [...] FROM `policy` 
LEFT JOIN `policy` `p2` ON `p2`.`policy_no` = `policy`.`policy_no`
    AND `p2`.`id` > `policy`.`id`
    AND `p2`.`status` = 'INFORCE'
    AND `p2`.`modification` = 'CANCELLED' 
WHERE `policy`.`modification` != 'CANCELLED'
    AND `p2`.`id` IS NULL 
PreviousFiltering by AggregatesNextPagination and Sorting

Last updated 4 years ago

Was this helpful?