From 4ad2aa6ca82b9d744b1640fb86aaabfcbf8939ff Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Wojciech=20=C5=9Alawski?= Date: Wed, 15 Mar 2017 16:32:23 +0100 Subject: [PATCH] Model hasChanged array --- CHANGELOG.md | 1 + phalcon/mvc/model.zep | 23 ++++++++++++++++++- tests/unit/Mvc/Model/SnapshotTest.php | 33 +++++++++++++++++++++++++++ 3 files changed, 56 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2d0956544db..b2619ee5b21 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,4 +1,5 @@ # [3.2.0](https://github.com/phalcon/cphalcon/releases/tag/v3.2.0) (2017-XX-XX) +- Updated `Phalcon\Mvc\Model::hasChanged` to correctly use it with arrays [#12669](https://github.com/phalcon/cphalcon/issues/12669) # [3.1.1](https://github.com/phalcon/cphalcon/releases/tag/v3.1.1) (2017-XX-XX) diff --git a/phalcon/mvc/model.zep b/phalcon/mvc/model.zep index 9ef68e760a5..1f199ac9774 100644 --- a/phalcon/mvc/model.zep +++ b/phalcon/mvc/model.zep @@ -3850,10 +3850,25 @@ abstract class Model implements EntityInterface, ModelInterface, ResultInterface /** * Check if a specific attribute has changed * This only works if the model is keeping data snapshots + * + * + * $robot = new Robots(); + * + * $robot->type = "mechanical"; + * $robot->name = "Astro Boy"; + * $robot->year = 1952; + * + * $robot->create(); + * $robot->type = "hydraulic"; + * $hasChanged = $robot->hasChanged("type"); // returns true + * $hasChanged = $robot->hasChanged(["type", "name"]); // returns true + * $hasChanged = $robot->hasChanged(["type", "name", true]); // returns false + * * * @param string|array fieldName + * @param boolean allFields */ - public function hasChanged(var fieldName = null) -> boolean + public function hasChanged(var fieldName = null, boolean allFields = false) -> boolean { var changedFields; @@ -3864,6 +3879,12 @@ abstract class Model implements EntityInterface, ModelInterface, ResultInterface */ if typeof fieldName == "string" { return in_array(fieldName, changedFields); + } elseif typeof fieldName == "array" { + if allFields { + return array_intersect(fieldName, changedFields) == fieldName; + } + + return count(array_intersect(fieldName, changedFields)) > 0; } return count(changedFields) > 0; diff --git a/tests/unit/Mvc/Model/SnapshotTest.php b/tests/unit/Mvc/Model/SnapshotTest.php index 5e0f6ed0652..1b2d2879717 100644 --- a/tests/unit/Mvc/Model/SnapshotTest.php +++ b/tests/unit/Mvc/Model/SnapshotTest.php @@ -299,4 +299,37 @@ function () { } ); } + + /** + * When model is refreshed snapshot should be updated + * + * @issue 12669 + * @author Wojciech Ĺšlawski + * @since 2017-03-15 + */ + public function testIssue12669() + { + $this->specify( + 'hasChanged method for array argument is not working correctly', + function () { + $this->setUpModelsManager(); + $robots = new Robots( + [ + 'name' => 'test', + 'year' => 2017, + 'datetime' => (new \DateTime())->format('Y-m-d'), + 'text' => 'asd', + ] + ); + + expect($robots->create())->true(); + $robots->name = 'test2'; + expect($robots->hasChanged(['name', 'year']))->equals(true); + expect($robots->hasChanged(['text', 'year']))->equals(false); + expect($robots->hasChanged(['name', 'year'], true))->equals(false); + $robots->year = 2018; + expect($robots->hasChanged(['name', 'year'], true))->equals(true); + } + ); + } }