Skip to content
This repository has been archived by the owner on Feb 4, 2023. It is now read-only.

Support for join conditions (using 'WITH') #820

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

roxid
Copy link

@roxid roxid commented Sep 19, 2018

As mentioned in #700 this PR adds support for join conditions.

// for the last dql part join using WITH, if expression is given for column
if (count($parts) === 1 && array_key_exists($columnTableName, $this->joins) && null !== $withExpr) {
$with = str_replace($currentPart . '.', $currentAlias . '.', $withExpr);
$this->addJoin($columnTableName, $currentAlias, $this->accessor->getValue($column, 'joinType'), $with);
Copy link
Collaborator

@Seb33300 Seb33300 Sep 23, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you have a "complex" join like table1.table2.table3.field, maybe we want to set a different join condition on each join.
How do you handle it?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's possible by adding a column of each joined table to the column builder and defining the respective join condition.

$this->columnBuilder
    ->add('table1.table2.id', Column\Column::class, array(
        'visible' => false,
        'join_conditions' => 'table2.enabled = 1'
    ));

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did you tried it?
Is the table2.enabled = 1 not applied on the table1.table2 join?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, its applied on the table1.table2 join. If you then add a join to table3 you have different join conditions on each join. Or am I misunderstanding the question?

$this->columnBuilder
    ->add('table1.id', Column\Column::class, array()) // see comment below
    ->add('table1.table2.id', Column\Column::class, array(
        'join_conditions' => 'table2.enabled = 1'
    ))
    ->add('table1.table2.table3.id', Column\Column::class, array()) // see comment below
    ->add('table1.table2.table3.field', Column\Column::class, array(
        'join_conditions' => 'table3.otherField = 0'
    ));

During my tests I noticed that the joins only worked if the ids of the joined tables were explicitly included as columns in the datatable. Otherwise doctrine will throw an exception Cannot select distinct identifiers from query with LIMIT and ORDER BY on a column from a fetch joined to-many association. Use output walkers. But this also happens without the changes I made in this PR and the usage of join_conditions.

@stephanvierkant
Copy link
Collaborator

Thanks for providing a PR! Unfortunately, this library hasn't got many unit tests (yet). Are you willing to contribute by adding a tests for this new feature?

pokurek added a commit to RedWeb/DatatablesBundle that referenced this pull request Sep 10, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants