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 getter for underscore named fields

Hi all,

I have a field in my db table by name foo_bar and in my model, I try to set/get it as follow:

public function setFooBar($value)
{
    $this->foo_bar = $value
}

public function getFooBar()
{
    return 'foo_bar value gets';
}

but when I try to get foo_bar value in controller $model->foo_bar the getter method does not calls anymore! In source I see there is logic to do that.

What is wrong here?

Thanks.

edited Jun '15

Before v.2.0.3 if I comment public field definitions then setters/getters work.

In v.2.0.3 it seems I shoud defined them protected to works as expected!!

U guys in phalcon maintain somthings in code to prevent breaking existing applications but this change breaks alot of things! :/

If I want to do some logic in setting some model's property, then I should set this property protected then implement setFoo and getFoo both!!

But in last approach (last versions) this is not required to implement getter at all!

This is a weird I think.

In all setters you should have useless getter such as:

getFoo()
{
    return $this->foo;
}
edited Jun '15

Would be great to have a definitive version of this behaviours on models. As I see it getters setters:

  • must override the default get set functionality but only if they are defined.

  • if they are defined they override it always. Not depending on the variable visibility.

thats the default behaviour for getters and setters on a simple entity, so you don't need to define it and when you are calling for a property or setting it the system uses the override method if exists.