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?
samdark
(Alexander Makarov)
November 14, 2013, 8:35am
2
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='???' />
samdark:
Yes, exactly that.
No.
The question is, how should I
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)
use ‘virtual’ attribute?
$user->fullName or $user->full_name ?
samdark
(Alexander Makarov)
November 14, 2013, 11:38am
6
Same way as its defined in DB.
fullName since you defined it like that.
Yii doesn’t introduce any extra magic to convert column names.
samdark:
Same way as its defined in DB.
fullName since you defined it like that.
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…
samdark
(Alexander Makarov)
November 15, 2013, 8:11am
8
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.