Skip to content

Commit 7d4c188

Browse files
committed
docs: add HasManyThrough relationship documentation
1 parent f18b55f commit 7d4c188

1 file changed

Lines changed: 24 additions & 0 deletions

File tree

docs/1-essentials/03-database.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -293,6 +293,30 @@ public ?Address $address = null;
293293
- `throughOwnerJoin`: FK on the target table pointing to the intermediate
294294
- `throughRelationJoin`: PK on the intermediate table
295295

296+
### Has many through
297+
298+
The {b`#[Tempest\Database\HasManyThrough]`} attribute defines a one-to-many relationship that traverses an intermediate model. This lets you access a collection of distant relations directly, resolved in a single SQL query with two JOINs.
299+
300+
```php
301+
use Tempest\Database\HasManyThrough;
302+
303+
final class Author
304+
{
305+
/** @var \App\Payment\Payment[] */
306+
#[HasManyThrough(Contract::class)]
307+
public array $payments = [];
308+
}
309+
```
310+
311+
The `through` parameter specifies the intermediate model class. The target model is inferred from the docblock's array type. This generates SQL like:
312+
313+
```sql
314+
LEFT JOIN contracts ON contracts.author_id = authors.id
315+
LEFT JOIN payments ON payments.contract_id = contracts.id
316+
```
317+
318+
The same optional parameters as `HasOneThrough` are available for custom join fields: `ownerJoin`, `relationJoin`, `throughOwnerJoin`, and `throughRelationJoin`.
319+
296320
### Using UUIDs as primary keys
297321

298322
By default, Tempest uses auto-incrementing integers as primary keys. UUIDs can be used as primary keys instead by annotating the {b`Tempest\Database\PrimaryKey`} property with the {b`#[Tempest\Database\Uuid]`} attribute. Tempest automatically generates a UUID v7 when a new model is created:

0 commit comments

Comments
 (0)