Browse Source

added example about alias for nesting joinWith calls

close #10878
tags/2.0.11
Carsten Brandt 8 years ago
parent
commit
0d89bc5171
  1. 15
      docs/guide/db-active-record.md

15
docs/guide/db-active-record.md

@ -1039,10 +1039,10 @@ In the code example above, we are modifying the relational query by appending an
### Joining with Relations <span id="joining-with-relations"></span>
> Note: The content described in this subsection is only applicable to relational databases, such as
> Note: The content described in this subsection is only applicable to relational databases, such as
MySQL, PostgreSQL, etc.
The relational queries that we have described so far only reference the primary table columns when
The relational queries that we have described so far only reference the primary table columns when
querying for the primary data. In reality we often need to reference columns in the related tables. For example,
we may want to bring back the customers who have at least one active order. To solve this problem, we can
build a join query like the following:
@ -1139,6 +1139,17 @@ Since version 2.0.7, Yii provides a shortcut for this. You may now define and us
$query->joinWith(['orders o'])->orderBy('o.id');
```
The above syntax works for simple relations. If you need an alias for an intermediate table when joining over
nested relations, e.g. `$query->joinWith(['orders.product'])`,
you need to nest the joinWith calls like in the following example:
```php
$query->joinWith(['orders o' => function($q) {
$q->joinWith('product p');
}])
->where('o.amount > 100');
```
### Inverse Relations <span id="inverse-relations"></span>
Relation declarations are often reciprocal between two Active Record classes. For example, `Customer` is related

Loading…
Cancel
Save