There is no way to add a condition in on for dependency. There is such a model:

class Parent extends ActiveRecord { public function getChild() { return $this->hasMany(Child::class, ['parent_id' => 'parent_id'])->alias('c'); } } 

I get the data like this:

 $query = Parent::find() ->alias('p') ->distinct() ->joinWith([ 'child c' ], true, 'inner join'); 

As a result, the request is approximately as

 select p.* from parent p inner join child c on c.parent_id = p.parent_id 

The question is that I can’t figure out how to get such a request:

 select p.* from parent p inner join child c on c.parent_id = p.parent_id and c.is_disabled = false 
  • in where, of course, I can add, but the result of the query will not be what is needed. - ilyaplot

1 answer 1

ActiveQuery documentation solved the problem \ http://www.yiiframework.com/doc-2.0/yii-db-activequery.html#onCondition()-detail

 class Parent extends ActiveRecord { public function getChild() { return $this->hasMany(Child::class, ['parent_id' => 'parent_id']) ->alias('c') ->onCondition('c.is_disabled = false'); } };