|
1 | 1 | --- |
2 | 2 | title: Using Custom Data Models |
3 | 3 | description: The dependency injector makes it easy to override entire data models in your Sprinkle. |
4 | | -wip: true |
5 | 4 | --- |
6 | 5 |
|
7 | | -Extending PHP classes is a little different from extending other types of entities. You cannot simply replace a class by redefining it in a custom Sprinkle. In fact, classes with the same name in two different Sprinkles would be treated as two different fully-qualified classes per the [PSR-4 standard](http://www.php-fig.org/psr/psr-4/). For example, if I loaded the Sprinkles `Account` and `Site`, and I had the following structure: |
| 6 | +Extending PHP classes is a little different from extending other types of entities. You cannot simply replace a class by redefining it in a custom Sprinkle. In fact, classes with the same name in two different Sprinkles would be treated as two different fully-qualified classes per the [PSR-4 standard](http://www.php-fig.org/psr/psr-4/). For example, if the `Account` Sprinkles and your `app` have the following structure: |
8 | 7 |
|
9 | 8 | ``` |
10 | | -sprinkles |
11 | | -├── account |
12 | | -│ └── src |
13 | | -│ └── Database |
14 | | -│ └── Models |
15 | | -│ └── User.php |
16 | | -├── site |
17 | | -│ └── src |
18 | | -│ └── Database |
19 | | -│ └── Models |
20 | | -│ └── User.php |
| 9 | +account |
| 10 | +└── src |
| 11 | + └── Database |
| 12 | + └── Models |
| 13 | + └── User.php |
| 14 | +
|
| 15 | +app |
| 16 | +└── src |
| 17 | + └── Database |
| 18 | + └── Models |
| 19 | + └── User.php |
21 | 20 | ``` |
22 | 21 |
|
23 | | -...then `User.php` in `site` would *not* override `User.php` in `account`. Rather, I'd have two different classes because both classes would have two different **namespace** : `\UserFrosting\Sprinkle\Account\Database\Models\User` and `\UserFrosting\Sprinkle\Site\Database\Models\User`. |
| 22 | +...then `User.php` in `app` would *not* override `User.php` in `account`. Rather, you'd have two different classes because both classes would have two different **namespace** : `\UserFrosting\Sprinkle\Account\Database\Models\User` and `\UserFrosting\Sprinkle\App\Database\Models\User`. |
24 | 23 |
|
25 | 24 | To actually override and replace the functionality of a class, we have two tools available: |
26 | 25 |
|
27 | 26 | ## Class Inheritance |
28 | 27 |
|
29 | | -We could, for example, define our `User` class in the `site` Sprinkle to inherit from the `User` class in `account` using the `extends` keyword: |
| 28 | +We could, for example, define our `User` class in the `app` Sprinkle to inherit from the `User` class in `account` using the `extends` keyword: |
30 | 29 |
|
31 | 30 | **app/src/Database/Models/User.php** : |
32 | 31 | ```php |
33 | 32 | <?php |
34 | 33 |
|
35 | | -namespace \UserFrosting\Sprinkle\MySprinkle\Database\Models; |
| 34 | +namespace \UserFrosting\Sprinkle\App\Database\Models; |
36 | 35 |
|
37 | 36 | class User extends \UserFrosting\Sprinkle\Account\Database\Models\User |
38 | 37 | { |
39 | 38 | // ... |
40 | 39 | } |
41 | 40 | ``` |
42 | 41 |
|
43 | | -Now, we can start using `\UserFrosting\Sprinkle\Site\Database\Models\User` to extend the functionality provided by the `User` class in the `Account` sprinkle. |
| 42 | +Now, we can start using `\UserFrosting\Sprinkle\App\Database\Models\User` to extend the functionality provided by the `User` class in the `Account` sprinkle. |
44 | 43 |
|
45 | 44 | ## Dynamic Model Mapper |
46 | 45 |
|
47 | | -Of course, the limitations of object-oriented inheritance becomes clear when you want to change the behavior of the original class in other places where it has been used. For example, if I extended `Account\Database\Models\User` and redefined the `onLogin` method in my `Site\Database\Models\User` class, this would let me use `Site\Database\Models\User` going forward in any code I write in the `site` Sprinkle. However, it wouldn't affect references to `User` in the `account` Sprinkle - they would still be referring to the base class. |
| 46 | +Of course, the limitations of object-oriented inheritance becomes clear when you want to change the behavior of the original class in other places where it has been used. For example, if I extended `Account\Database\Models\User` and redefined the `onLogin` method in my `App\Database\Models\User` class, this would let me use `App\Database\Models\User` going forward in any code I write in the `app` Sprinkle. However, it wouldn't affect references to `User` in the `account` Sprinkle - they would still be referring to the base class. |
48 | 47 |
|
49 | 48 | To allow this sort of "_retroactive extendability_", the Dependency Injector can be used to resolves interface identifiers to specific class names at runtime [through Interface Binding and custom Autowiring](/dependency-injection/the-di-container#binding-interfaces). |
50 | 49 |
|
@@ -81,16 +80,15 @@ public function __invoke(Request $request, Response $response): Response |
81 | 80 |
|
82 | 81 | The following interface-model association are defined by default in the *Account* sprinkle : |
83 | 82 |
|
84 | | -| Interface | Model | |
85 | | -| --------------------------------------------------------------------------------- | ------------------------------------------------------------- | |
86 | | -| `UserFrosting\Sprinkle\Account\Database\Models\Interfaces\ActivityInterface` | `UserFrosting\Sprinkle\Account\Database\Models\Activity` | |
87 | | -| `UserFrosting\Sprinkle\Account\Database\Models\Interfaces\GroupInterface` | `UserFrosting\Sprinkle\Account\Database\Models\Group` | |
88 | | -| `UserFrosting\Sprinkle\Account\Database\Models\Interfaces\PasswordResetInterface` | `UserFrosting\Sprinkle\Account\Database\Models\PasswordReset` | |
89 | | -| `UserFrosting\Sprinkle\Account\Database\Models\Interfaces\PermissionInterface` | `UserFrosting\Sprinkle\Account\Database\Models\Permission` | |
90 | | -| `UserFrosting\Sprinkle\Account\Database\Models\Interfaces\PersistenceInterface` | `UserFrosting\Sprinkle\Account\Database\Models\Persistence` | |
91 | | -| `UserFrosting\Sprinkle\Account\Database\Models\Interfaces\RoleInterface` | `UserFrosting\Sprinkle\Account\Database\Models\Role` | |
92 | | -| `UserFrosting\Sprinkle\Account\Database\Models\Interfaces\UserInterface` | `UserFrosting\Sprinkle\Account\Database\Models\User` | |
93 | | -| `UserFrosting\Sprinkle\Account\Database\Models\Interfaces\VerificationInterface` | `UserFrosting\Sprinkle\Account\Database\Models\Verification` | |
| 83 | +| Interface | Model | |
| 84 | +| -------------------------------------------------------------------------------------- | ------------------------------------------------------------------ | |
| 85 | +| `UserFrosting\Sprinkle\Account\Database\Models\Interfaces\ActivityInterface` | `UserFrosting\Sprinkle\Account\Database\Models\Activity` | |
| 86 | +| `UserFrosting\Sprinkle\Account\Database\Models\Interfaces\GroupInterface` | `UserFrosting\Sprinkle\Account\Database\Models\Group` | |
| 87 | +| `UserFrosting\Sprinkle\Account\Database\Models\Interfaces\PermissionInterface` | `UserFrosting\Sprinkle\Account\Database\Models\Permission` | |
| 88 | +| `UserFrosting\Sprinkle\Account\Database\Models\Interfaces\PersistenceInterface` | `UserFrosting\Sprinkle\Account\Database\Models\Persistence` | |
| 89 | +| `UserFrosting\Sprinkle\Account\Database\Models\Interfaces\RoleInterface` | `UserFrosting\Sprinkle\Account\Database\Models\Role` | |
| 90 | +| `UserFrosting\Sprinkle\Account\Database\Models\Interfaces\UserInterface` | `UserFrosting\Sprinkle\Account\Database\Models\User` | |
| 91 | +| `UserFrosting\Sprinkle\Account\Database\Models\Interfaces\UserVerificationInterface` | `UserFrosting\Sprinkle\Account\Database\Models\UserVerification` | |
94 | 92 |
|
95 | 93 | ## Overwriting existing map |
96 | 94 |
|
|
0 commit comments