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

Camelize column names in models.

There's any configuration to camelize column names in models? Example: make the column bar_baz in table foo be accessed like this:

$myFoo = new Foo();
echo $myFoo->barBaz;


125.8k
Accepted
answer

No configuration, but you could overwrite __get() in a base model class, then have all your models extend that class

That wouldn't break the default implementation of __get()? I believe there's some crazy magic methods in there like findFirstByFoo(). I'm asking about a configuration because I believe that I've seen something similar somewhere (In the same place where you set that save() should throw an exception instead of returning false) but now I can't remeber where I read about that, surely not in the docs.

@cvsguimaraes, is columnMap what you need?


    public function columnMap()
    {
        //Keys are the real names in the table and
        //the values their names in the application
        return array(
            'id' => 'code',
            'the_name' => 'theName',
            'the_type' => 'theType',
            'the_year' => 'theYear'
        );
    }

Found ya!! https://docs.phalcon.io/en/latest/reference/models.html#disabling-enabling-features

But the options descriptions don't tell me much, I'll test these settings as soon I get to work again...

Still no clue of what 'columnRenaming' actully does... It just disables columnMap()?

I'll try to implement the the magic getter solution...