You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/1-essentials/03-database.md
+24Lines changed: 24 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -293,6 +293,30 @@ public ?Address $address = null;
293
293
-`throughOwnerJoin`: FK on the target table pointing to the intermediate
294
294
-`throughRelationJoin`: PK on the intermediate table
295
295
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 ONcontracts.author_id=authors.id
315
+
LEFT JOIN payments ONpayments.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
+
296
320
### Using UUIDs as primary keys
297
321
298
322
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