Skip to content

Commit

Permalink
Model hasChanged array
Browse files Browse the repository at this point in the history
  • Loading branch information
Jurigag committed Apr 12, 2017
1 parent 504165a commit 090064c
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
- Fixed Dispatcher forwarding when handling exception [#11819](https://github.com/phalcon/cphalcon/issues/11819), [#12154](https://github.com/phalcon/cphalcon/issues/12154)
- Fixed params view scope for PHP 7 [#12648](https://github.com/phalcon/cphalcon/issues/12648)
- Fixed `Phalcon\Mvc\Micro::handle` to prevent attemps to send response twice [#12668](https://github.com/phalcon/cphalcon/pull/12668)
- Updated `Phalcon\Mvc\Model::hasChanged` to correctly use it with arrays [#12669](https://github.com/phalcon/cphalcon/issues/12669)

# [3.1.2](https://github.com/phalcon/cphalcon/releases/tag/v3.1.2) (2017-04-05)
- Fixed PHP 7.1 issues [#12055](https://github.com/phalcon/cphalcon/issues/12055)
Expand Down
23 changes: 22 additions & 1 deletion phalcon/mvc/model.zep
Original file line number Diff line number Diff line change
Expand Up @@ -3863,9 +3863,24 @@ abstract class Model implements EntityInterface, ModelInterface, ResultInterface
* Check if a specific attribute has changed
* This only works if the model is keeping data snapshots
*
*<code>
* $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
*</code>
*
* @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;

Expand All @@ -3876,6 +3891,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;
Expand Down
33 changes: 33 additions & 0 deletions tests/unit/Mvc/Model/SnapshotTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -402,4 +402,37 @@ function () {
}
);
}

/**
* When model is refreshed snapshot should be updated
*
* @issue 12669
* @author Wojciech Ślawski <[email protected]>
* @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);
}
);
}
}

0 comments on commit 090064c

Please sign in to comment.