Skip to content

Proposal to disable guarding in constructor #10

Description

@dan-bowen

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?

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions