Skip to content

Commit

Permalink
Model hasChanged array
Browse files Browse the repository at this point in the history
  • Loading branch information
Jurigag committed Mar 23, 2017
1 parent 7eb85bf commit 4ad2aa6
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
@@ -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)

Expand Down
23 changes: 22 additions & 1 deletion phalcon/mvc/model.zep
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
*<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 @@ -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;
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 @@ -299,4 +299,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 4ad2aa6

Please sign in to comment.