Skip to content

Commit 937a749

Browse files
committed
docs: add Hidden attribute documentation
1 parent 4017269 commit 937a749

2 files changed

Lines changed: 85 additions & 0 deletions

File tree

docs/1-essentials/03-database.md

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -447,6 +447,57 @@ final class Book
447447
}
448448
```
449449

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:
475+
476+
```php
477+
// Password is not included in the query
478+
$user = User::select()->where('email', $email)->first();
479+
480+
// Password is explicitly included
481+
$user = User::select()
482+
->include('password')
483+
->where('email', $email)
484+
->first();
485+
486+
// Multiple hidden fields can be included
487+
$user = User::select()
488+
->include('password', 'apiKey')
489+
->where('email', $email)
490+
->first();
491+
```
492+
493+
:::info
494+
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+
450501
### The `IsDatabaseModel` trait
451502

452503
The {b`Tempest\Database\IsDatabaseModel`} trait provides an active record pattern. This trait enables database interaction via static methods on the model class itself.

docs/2-features/01-mapper.md

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,40 @@ $array = map($book)->toArray();
7171
$json = map($book)->toJson();
7272
```
7373

74+
### Hiding properties from serialization
75+
76+
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+
74108
### Overriding field names
75109

76110
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

Comments
 (0)