Yii model overriding based on constant

494 views Asked by At

I need to change model class in my yii2 project. Because of a change in database so all the query of model class need to be converted according to new database table. But I want to the old db configuration also because if something went wrong in my new db then i can easily move to all model class query (failover). So i want a old model class and override it with new model class function based on constant. In constant if i'm set old (0) then pick the old db class query or new (1) then override the model with new query. How this is possible using OOPS concept please help .

class ModelClass extends ActiveRecord
{
    public static function tableName()
    {
        return 'table1';
    }
    public function getData($limit = '5', $offset = '0')
    {
     //
    }
}

New Model class.

class NewModelClass extends ModelClass
{
    public static function tableName()
    {
        return 'table2';
    }
    public static function getDb()
    {
        return \Yii::$app->get('newDb'); // second database
    }
    public function getData($limit = '5', $offset = '0')
    {
     //
    }
}

In Controller class i'm using the parent model class in namespace like :

use api\modules\v1\models\ModelClass;

For calling function of model in action :

$objModel = new ModelClass(); // call parent class 
$objModel->getData();

Define constant in params:

'model' => '1', // 1 means new or 0 means old

Then how to switch between model without putting if else conditions in everywhere of controller . Need a basic check 0/1 to switch between the classes of model.

2

There are 2 answers

1
harry On

i do't fully understand what your mean. is that?

$config = array('model' => 1);

$model = $config['model'] == 1
    ? new api\modules\v1\models\ModelClass()
    : new api\modules\v1\models\NewModelClass();
0
Ripper On

If anyone know better solution feel free to correct me !

I think you can do it directly in methods of new Model,

like this:

class NewModelClass extends ModelClass
{
    const $useNew = true;

    public static function tableName()
    {
        if ($useNew) return parent::tableName();
        return 'table2';
    }
    public static function getDb()
    {
        if ($useNew) return parent::getDb();
        return \Yii::$app->get('newDb'); // second database
    }
    public function getData($limit = '5', $offset = '0')
    {
        // Although there is problem with non-static methods
        // as you need instance of parent class, in php
        // you can get away with warning if you use following,
        // but just in case, you dont use an instance of parent
        // class or $this word in parent class method
        if ($useNew) return parent::getData(limit , $offset);
     //
    }
}

Discussion about using call to parent within non-static method

EDIT:

just came in my mind you can use magic __construct method maybe for non-static methods like this, not sure if it will work

class NewModelClass extends ModelClass
{
    const $useNew = true;

    public function __construct() {
        if ($useNew) parent::__construct();
    }
}

if this doesnt work then maybe

class NewModelClass extends ModelClass
{
    const $useNew = true;

    public function NewModelClass() {
        if ($useNew) {
            $this = new parent::__construct();
            // or $this = new ModelClass();
        }
    }
}

then in non-static methods you should be able to just reffer to $this etc...

If anyone know better solution feel free to correct me !