one model many tables with different names


i'm new to yii and i'm trying to figure out one problem. i have many tables with different names, but all of them have the same columns, so i want to use only one model on all of them.

found on the site that i need to override tableName() method, so here's what i did in my model class:

private $_table = 'B03';

public function tableName() {

    return $this->_table;


public function setNewTableName($table) {

    $this->_table = $table;


in my actionList function i added:


but it always loads the data from B03 table… if i change tableName manualy than it works fine. i even tried to use model()->refresh() but it returns false and nothing is refeshed.

can anyone lead me on the right track? :)

thanks for the help


tableName method returns the name of the associated database table. You should generate one model for one table in database.

You might want to create a new column called category (or sth similar), and then combine all tables into one giant table with this additional category column.

Then you can call on different category to switch to data in previous different tables.

Just a thought. There might be many other ways to perform this similar task.

if i could change database i would have done it. creating models for every table seems too inconvenient for me. i need to refresh the model somehow after i change tableName or find the method where yii uses tableName to load data from the table in the first place…

You may want one base model for all these tables containing the shared code and then create new models for each table that extend the base model

Johah, very good idea.