We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

Model getters and setters

Hey,

quick question about getters and setters. Coming from Slim, trying out Phalcon, i'm a bit confused about the getter and setters. In Slim, creating a setter would cause it to be invoked when accessing the property normally. Does not Phalcon support this?

Also, it seems that when calling $object->toArray() it doesn't take in consideration whether a property i've set in the model is protected or public.

Am i missing something here?



98.9k

No, Phalcon has a different behavior, it updates the property directly with the value recovered from the database. If you set the value using Model::assign, Model::save, Model::create or Model::update it will check for a setter. If you're using Phalcon\Forms it will also check if there is a setter first.