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
+51Lines changed: 51 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -447,6 +447,57 @@ final class Book
447
447
}
448
448
```
449
449
450
+
### Hidden properties
451
+
452
+
Sensitive properties can be marked with the {b`#[Tempest\Mapper\Hidden]`} attribute to exclude them from SELECT queries. This is useful for properties like passwords, API keys, or other sensitive data that should not be fetched or exposed by default.
453
+
454
+
```php
455
+
use Tempest\Database\IsDatabaseModel;
456
+
use Tempest\Mapper\Hidden;
457
+
458
+
final class User
459
+
{
460
+
use IsDatabaseModel;
461
+
462
+
public string $email;
463
+
464
+
#[Hidden]
465
+
public string $password;
466
+
467
+
#[Hidden]
468
+
public ?string $apiKey = null;
469
+
}
470
+
```
471
+
472
+
Hidden properties are still included in INSERT and UPDATE queries, allowing them to be persisted to the database.
473
+
474
+
To explicitly include hidden fields in a query, use the `include()` method on the query builder:
Unlike {b`#[Virtual]`} which marks computed properties that don't exist in the database, {b`#[Hidden]`} is for real database columns that should be protected from accidental exposure.
495
+
:::
496
+
497
+
:::info
498
+
The {b`#[Hidden]`} attribute also excludes properties from serialization. See the [mapper documentation](../2-features/01-mapper.md#hiding-properties-from-serialization) for more information.
499
+
:::
500
+
450
501
### The `IsDatabaseModel` trait
451
502
452
503
The {b`Tempest\Database\IsDatabaseModel`} trait provides an active record pattern. This trait enables database interaction via static methods on the model class itself.
Properties marked with the {b`#[Tempest\Mapper\Hidden]`} attribute are excluded from serialization. This is useful for sensitive data like passwords or API keys that should never be exposed in arrays or JSON responses.
77
+
78
+
```php
79
+
use Tempest\Mapper\Hidden;
80
+
81
+
final class User
82
+
{
83
+
public string $email;
84
+
85
+
#[Hidden]
86
+
public string $password;
87
+
}
88
+
```
89
+
90
+
When serializing, hidden properties are automatically excluded:
91
+
92
+
```php
93
+
$user = new User();
94
+
$user->email = 'user@example.com';
95
+
$user->password = 'secret';
96
+
97
+
$array = map($user)->toArray();
98
+
// ['email' => 'user@example.com']
99
+
100
+
$json = map($user)->toJson();
101
+
// {"email":"user@example.com"}
102
+
```
103
+
104
+
:::info
105
+
The {b`#[Hidden]`} attribute also excludes properties from database SELECT queries. See the [database documentation](../1-essentials/03-database.md#hidden-properties) for more information.
106
+
:::
107
+
74
108
### Overriding field names
75
109
76
110
When mapping from an array to an object, Tempest uses the property names of the target class to map the data. If a property name doesn't match a key in the source array, use the {b`#[Tempest\Mapper\MapFrom]`} attribute to specify the source key to map to the property.
0 commit comments