Core: OrderFilter on nested entity seems KO

Created on 18 Jul 2018  路  6Comments  路  Source: api-platform/core

Hi guys,

An entity _User_ has a relation with another entity _Conversation_.
If I use the OrderBy filter on _conversation.createdAt_ the User without a _Conversation_ will be excluded from the results.

@ApiFilter(OrderFilter::class, properties={"conversation.createdAt": "ASC" }})

Searching through github I've found :

So I transform my filter to :

@ApiFilter(OrderFilter::class, properties={"conversation.createdAt": { "nulls_comparison": OrderFilter::NULLS_SMALLEST, "default_direction": "ASC" }})

Full of trust :+1: , I press Send button on postman and... still excluded :disappointed:

Digging a bit I see that the OrderFilter call a specific method for join relation :

list($alias, $field) = $this->addJoinsForNestedProperty($property, $alias, $queryBuilder, $queryNameGenerator, $resourceClass);

Then on the AbstractFilter :

$alias = QueryBuilderHelper::addJoinOnce($queryBuilder, $queryNameGenerator, $parentAlias, $association);

And finally in the QueryBuilderHelper :

if (Join::LEFT_JOIN === $joinType || QueryChecker::hasLeftJoin($queryBuilder)) {
    $queryBuilder->leftJoin($query, $associationAlias, $conditionType, $condition);
} else {
    $queryBuilder->innerJoin($query, $associationAlias, $conditionType, $condition);
}

There is no $joinType send and the QueryCheck::hasLeftJoin returns false, so an _innerJoin_ is made instead of a _leftJoin_.
If I forced a _leftJoin_, the results are ok.

Am I missing something or is there a problem?

Thanks,
J茅r茅my.

bug

All 6 comments

nulls_comparison is for ordering NULL values. It does not affect joins.

That said, we might be able to do something similar for a non-existent relation...

But anyway, yes, I think this should be considered a bug.

Summoning @soyuka :crystal_ball:

@BatsaxIV could you send me both entities? Thanks!

Hi @soyuka,

Here are the entities : 2109.tar.gz

So when I get the entries for the Friend entity, I need them ordered by _conversation.updatedAt_ and if there is no conversation, the entry is not returned.

Thanks @soyuka !

Was this page helpful?
0 / 5 - 0 ratings