Custom orderBy active record hasMany relation


I am stuck on this problem. I had done it succesfully with providing an ArrayDataProvider to the view, but with ActiveDataProvider it seems a little tricky.

I need a specific order for records from a hasMany relation. I need to return the newest one and sort specific ones first and then the rest.

The order ir something like this
‘CASE WHEN table_name.status IN (1, 2, 3) THEN END DESC’

Now the problem that I am getting is that my query returns all records from the hasMany relation and in the view we don’t see the most recent information.

I am trying to add the relation by joinWith() ordering that relation descendingly so I get the most recent one first then limit(1), but that does not seem to work.