You may face a situation when you need to access to a configurable module's property from everywhere and you don't have an instance of this module's class.
There's a extremely simple trick that was used in the AutoAdmin extension.
class AADb
{
public static $dbConnection = 'db'; //The necessary static property
...
}
Config:
$main['modules'] = array(
'autoadmin'=>array(
'dbConnection' => 'db',
//. . .
)
);
class AutoAdmin
{
public $dbConnection = 'db'; //using intermediate property (with default)
//. . .
public function init()
{
//. . .
$this->_db = new AADb($this->_data);
AADb::$dbConnection =& $this->dbConnection; //you can also use some self::$db variable
}
}
Then from everywhere:
class AAFieldForeign extends AAField implements AAIField
{
//. . .
private function selectDefault()
{
$q = Yii::app()->{AADb::$dbConnection}->createCommand();
//. . .
}
}
So as you can see you get an access to the property $dbConnection which can be configured by user. And it's accessible as static through the special class (of course not only for this). And you can change the value of the property if you need (but look after succession of operations).
If you have any questions, please ask in the forum instead.
Signup or Login in order to comment.