joinWith() public method
Joins with the specified relations.
This method allows you to reuse existing relation definitions to perform JOIN queries. Based on the definition of the specified relation(s), the method will append one or multiple JOIN statements to the current query.
If the $eagerLoading
parameter is true, the method will also perform eager loading for the specified relations, which is equivalent to calling with() using the specified relations.
Note that because a JOIN query will be performed, you are responsible to disambiguate column names.
This method differs from with() in that it will build up and execute a JOIN SQL statement for the primary table. And when $eagerLoading
is true, it will call with() in addition with the specified relations.
public $this joinWith ( $with, $eagerLoading = true, $joinType = 'LEFT JOIN' ) | ||
---|---|---|
$with | string|array |
The relations to be joined. This can either be a string, representing a relation name or an array with the following semantics:
The relation name may optionally contain an alias for the relation table (e.g. Sub-relations can also be specified, see with() for the syntax. In the following you find some examples: // find all orders that contain books, and eager loading "books" Order::find()->joinWith('books', true, 'INNER JOIN')->all(); // find all orders, eager loading "books", and sort the orders and books by the book names. Order::find()->joinWith([ 'books' => function (\yii\db\ActiveQuery $query) { $query->orderBy('item.name'); } ])->all(); // find all orders that contain books of the category 'Science fiction', using the alias "b" for the books table Order::find()->joinWith(['books b'], true, 'INNER JOIN')->where(['b.category' => 'Science fiction'])->all(); The alias syntax is available since version 2.0.7. |
$eagerLoading | boolean|array |
Whether to eager load the relations specified in |
$joinType | string|array |
The join type of the relations specified in |
return | $this |
The query object itself |
Please login to continue.