Answer the question
In order to leave comments, you need to log in
How to inject a component through Yii::$container into a class constructor?
There is a class which works with a DB. The database connection instance is passed through the constructor.
class UpdateStatsCommand extends Object {
/**
* @var Connection
*/
protected $db;
/**
* @inheritdoc
*/
public function __construct(Connection $db, array $config = []) {
$this->db = $db;
parent::__construct($config);
}
...
Yii::$container->set(yii\db\Connection::class, function () {
return Yii::$app->get('db');
});
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question