How to define db prefix in Yii?

Same as the title, how to define db prefix in Yii?

Do you mean common table prefix? You may define it as an application parameter (configure it in 'params' property in app config). And then in your AR's tableName() method, use Yii::app()->params['tablePrefix'] to refer to this prefix.

this means I should write a base class to inherit the CActiveRecord and rewrite the tableName method?

You ARE subclassing CActiveRecord and rewriting tableName() even if you don't need this prefix feature. :wink: Yes, creating a new base class can make your life easier:



<?php


class MyActiveRecord extends CActiveRecord


{


	public function tableName()


	{


		return $this->tableNamePrefix().$this->rawTableName();


	}


	


	protected function tableNamePrefix()


	{


		return Yii::app()->params['tablePrefix'];


	}


	


	abstract protected function rawTableName();


}