With the help of activeRecord I want to achieve the following.

There are nomenclature, they have events.

For example: Nomenclature Volleyball, and in her 3 events Nomenclature Football and in her 6 events.

I need to load the nomenclature and eagerly load the events to them. I do the following:

Nom::find()->with('event')->all(); 

But in the list there are nomenclature, within which there is not a single active event, such are not needed. I exclude them.

 Nom::find()->joinWith('event') ->where(['event.is_active'=>1]) ->all(); 

Now items that have no active events are not selected. But there are nomenclature, which contain inactive events. Such activities do not need to display.

You need to somehow include the Where clause (is_active => 1) also for the Event table. How to do it?

    1 answer 1

    You can do this directly in the communication models

     public function getEvent() { return $this->hasOne(Event::class, ['id_чего-то' => 'id')->where([/* тут условие */]); } 

    You can assign an alias to the table and add a condition as you do.

     ->with('event') ->joinWith( 'event' => function(ActieQuery $query) { $query->from(['event' => Event::tableName()]); // назначается псевдоним таблицы } ) ->where(['event.active' => 1])