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
* add BaseModelFrozenAttributes
* add BaseModelFrozenAttributes
* add BaseModelFrozenAttributes
* add BaseModelFrozenAttributes replace toArray with __toString and return json to ease the @mixin usage
* add BaseModelFrozenAttributes replace toArray with __toString and return json to ease the @mixin usage
* add BaseModelFrozenAttributes add getFrozen to BaseModel
* add BaseModelFrozenAttributes update README.md
* add BaseModelFrozenAttributes update README.md
* add BaseModelFrozenAttributes handle return type for json_decode
* Make the stdClass read only also for its content and update README.md
* README.md mention that Model is retrievable form db for frozen attributes
---------
Copy file name to clipboardExpand all lines: README.md
+49Lines changed: 49 additions & 0 deletions
Original file line number
Diff line number
Diff line change
@@ -75,6 +75,55 @@ For model properties autocomplete:
75
75
- add in its class dock block using **@property** all the models properties/attributes/columns
76
76
- add in the model's class dock block **@property ChildBaseModelAttributes $a** and **@mixin ChildBaseModelAttributes**
77
77
- use **$model->a->** instead of **$model->**
78
+
- BaseModelFrozenAttributes can be also extended on the same logic and used for model read only situations - DTO without setters (Reflection or Closure binding usage will retrieve/set private stdClass not Model - but the model can be retrieved from DB by its primary key that is readable in this frozen model):
79
+
```php
80
+
#OperationModel example for BaseModelFrozenAttributes
81
+
public function getFrozen(): OperationFrozenAttributes
82
+
{
83
+
return parent::getFrozen(); // this is needed for autocompletion and will include also the loaded relations
84
+
// or
85
+
return new OperationFrozenAttributes((clone $this)->forceFill($this->toArray()));
86
+
// or just attributes without loaded relations
87
+
return new OperationFrozenAttributes((clone $this)->forceFill($this->attributesToArray()));
88
+
}
89
+
```
90
+
91
+
```php
92
+
#OperationService example for BaseModelAttributes and BaseModelFrozenAttributes
93
+
public function someFunction(): void
94
+
{
95
+
// BaseModelAttributes
96
+
echo $this->model-a->value; // has autocomplete - will print for example 1
97
+
echo $this->model-a->value = 10; // has autocomplete - will print 10
98
+
echo $this->model->value; // has autocomplete - will print 10
99
+
100
+
// BaseModelFrozenAttributes
101
+
$dto = $this->model->getFrozen();
102
+
echo $dto->client_id; // has autocomplete - will print for example 1
103
+
$dto->client_id = 4; // Exception: Dynamic properties are forbidden.
104
+
105
+
if (isset($dto->client)) {
106
+
/** @var ClientFrozenAttributes $client */
107
+
// $client will be an stdClass that has autocomplete like a ClientFrozenAttributes
108
+
$client = $dto->client;
109
+
echo $client->name; // has autocomplete - will print for example 'name'
110
+
$client->name = 'text'; // NO Exception
111
+
echo $client->name; // will print 'text'
112
+
// $client changes can happen, but they will not be persisted in the $dto ($client is a stdClass clone)
113
+
echo $dto->client->name; // will print 'name'
114
+
}
115
+
116
+
foreach (($dto->products ?? []) as $k => $product) {
117
+
/** @var ProductFrozenAttributes $product */
118
+
// $product will be an stdClass that has autocompletes like a ProductFrozenAttributes
119
+
echo $product->value; // has autocomplete - will print for example 1
120
+
$product->value = 2; // NO Exception
121
+
echo $product->value; // will print 2
122
+
// $product changes can happen, but they will not be persisted in the $dto ($product is a stdClass clone)
0 commit comments