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: README.md
+36Lines changed: 36 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -32,6 +32,42 @@ A list of the utopia/php concepts and their relevant equivalent using the differ
32
32
33
33
Attribute filters are functions that manipulate attributes before saving them to the database and after retrieving them from the database. You can add filters using the `Database::addFilter($name, $encode, $decode)` where `$name` is the name of the filter that we can add later to attribute `filters` array. `$encode` and `$decode` are the functions used to encode and decode the attribute, respectively. There are also instance-level filters that can only be defined while constructing the `Database` instance. Instance level filters override the static filters if they have the same name.
34
34
35
+
### Custom Document Types
36
+
37
+
The database library supports mapping custom document classes to specific collections, enabling a domain-driven design approach. This allows you to create collection-specific classes (like `User`, `Post`, `Product`) that extend the base `Document` class with custom methods and business logic.
38
+
39
+
```php
40
+
// Define a custom document class
41
+
class User extends Document
42
+
{
43
+
public function getEmail(): string
44
+
{
45
+
return $this->getAttribute('email', '');
46
+
}
47
+
48
+
public function isAdmin(): bool
49
+
{
50
+
return $this->getAttribute('role') === 'admin';
51
+
}
52
+
}
53
+
54
+
// Register the custom type
55
+
$database->setDocumentType('users', User::class);
56
+
57
+
// Now all documents from 'users' collection are User instances
0 commit comments