modler icon indicating copy to clipboard operation
modler copied to clipboard

Proposal to disable guarding in constructor

Open dan-bowen opened this issue 6 years ago • 0 comments

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?

dan-bowen avatar Jul 08 '18 16:07 dan-bowen