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.
i do't fully understand what your mean. is that?