nilportugues / php-sql-query-builder

An elegant lightweight and efficient SQL Query Builder with fluid interface SQL syntax supporting bindings and complicated query generation.
http://nilportugues.com
MIT License
417 stars 114 forks source link

Allow custom self column in join #100

Closed nathandentzau closed 6 years ago

nathandentzau commented 6 years ago

The current JoinQuery::addJoin() method does not allow for a custom Column. Below is an example of the use case for this change:

$query = new Select();

$query
  ->setTable('company')
  ->setColumns([
    'company_id' => 'id',
    'name' => 'name',
  ])
  ->join(
    'field_company_code',
    'id',
    'entity_id',
  )
  ->join(
    'company_codes',
    new Column('target_id', 'field_company_code'),
    new Column('id', 'company_codes'),
    ['code' => 'code']
  );

This query structure is common in Drupal when selecting values from fields on entities that reference other entities through the Field and Entity API.