Skip to content

Commit fa6f797

Browse files
committed
Improve docs
1 parent cc79fc4 commit fa6f797

File tree

1 file changed

+39
-19
lines changed

1 file changed

+39
-19
lines changed

src/Web/Documentation/content/main/1-framework/05-models.md

Lines changed: 39 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -15,31 +15,29 @@ use App\Author;
1515

1616
final class Book
1717
{
18-
public function __construct(
19-
#[Length(min: 1, max: 120)]
20-
public string $title,
18+
#[Length(min: 1, max: 120)]
19+
public string $title;
2120

22-
public Author $author,
21+
public Author $author;
2322

24-
/** @var \App\Chapter[] */
25-
public array $chapters = [],
26-
) {}
23+
/** @var \App\Chapter[] */
24+
public array $chapters = [];
2725
}
2826
```
2927

30-
Retrieving and persisting a model from a data source is done via Tempest's `{php}map()` function:
28+
Retrieving and persisting a model from a data source is done via Tempest's `map()` function:
3129

3230
```php
3331
use function Tempest\map;
3432

3533
map('path/to/books.json')->collection->to(Book::class);
3634

37-
map($book)->to(MapTo::JSON);
35+
map($book)->toJson();
3836
```
3937

4038
## Database models
4139

42-
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:
4341

4442
```php
4543
// app/Book.php
@@ -53,15 +51,13 @@ final class Book implements DatabaseModel
5351
{
5452
use IsDatabaseModel;
5553

56-
public function __construct(
57-
#[Length(min: 1, max: 120)]
58-
public string $title,
54+
#[Length(min: 1, max: 120)]
55+
public string $title;
5956

60-
public ?Author $author = null,
57+
public ?Author $author = null;
6158

62-
/** @var \App\Chapter[] */
63-
public array $chapters = [],
64-
) {}
59+
/** @var \App\Chapter[] */
60+
public array $chapters = [];
6561
}
6662
```
6763

@@ -79,7 +75,7 @@ return new SQLiteConfig(
7975
);
8076
```
8177

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:
8379

8480
```php
8581
// app/Config/database.config.php
@@ -98,7 +94,7 @@ return new MysqlConfig(
9894

9995
## Migrations
10096

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:
10298

10399
```php
104100
// app/CreateBookTable.php
@@ -236,6 +232,30 @@ $books = Book::query()
236232
->all();
237233
```
238234

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+
239259
## Model relations
240260

241261
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

Comments
 (0)