Skip to content

Commit 5556ba1

Browse files
committed
Merge PR stwe#820 support for join conditions (using 'WITH').
1 parent 291422f commit 5556ba1

File tree

3 files changed

+53
-4
lines changed

3 files changed

+53
-4
lines changed

Datatable/Column/AbstractColumn.php

+34
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,14 @@ abstract class AbstractColumn implements ColumnInterface
189189
*/
190190
protected $joinType;
191191

192+
/**
193+
* Join conditions, if the column represents an association.
194+
* Default: null
195+
*
196+
* @var string|null
197+
*/
198+
protected $joinConditions;
199+
192200
/**
193201
* The data type of the column.
194202
* Is set automatically in ColumnBuilder when 'null'.
@@ -323,6 +331,7 @@ public function configureOptions(OptionsResolver $resolver)
323331
'width' => null,
324332
'add_if' => null,
325333
'join_type' => 'leftJoin',
334+
'join_conditions' => null,
326335
'type_of_field' => null,
327336
'responsive_priority' => null,
328337
'sent_in_response' => true,
@@ -344,6 +353,7 @@ public function configureOptions(OptionsResolver $resolver)
344353
$resolver->setAllowedTypes('width', ['null', 'string']);
345354
$resolver->setAllowedTypes('add_if', ['null', 'Closure']);
346355
$resolver->setAllowedTypes('join_type', 'string');
356+
$resolver->setAllowedTypes('join_conditions', ['null', 'string']);
347357
$resolver->setAllowedTypes('type_of_field', ['null', 'string']);
348358
$resolver->setAllowedTypes('responsive_priority', ['null', 'int']);
349359
$resolver->setAllowedTypes('sent_in_response', ['bool']);
@@ -769,6 +779,30 @@ public function setJoinType($joinType)
769779
return $this;
770780
}
771781

782+
/**
783+
* Get join conditions.
784+
*
785+
* @return string
786+
*/
787+
public function getJoinConditions()
788+
{
789+
return $this->joinConditions;
790+
}
791+
792+
/**
793+
* Set join conditions.
794+
*
795+
* @param string $joinConditions
796+
*
797+
* @return $this
798+
*/
799+
public function setJoinConditions($joinConditions = null)
800+
{
801+
$this->joinConditions = $joinConditions;
802+
803+
return $this;
804+
}
805+
772806
/**
773807
* Get type of field.
774808
*

Resources/doc/columns.md

+2
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ With 'null' initialized options uses the default value of the DataTables plugin.
4747
| width | null or string | null | | Column width assignment. |
4848
| add_if | null or Closure | null | | Add column only if conditions are TRUE. |
4949
| join_type | string | 'leftJoin' | | Join type (default: 'leftJoin'), if the column represents an association. |
50+
| join_conditions | null or string | null | | Join conditions (using WITH), if the column represents an association. |
5051
| type_of_field | null or string | null (autodetect) | | Set the data type itself for ordering (example: integer instead string). |
5152
| responsive_priority | null or int | null | | Set column's visibility priority. Requires the Responsive extension. |
5253
| filter | array | TextFilter | | A Filter instance for individual filtering. |
@@ -124,6 +125,7 @@ $this->columnBuilder
124125
'data' => 'comments[,].title',
125126
'searchable' => true,
126127
'orderable' => true,
128+
'join_conditions' => 'comments.reviewed = 1',
127129
))
128130
;
129131
```

Response/DatatableQueryBuilder.php

+17-4
Original file line numberDiff line numberDiff line change
@@ -403,6 +403,7 @@ private function initColumnArrays()
403403
$this->addSearchColumn($column, null, $searchDql);
404404
} elseif (true === $this->accessor->getValue($column, 'selectColumn')) {
405405
$parts = explode('.', $dql);
406+
$withExpr = $this->accessor->getValue($column, 'joinConditions');
406407

407408
while (\count($parts) > 1) {
408409
$previousPart = $currentPart;
@@ -412,8 +413,14 @@ private function initColumnArrays()
412413
$currentAlias = ($previousPart === $this->entityShortName ? '' : $previousPart.'_').$currentPart;
413414
$currentAlias = $this->getSafeName($currentAlias);
414415

415-
if (! \array_key_exists($previousAlias.'.'.$currentPart, $this->joins)) {
416-
$this->addJoin($previousAlias.'.'.$currentPart, $currentAlias, $this->accessor->getValue($column, 'joinType'));
416+
$columnTableName = $previousAlias.'.'.$currentPart;
417+
if (!array_key_exists($columnTableName, $this->joins)) {
418+
$this->addJoin($columnTableName, $currentAlias, $this->accessor->getValue($column, 'joinType'));
419+
}
420+
// for the last dql part join using WITH, if expression is given for column
421+
if (count($parts) === 1 && array_key_exists($columnTableName, $this->joins) && null !== $withExpr) {
422+
$with = str_replace($currentPart . '.', $currentAlias . '.', $withExpr);
423+
$this->addJoin($columnTableName, $currentAlias, $this->accessor->getValue($column, 'joinType'), $with);
417424
}
418425

419426
$metadata = $this->setIdentifierFromAssociation($currentAlias, $currentPart, $metadata);
@@ -486,7 +493,11 @@ private function setSelectFrom(QueryBuilder $qb)
486493
private function setJoins(QueryBuilder $qb)
487494
{
488495
foreach ($this->joins as $key => $value) {
489-
$qb->{$value['type']}($key, $value['alias']);
496+
if (\array_key_exists('with', $value) && null !== $value['with']) {
497+
$qb->{$value['type']}($key, $value['alias'], Query\Expr\Join::WITH, $value['with']);
498+
} else {
499+
$qb->{$value['type']}($key, $value['alias']);
500+
}
490501
}
491502

492503
return $this;
@@ -714,14 +725,16 @@ private function addSearchOrderColumn($column, $columnTableName, $data)
714725
* @param string $columnTableName
715726
* @param string $alias
716727
* @param string $type
728+
* @param string $with
717729
*
718730
* @return $this
719731
*/
720-
private function addJoin($columnTableName, $alias, $type)
732+
private function addJoin($columnTableName, $alias, $type, $with = null)
721733
{
722734
$this->joins[$columnTableName] = [
723735
'alias' => $alias,
724736
'type' => $type,
737+
'with' => $with,
725738
];
726739

727740
return $this;

0 commit comments

Comments
 (0)