I have been testing the guarded property and was surprised that when you send data in the constructor none of the guarded properties are set. For instance, if I have this model
class TestModel extends \Modler\Model
{
protected $properties = array(
'id' => array(
'type' => 'int',
'guarded' => true,
)
);
}
The guarded properties are not set in the constructor.
$model = new TestModel(['id' => 12345]);
var_dump($model->id); // null
My interpretation of the guarded property is that it protects properties from being changed after they have already been set. Since by definition the constructor is setting up the initial state of the model, I would have expected the guarded properties to be set in that case.
I tracked this down to calling $this->load($data); in the constructor. https://github.com/enygma/modler/blob/master/src/Modler/Model.php#L35
The proposal would be to turn off guarding in the constructor by calling $this->load($data, false); so that guarded properties can be set there. What do you think?
I have been testing the
guardedproperty and was surprised that when you send data in the constructor none of the guarded properties are set. For instance, if I have this modelThe guarded properties are not set in the constructor.
My interpretation of the
guardedproperty is that it protects properties from being changed after they have already been set. Since by definition the constructor is setting up the initial state of the model, I would have expected the guarded properties to be set in that case.I tracked this down to calling
$this->load($data);in the constructor. https://github.com/enygma/modler/blob/master/src/Modler/Model.php#L35The proposal would be to turn off guarding in the constructor by calling
$this->load($data, false);so that guarded properties can be set there. What do you think?