Yii2 dependency injection for models


I’d like to ask how can I use DI for models extend ActiveRecord?

My intention is to create one object with dependency chain.

I have Class A, Class B, Class C and Class D. Class A contains Class B, Class B uses Class C and Class D.

class A extends \yii\db\ActiveRecord{

private $b;

    public function __construct(B b, $config){


      $this->b = b;



class B extends \yii\db\ActiveRecord{

private $c;

private $d;

    public function __construct(C c,D d, $config){


      $this->c = c;

      $this->d = d;


    public function f1(){





     public function actionDoA() {


    	$container = new Container();

        $aObject = $container->get('A');


        $sth = $aObject->b->f1();  


After action execute there is no error, only blank page.

How should it be implemented?

Thx for help.

you probably have the php set to not display errors. Looked in the file where the php log the errors or where yii2 log it. Be sure the php is set with display_errors on.

the activeRecord constructor accept only one parameters, $config

instead of rewrite the __construct method you could try to call:

$c = new C();

$d = new D();

$b = new B(['c'=>$c, 'd' => $d]);

$a = new A(['b'=>$b])

I had done it similiar as you wrote, but I inject dependencies in container:

	$container = new Container();

		$container->get('B', [], [

				'c' => $container->get('C'),

				'd' => $container->get('D'),