How to name models vs database tables

Hi, I have 2 years of php/MySQL experience, now learning the Yii framework.

I always named my DB tables according to what they contained. Example 'users' contained all users, and 'invoices' all invoices. I intentionally used the plural, because each table contains one or more records. When dealing with one record in an object, I called it 'user' or 'invoice'

Now how should I go on doing this with Yii ?

If I call my table 'users' then the model will also be called 'users' and in the code you will find $users = new users; (whereas it should be $user = new User;)

What are good naming conventions to make both database AND php code very readible ???

Thanks.

A quick answer, you can name the model whatever you want, just need to add

	public function tableName()


	{


		return 'actual_table_name_here';


	}

to the model class.

You can keep your habit of table naming. As @will pointed out, by overriding tableName(), you can use table name that is different from the model class name. In the yiic shell, you can use command "model ClassName TableName" to create a model class that has different table name.

Excellent : simple and flexible  :)

Thanks !

I prefer to have a funtion in model that we can associate an alias for the table. Qiang approach is good, but actually:

  • We can't use as 'alias' with spaces or symbols. For example, I have a table seg_user_group, but in the forms i'd like to name it just 'User Group'.

  • Sometimes we don't have the freedom to name our tables as we wish (as me :/).

So, together with 'tableName', I'd like an 'aliasName' function in models. So, forms could use this 'alias' to build the interface.

Why would you use table name/alias name to build a form?

My mistake, Qiang. 'Build' was an modification of mine.

I have created my own template views (form, list, show, manage, etc) based on the original templates to obtain the 'alias' name from the Model. Right now, this information is constant in the view.

For example, actually If i crud a seg_user_group table, my manage view is generated as:



<h2>Managing seg_user_group</h2>


...


Then, I have to modify each view to remove the underscores and the 'seg' prefix.

In my aproach, the code above changes to



<h2>Managing <?php echo seg_user_group::model()->aliasName(); ?></h2>


And the same in each view created by crud.

So, after crud, I just modify my aliasName function to change all my views at once.

I think this is a bit too specific to be included in the core. If you really need this, you can define a base AR class and declare such a method. All your other AR classes should extend from this base class.

Quote

I think this is a bit too specific to be included in the core. If you really need this, you can define a base AR class and declare such a method. All your other AR classes should extend from this base class.

Yes, I'm already doing what you said. It was not a request, i was just sharing my idea.

This is not difficult to implement, just needed the inclusion of the function aliasName in CActiveRecord and modification in crud view templates to obtain the aliasName.

About the aliasName function, if it isn't defined, it will use the value from tableName function, as it is today.

I think it is not so especific. Some reasons:

  • It is faster to rename model, without requiring changing the table name or the model name. Suppose your costumer don't like your naming. You will just rename your views, not models or table names. That's my vision.

  • To renaming complex table names, like 'invoicePartComposition' (or something like that). This is a very common case.

  • Good for internationalization.