Solved thread

This post is marked as solved. If you think the information contained on this thread must be part of the official documentation, please contribute submitting a pull request to its repository.

Model protected properties - Bug?

As this does not throw an error;

class Robots extends \Phalcon\Mvc\Model {
    protected $name;

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

$robot = new Robots();
$robot->name = "Bumblebee";
$robot->save();

but the following does;

// Notice: Access to undefined property ...
echo $robot->name;

should I override the magic __set() function (in Robots) and throw an Exception, or is it a bug?



81.3k
Accepted
answer

You should override the _set method, this method is currently used by the ORM to set records on related relations. Since the _set method is defined on the same model it can write the property due to its protected visibility.

Thanks - from Sunny Australia :)