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
// Commit changes (makes current values the "original")
240
+
$breakfast->commitChanges();
241
+
// or use the alias:
242
+
$breakfast->syncOriginal();
243
+
244
+
// Revert a specific property change
245
+
$breakfast->setAttribute('price', 7.99);
246
+
$breakfast->revertChange('price'); // price is back to 5.99
247
+
248
+
// Revert all changes
249
+
$breakfast->setAttribute('name', 'Another Name');
250
+
$breakfast->setAttribute('price', 8.99);
251
+
$breakfast->revertChanges(); // All properties back to original
252
+
253
+
// Get original value
254
+
$originalName = $breakfast->getOriginal('name');
255
+
$allOriginal = $breakfast->getOriginal(); // Get all original values
256
+
```
257
+
258
+
### Checking if properties are set
259
+
260
+
The `isSet()` method checks if a property has been set. This is different from PHP's `isset()` because it considers `null` values and default values as "set":
261
+
262
+
```php
263
+
$breakfast = new Breakfast_Model();
264
+
265
+
// Properties with defaults are considered set
266
+
if ($breakfast->isSet('name')) { // true if 'name' has a default value
267
+
echo 'Name is set';
268
+
}
269
+
270
+
// Properties without defaults are not set until assigned
271
+
if (!$breakfast->isSet('price')) { // false - no default and not assigned
272
+
echo 'Price is not set';
273
+
}
274
+
275
+
// Setting a property to null still counts as set
276
+
$breakfast->setAttribute('price', null);
277
+
if ($breakfast->isSet('price')) { // true - explicitly set to null
278
+
echo 'Price is set (even though it\'s null)';
279
+
}
280
+
281
+
// PHP's isset() behaves differently with null
282
+
if (!isset($breakfast->price)) { // false - isset() returns false for null
283
+
echo 'PHP isset() returns false for null values';
284
+
}
285
+
```
286
+
287
+
**Key differences from PHP's `isset()`:**
288
+
-`isSet()` returns `true` for properties with default values
289
+
-`isSet()` returns `true` for properties explicitly set to `null`
290
+
-`isSet()` returns `false` only for properties that have no default and haven't been assigned
291
+
292
+
### Creating models from query data
293
+
294
+
Models can be created from database query results using the `fromData()` method:
295
+
296
+
```php
297
+
// From an object or array
298
+
$data = DB::get_row("SELECT * FROM breakfasts WHERE id = 1");
Config::throwInvalidArgumentException( "Unexpected type: '$type'. To support additional types, overload this method or use Definition casting." );
141
+
Config::throwInvalidArgumentException( "Unexpected type: '{$type[0]}'. To support additional types, overload this method or use Definition casting." );
134
142
}
135
143
}
136
144
@@ -235,10 +243,10 @@ public static function getPropertyDefinition( string $key ): ModelPropertyDefini
235
243
*
236
244
* @throws InvalidArgumentException If the property key is not a string.
0 commit comments