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
+26-19Lines changed: 26 additions & 19 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -304,34 +304,41 @@ final class User
304
304
305
305
The encryption key is taken from the `SIGNING_KEY` environment variable.
306
306
307
-
### DTO properties
307
+
### Data transfer object properties
308
308
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.
310
312
311
313
```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;
317
315
318
-
final class DebugItem
316
+
final class User implements Authenticatable
319
317
{
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
+
) {}
325
326
}
326
327
327
-
#[CastWith(DtoCaster::class)]
328
-
#[SerializeWith(DtoSerializer::class)]
329
-
final class Backtrace
328
+
#[SerializeAs('user_settings')]
329
+
final class Settings
330
330
{
331
-
// This object won't be considered a relation,
332
-
// but rather serialized and stored in a JSON column.
0 commit comments