Chinmay Waghmare
Chinmay Waghmare

Reputation: 5456

Yii2 How can I use one active record class and reuse it for multiple tables having same schema

If suppose I have 3 tables:

School ( id, name, location)

School_1 ( id, name, location)

School_2 (id, name, location)

I have a model extending ActiveRecord for School. Can I use the same active record class for School_1 and School_2. Because the table schema is same I don't want to use multiple model classes.

Is it possible? Any help would be appreciated. Thanks in advance.

Upvotes: 1

Views: 510

Answers (1)

soju
soju

Reputation: 25322

You should simply override tableName(), e.g. for School_1 :

class School1 extends School
{
    public static function tableName()
    {
        return 'School_1';
    }
}

And if needed (I think so) you can create these classes on the fly using eval().

Upvotes: 1

Related Questions