From 380164d45358f7bcf858376c4fff84e60d8e9292 Mon Sep 17 00:00:00 2001 From: Ryan Mitchell Date: Fri, 1 Mar 2024 10:47:19 +0000 Subject: [PATCH 1/3] Add isDirty / isClean / getOriginal docs --- content/collections/extending-docs/data.md | 41 ++++++++++++++++++++++ 1 file changed, 41 insertions(+) diff --git a/content/collections/extending-docs/data.md b/content/collections/extending-docs/data.md index ec3ee428b..91565356e 100644 --- a/content/collections/extending-docs/data.md +++ b/content/collections/extending-docs/data.md @@ -268,3 +268,44 @@ $entry->toAugmentedCollection(['title', 'collection']); ``` The `toAugmentedArray` method does the same as `toAugmentedCollection`, except that it returns an array. + + +### Checking for data changes + +Statamic provides isDirty, isClean, and getOriginal methods to let you see what data has been changed in your item. + +The `isDirty` method checks if any of the item's data has been changed since it was last saved. You may pass a specific attribute name or an array of attributes to the `isDirty` method to determine if any of the attributes are "dirty". The `isClean` method will determine if an attribute has remained unchanged since the model was retrieved. This method also accepts an optional attribute argument: + +```php +$entry->title; // "Post title" +$entry->image; // "/path/to/image.jpg" + +$entry->title = 'New Title'; + +$entry->isDirty(); // true +$entry->isDirty('title'); // true +$entry->isDirty('image'); // false +$entry->isDirty(['image', 'title']); // true + +$entry->isClean(); // false +$entry->isClean('title'); // false +$entry->isClean('image'); // true +$entry->isClean(['image', 'title']); // false + +$entry->save(); + +$entry->isDirty(); // false +$entry->isClean(); // true +``` + +The `getOriginal` method returns an array containing the original attributes of the item regardless of any changes to the model since it was retrieved. This method also accepts an optional attribute argument: + +```php +$entry->title; // "Post title" +$entry->image; // "/path/to/image.jpg" + +$entry->title = 'New Title'; + +$entry->getOriginal('title'); // "Post title" +$entry->getOriginal(); // ["Post title", "/path/to/image.jpg"] +``` From 0c85fdcf215b359a59f09569f966595577a98ada Mon Sep 17 00:00:00 2001 From: Ryan Mitchell Date: Fri, 1 Mar 2024 10:48:47 +0000 Subject: [PATCH 2/3] item not model --- content/collections/extending-docs/data.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/content/collections/extending-docs/data.md b/content/collections/extending-docs/data.md index 91565356e..55c1f77c5 100644 --- a/content/collections/extending-docs/data.md +++ b/content/collections/extending-docs/data.md @@ -274,7 +274,7 @@ The `toAugmentedArray` method does the same as `toAugmentedCollection`, except t Statamic provides isDirty, isClean, and getOriginal methods to let you see what data has been changed in your item. -The `isDirty` method checks if any of the item's data has been changed since it was last saved. You may pass a specific attribute name or an array of attributes to the `isDirty` method to determine if any of the attributes are "dirty". The `isClean` method will determine if an attribute has remained unchanged since the model was retrieved. This method also accepts an optional attribute argument: +The `isDirty` method checks if any of the item's data has been changed since it was last saved. You may pass a specific attribute name or an array of attributes to the `isDirty` method to determine if any of the attributes are "dirty". The `isClean` method will determine if an attribute has remained unchanged since the item was retrieved. This method also accepts an optional attribute argument: ```php $entry->title; // "Post title" @@ -298,7 +298,7 @@ $entry->isDirty(); // false $entry->isClean(); // true ``` -The `getOriginal` method returns an array containing the original attributes of the item regardless of any changes to the model since it was retrieved. This method also accepts an optional attribute argument: +The `getOriginal` method returns an array containing the original attributes of the item regardless of any changes to the item since it was retrieved. This method also accepts an optional attribute argument: ```php $entry->title; // "Post title" From c411b1df48a39ce6cc52473ab07aa0bec622c099 Mon Sep 17 00:00:00 2001 From: Duncan McClean Date: Wed, 6 Mar 2024 11:57:08 +0000 Subject: [PATCH 3/3] tweak the first paragraph include backticks around method names --- content/collections/extending-docs/data.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/content/collections/extending-docs/data.md b/content/collections/extending-docs/data.md index 55c1f77c5..4a7bba64d 100644 --- a/content/collections/extending-docs/data.md +++ b/content/collections/extending-docs/data.md @@ -272,7 +272,7 @@ The `toAugmentedArray` method does the same as `toAugmentedCollection`, except t ### Checking for data changes -Statamic provides isDirty, isClean, and getOriginal methods to let you see what data has been changed in your item. +Statamic provides the `isDirty`, `isClean`, and `getOriginal` methods so you can determine what data has changed from when the item was originally retrieved. The `isDirty` method checks if any of the item's data has been changed since it was last saved. You may pass a specific attribute name or an array of attributes to the `isDirty` method to determine if any of the attributes are "dirty". The `isClean` method will determine if an attribute has remained unchanged since the item was retrieved. This method also accepts an optional attribute argument: