Column names different than field names

Hello,

according to Coding Standards that I created for myself, I create db tables and column names using lowercase and underscore to separate words, so columns are named like:

user_id, post_id etc.

However, when coding PHP I prefer to use camelCase:

$this -> userId, $this -> postId

Is it possible to set somewhere in Yii to create fields in classes using camelCase when in db are underscores? It’s probably hard to implement, but I believe it’s possible, so maybe Yii allows such thing?

Sorry for my english and thanks in advance.

Mateusz Kamola

helo you may check this post.

if you find the answer, you can post here to make everyone know about that :P

I haven’t tried it, but I suppose you just have to overload the magic getters and setters.




class UnderscoreActiveRecord extends CActiveRecord

{

    public function __get($name)

    {

        if (strpos($name, '_') !== false) {

            $name = preg_replace('/_([a-z])/e', 'ucfirst($1)', $name);

        }

        return parent::__get($name);

    }

    public function __set($name, $value)

    {

        if (strpos($name, '_') !== false) {

            $name = preg_replace('/_([a-z])/e', 'ucfirst($1)', $name);

        }

        return parent::__set($name, $value);

    }

}



Though you could use a behavior, the easiest solution is probably to make all your models inherit from this custom class.

If what the posts xent referred to is still right, the you don’t need to overload “setAttributes()”.