Yii Framework Forum: Coding Style: Table Columns - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Coding Style: Table Columns

#1 User is offline   ORey 

  • Elite Member
  • PipPipPipPipPip
  • Yii
  • Group: Members
  • Posts: 1,703
  • Joined: 20-April 09
  • Location:Moscow, Russia

Posted 13 November 2013 - 03:42 PM

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?
God is real unless declared as integer
0

#2 User is offline   samdark 

  • Having fun
  • Yii
  • Group: Yii Dev Team
  • Posts: 4,719
  • Joined: 17-January 09
  • Location:Russia

Posted 14 November 2013 - 03:35 AM

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

#3 User is offline   ORey 

  • Elite Member
  • PipPipPipPipPip
  • Yii
  • Group: Members
  • Posts: 1,703
  • Joined: 20-April 09
  • Location:Moscow, Russia

Posted 14 November 2013 - 03:49 AM

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; // ???
echo $user->full_name; // ???
echo $user->fullName; // ???

God is real unless declared as integer
0

#4 User is offline   samdark 

  • Having fun
  • Yii
  • Group: Yii Dev Team
  • Posts: 4,719
  • Joined: 17-January 09
  • Location:Russia

Posted 14 November 2013 - 06:07 AM

Yes, exactly that.
0

#5 User is offline   ORey 

  • Elite Member
  • PipPipPipPipPip
  • Yii
  • Group: Members
  • Posts: 1,703
  • Joined: 20-April 09
  • Location:Moscow, Russia

Posted 14 November 2013 - 06:18 AM

View Postsamdark, on 14 November 2013 - 06:07 AM, said:

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)

2. use 'virtual' attribute?

$user->fullName or $user->full_name?
God is real unless declared as integer
0

#6 User is offline   samdark 

  • Having fun
  • Yii
  • Group: Yii Dev Team
  • Posts: 4,719
  • Joined: 17-January 09
  • Location:Russia

Posted 14 November 2013 - 06:38 AM

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.
0

#7 User is offline   ORey 

  • Elite Member
  • PipPipPipPipPip
  • Yii
  • Group: Members
  • Posts: 1,703
  • Joined: 20-April 09
  • Location:Moscow, Russia

Posted 14 November 2013 - 06:50 AM

View Postsamdark, on 14 November 2013 - 06:38 AM, said:

1. Same way as its defined in DB.
2. 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?

View Postsamdark, on 14 November 2013 - 06:38 AM, said:

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


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...
God is real unless declared as integer
0

#8 User is offline   samdark 

  • Having fun
  • Yii
  • Group: Yii Dev Team
  • Posts: 4,719
  • Joined: 17-January 09
  • Location:Russia

Posted 15 November 2013 - 03:11 AM

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.
0

#9 User is offline   ORey 

  • Elite Member
  • PipPipPipPipPip
  • Yii
  • Group: Members
  • Posts: 1,703
  • Joined: 20-April 09
  • Location:Moscow, Russia

Posted 15 November 2013 - 03:36 AM

ok.
God is real unless declared as integer
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users