I need to transfer the "number" to the model, and depending on this "number" to change the name of the table and a connection to the database, but getDb is static function and tableName too.
I found only one solution on the git: "/axiles89/yii2-sharding", but it seems to me too difficult. Maybe you know more a cleaner solution?
Even if the method is static you can make the result dinamic, just override it in your model
/**
* @return \yii\db\Connection the database connection used by this AR class.
*/
public static function getDb() {
return <get the value from where you want>
}
for example:
return variable from session
return variable from a function
Same is for tableName()
Static means only that you can access a class property/method without create an instance of the object.
private static $dbname='db';
public function setDb($db){
self::$dbname = $db;
return $this;
}
public static function getDb()
{
return Yii::$app->get(self::$dbname);
}