Coding Style: Table Columns

Hi guys.

According to current style guide, property names MUST be declared in camelCase.

What about table column names?

For example, here undescored fields are used ([‘customer_id’ => ‘id’])

Using camelCased identifiers is sometimes painful in, say, postgresql, so underscored fields are much better.

But this will result in underscored properties, $model->customer_id.

Also, it’s not quite clear how to name custom getters for table columns.

Can you shed some light on it?

We’re using undescore and lowercase for table fields for the reason you’ve mentioned. Custom getters or setters are camelCased.

Can I haz an example plz?


class User extends ActiveRecord

{

    public function getFullName()

    {

        return $this->first_name . ' ' . $this->last_name;

    }

}




$user = User::find($pk);

echo $user->first_name; // <img src='http://www.yiiframework.com/forum/public/style_emoticons/default/huh.gif' class='bbc_emoticon' alt='???' />

echo $user->full_name; // <img src='http://www.yiiframework.com/forum/public/style_emoticons/default/huh.gif' class='bbc_emoticon' alt='???' />

echo $user->fullName; // <img src='http://www.yiiframework.com/forum/public/style_emoticons/default/huh.gif' class='bbc_emoticon' alt='???' />

Yes, exactly that.

No.

The question is, how should I

  1. use ‘column’ attribute?

$user->first_name or $user->firstName?

Btw, what about conditions?

$users = User::find()->where([‘is_disabled’ => 0])->all()

or

$users = User::find()->where([‘isDisabled’ => 0])->all()? (this will cause sql error I suppose)

  1. use ‘virtual’ attribute?

$user->fullName or $user->full_name?

  1. Same way as its defined in DB.

  2. fullName since you defined it like that.

Yii doesn’t introduce any extra magic to convert column names.

So the code will look like this


<div><?= $user->fullName ?> has <?= $user->post_count ?> posts ?></div>

Isn’t that bad?

Yes, but it turns out getters can be ‘magic’:


pubic function getFull_name() {...}


echo $user->full_name;

Looks bad also, but at least this is hidden inside the model…

It’s not that bad for me. I see what’s column and what’s not immediately. Also I understand well issues if we’ll try to convert these: 1) column mapper 2) SQL abstraction layer. Introducing these will slow things down for certain and make DB layer code much more complex solving not that important issue.

ok.