active-record icon indicating copy to clipboard operation
active-record copied to clipboard

refactor joinWith() storing and processing

Open cebe opened this issue 9 years ago • 7 comments

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

cebe avatar Nov 25 '15 23:11 cebe

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

CedricYii avatar Apr 07 '16 10:04 CedricYii

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 avatar Apr 07 '16 11:04 cebe

@cebe how about ?

->joinWith( ['relation' => 'alias'] ) ->joinWith( ['relation' => function ($q)] ) ->joinWith( ['relation' => ['alias' => 'alias', 'callback' => function ($q)]] )

Faryshta avatar Apr 08 '16 01:04 Faryshta

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' )

CedricYii avatar Apr 08 '16 08:04 CedricYii

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 avatar Apr 09 '16 02:04 cebe

@cebe Does joinWith() can have a query object? Could you give an example? Or are you talking about the from() method?

CedricYii avatar Apr 11 '16 08:04 CedricYii

talking about from() and join(), query does not make sense for joinWith() because its only sense is to reuse existing definition of a relation.

cebe avatar Apr 20 '16 07:04 cebe