Skip to content

Commit e63d48a

Browse files
committed
docs: update dto serialization docs
1 parent 3300c1f commit e63d48a

File tree

1 file changed

+26
-19
lines changed

1 file changed

+26
-19
lines changed

docs/1-essentials/03-database.md

Lines changed: 26 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -304,34 +304,41 @@ final class User
304304

305305
The encryption key is taken from the `SIGNING_KEY` environment variable.
306306

307-
### DTO properties
307+
### Data transfer object properties
308308

309-
Sometimes, you might want to store data objects as-is in a table, without there needing to be a relation to another table. To do so, it's enough to add a serializer and caster to the data object's class, and Tempest will know that these objects aren't meant to be treated as database models. Next, you can store the object's data as a json field on the table (see [migrations](#migrations) for more info).
309+
It's possible to store objects as-is in a `json` column, when they don't need to be part of your database schema.
310+
311+
To do this, you may simply mark these objects with the `#[Tempest\Mapper\SerializeAs]` and specify a name for this object to be serialized as. Not that this name must uniquely correspond to a single class.
310312

311313
```php
312-
use Tempest\Database\IsDatabaseModel;
313-
use Tempest\Mapper\CastWith;
314-
use Tempest\Mapper\SerializeWith;
315-
use Tempest\Mapper\Casters\DtoCaster;
316-
use Tempest\Mapper\Serializers\DtoSerializer;
314+
use Tempest\Mapper\SerializeAs;
317315

318-
final class DebugItem
316+
final class User implements Authenticatable
319317
{
320-
use IsDatabaseModel;
321-
322-
/* … */
323-
324-
public Backtrace $backtrace,
318+
public PrimaryKey $id;
319+
320+
public function __construct(
321+
public string $email,
322+
#[Hashed, SensitiveParameter]
323+
public ?string $password,
324+
public Settings $settings,
325+
) {}
325326
}
326327

327-
#[CastWith(DtoCaster::class)]
328-
#[SerializeWith(DtoSerializer::class)]
329-
final class Backtrace
328+
#[SerializeAs('user_settings')]
329+
final class Settings
330330
{
331-
// This object won't be considered a relation,
332-
// but rather serialized and stored in a JSON column.
331+
public function __construct(
332+
public readonly Theme $theme,
333+
public readonly bool $hide_sidebar_by_default,
334+
) {}
335+
}
333336

334-
public array $frames = [];
337+
enum Theme: string
338+
{
339+
case DARK = 'dark';
340+
case LIGHT = 'light';
341+
case AUTO = 'auto';
335342
}
336343
```
337344

0 commit comments

Comments
 (0)