Skip to content

Commit

Permalink
Added way to disable setters in model assign
Browse files Browse the repository at this point in the history
  • Loading branch information
Jurigag authored and sergeyklay committed Jun 18, 2017
1 parent 7d43145 commit da16160
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 4 deletions.
4 changes: 4 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,10 @@
"orm.update_snapshot_on_save": {
"type": "bool",
"default": true
},
"orm.disable_assign_setters": {
"type": "bool",
"default": false
}
},
"destructors": {
Expand Down
27 changes: 23 additions & 4 deletions phalcon/mvc/model.zep
Original file line number Diff line number Diff line change
Expand Up @@ -445,6 +445,19 @@ abstract class Model implements EntityInterface, ModelInterface, ResultInterface
* "year",
* ]
* );
*
* // By default assign method will use setters if exist, you can disable it by using ini_set to directly use properties
*
* ini_set("phalcon.orm.disable_assign_setters", true);
*
* $robot->assign(
* $_POST,
* null,
* [
* "name",
* "year",
* ]
* );
* </code>
*
* @param array data
Expand All @@ -454,7 +467,9 @@ abstract class Model implements EntityInterface, ModelInterface, ResultInterface
*/
public function assign(array! data, var dataColumnMap = null, var whiteList = null) -> <Model>
{
var key, keyMapped, value, attribute, attributeField, metaData, columnMap, dataMapped;
var key, keyMapped, value, attribute, attributeField, metaData, columnMap, dataMapped, disableAssignSetters;

let disableAssignSetters = globals_get("orm.disable_assign_setters");

// apply column map for data, if exist
if typeof dataColumnMap == "array" {
Expand Down Expand Up @@ -507,7 +522,7 @@ abstract class Model implements EntityInterface, ModelInterface, ResultInterface
}

// Try to find a possible getter
if !this->_possibleSetter(attributeField, value) {
if disableAssignSetters || !this->_possibleSetter(attributeField, value) {
let this->{attributeField} = value;
}
}
Expand Down Expand Up @@ -3866,7 +3881,7 @@ abstract class Model implements EntityInterface, ModelInterface, ResultInterface
{
return this->_oldSnapshot;
}

/**
* Check if a specific attribute has changed
* This only works if the model is keeping data snapshots
Expand Down Expand Up @@ -4664,7 +4679,7 @@ abstract class Model implements EntityInterface, ModelInterface, ResultInterface
var disableEvents, columnRenaming, notNullValidations,
exceptionOnFailedSave, phqlLiterals, virtualForeignKeys,
lateStateBinding, castOnHydrate, ignoreUnknownColumns,
updateSnapshotOnSave;
updateSnapshotOnSave, disableAssignSetters;

/**
* Enables/Disables globally the internal events
Expand Down Expand Up @@ -4732,6 +4747,10 @@ abstract class Model implements EntityInterface, ModelInterface, ResultInterface
if fetch updateSnapshotOnSave, options["updateSnapshotOnSave"] {
globals_set("orm.update_snapshot_on_save", updateSnapshotOnSave);
}

if fetch disableAssignSetters, options["disableAssignSetters"] {
globals_set("orm.disable_assign_setters", disableAssignSetters);
}
}

/**
Expand Down
18 changes: 18 additions & 0 deletions tests/_data/models/Robots.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,16 @@
*/
class Robots extends Model
{
/**
* @var bool
*/
public $wasSetterUsed = false;

/**
* @var string
*/
protected $name;

public function initialize()
{
$this->keepSnapshots(true);
Expand All @@ -42,4 +52,12 @@ public function initialize()
]
);
}

public function setName($name)
{
$this->name = $name;
$this->wasSetterUsed = true;

return $this;
}
}
39 changes: 39 additions & 0 deletions tests/unit/Mvc/ModelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use DateTime;
use Helper\ModelTrait;
use Phalcon\Mvc\Model;
use Phalcon\Mvc\Model\Message;
use Phalcon\Test\Models\Users;
use Phalcon\Cache\Backend\Apc;
Expand Down Expand Up @@ -671,4 +672,42 @@ function () {
}
);
}

/**
* Tests disabling assign setters
*
* @issue 12645
* @author Wojciech Ślawski <jurigag@gmail.com>
* @since 2017-03-23
*/
public function testAssignSettersDisabled()
{
$this->specify(
'Disabling setters in assign is not working',
function () {
$robots = new Robots(
[
'name' => 'test',
]
);
expect($robots->wasSetterUsed)->true();
Model::setup(
[
'disableAssignSetters' => true,
]
);
$robots = new Robots(
[
'name' => 'test',
]
);
expect($robots->wasSetterUsed)->false();
Model::setup(
[
'disableAssignSetters' => false,
]
);
}
);
}
}

0 comments on commit da16160

Please sign in to comment.