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
Because database persistence is a pretty common use case, Tempest provides an implementation for models that should interact with the database specifically. Any model class can implement the `{php}DatabaseModel` interface, and use the `{php}IsDatabaseModel` trait like so:
40
+
Because database persistence is a pretty common use case, Tempest provides an implementation for models that should interact with the database specifically. Any model class can implement the `DatabaseModel` interface, and use the `IsDatabaseModel` trait like so:
43
41
44
42
```php
45
43
// app/Book.php
@@ -53,15 +51,13 @@ final class Book implements DatabaseModel
53
51
{
54
52
use IsDatabaseModel;
55
53
56
-
public function __construct(
57
-
#[Length(min: 1, max: 120)]
58
-
public string $title,
54
+
#[Length(min: 1, max: 120)]
55
+
public string $title;
59
56
60
-
public ?Author $author = null,
57
+
public ?Author $author = null;
61
58
62
-
/** @var \App\Chapter[] */
63
-
public array $chapters = [],
64
-
) {}
59
+
/** @var \App\Chapter[] */
60
+
public array $chapters = [];
65
61
}
66
62
```
67
63
@@ -79,7 +75,7 @@ return new SQLiteConfig(
79
75
);
80
76
```
81
77
82
-
Tempest has three available database drivers: `{php}SQLiteDriver`, `{php}MySqlDriver`, and `{php}PostgreSqlDriver`. Note that you can use environment variables in config files like so:
78
+
Tempest has three available database drivers: `SQLiteConfig`, `MysqlConfig`, and `PostgresConfig`. Note that you can use environment variables in config files like so:
83
79
84
80
```php
85
81
// app/Config/database.config.php
@@ -98,7 +94,7 @@ return new MysqlConfig(
98
94
99
95
## Migrations
100
96
101
-
Migrations are used to manage database tables that hold persisted model data. Migrations are discovered, so you can create them wherever you like, as long as they implement the `{php}Migration` interface:
97
+
Migrations are used to manage database tables that hold persisted model data. Migrations are discovered, so you can create them wherever you like, as long as they implement the `DatabaseMigration` interface:
102
98
103
99
```php
104
100
// app/CreateBookTable.php
@@ -236,6 +232,30 @@ $books = Book::query()
236
232
->all();
237
233
```
238
234
235
+
## Virtual properties
236
+
237
+
By default, all public properties are considered to be part of the model's query fields. In order to exclude a field from the database mapper, you should mark it as virtual:
238
+
239
+
```php
240
+
use Tempest\Database\Virtual;
241
+
use Tempest\Database\DatabaseModel;
242
+
use Tempest\Database\IsDatabaseModel;
243
+
244
+
class Book implements DatabaseModel
245
+
{
246
+
use IsDatabaseModel;
247
+
248
+
// …
249
+
250
+
public DateTimeImmutable $publishedAt;
251
+
252
+
#[Virtual]
253
+
public DateTimeImmutable $saleExpiresAt {
254
+
get => $this->publishedAt->add(new DateInterval('P5D'));
255
+
}
256
+
}
257
+
```
258
+
239
259
## Model relations
240
260
241
261
Relations between models are primarily determined by a model's property type information. Tempest tries to infer as much information as possible from plain PHP code, so that you don't have to provide unnecessary configuration.
0 commit comments