active-record
active-record copied to clipboard
refactor joinWith() storing and processing
refactor joinWith internals:
- store one join in $joinWith property instead of the whole array. i.e. normalize beforehand!
- makes a lot of the aliasing much cleaner and simpler.
related to #10253
As you plan to make BC breaking changes in joinWith, I would like to remind the suggestion to get more consistency (and reliability and efficiency) for the alias syntax between;
joinWith( ['alias' => 'relation'] ) //not yet possible
from( ['alias' => 'table'] )
innerJoin( ['alias' => 'table'] )
select( ['alias' => 'column'] )
As mentioned in https://github.com/yiisoft/yii2/issues/6951 and https://github.com/yiisoft/yii2/issues/10900
This syntax has not been added as it adds confusion about what an array key should stand for:
->joinWith( ['alias' => 'relation'] ) // array key is the alias
->joinWith( ['relation' => function($q) { ... }] ) // array key is the relation!?
@cebe how about ?
->joinWith( ['relation' => 'alias'] ) ->joinWith( ['relation' => function ($q)] ) ->joinWith( ['relation' => ['alias' => 'alias', 'callback' => function ($q)]] )
To be honest, I don't really like the ['alias' => 'relation/table'] notation, the natural order would rather be ['relation/table' => 'alias']. But it is best if it is consistent within the same framework.
Note that this imply to write two separate's joinWith if the same relation is used twice with different aliases (but that's not a bad way to code).
Or maybe use the simple doctrine style (which limits to one relation by joinWith)
joinWith( $with, $alias = null, $eagerLoading = true, $joinType = 'LEFT JOIN' )
To be honest, I don't really like the ['alias' => 'relation/table'] notation
this notation is forced by the nature of php, because we can have aliases for Query objects, the table/Query must be the array value and the alias be the key. A Query object can not be an array key.
@cebe Does joinWith() can have a query object? Could you give an example? Or are you talking about the from() method?
talking about from() and join(), query does not make sense for joinWith() because its only sense is to reuse existing definition of a relation.