There was such a need:

  1. There is a Group model, with a HAS_MANY link to the ScheduleElement model
  2. It is necessary to make a scope that would limit the selection of groups, give only those that have ScheduleElement'y (at least one) for each combination week_day (1-7) and week_number (1-2) with the same semester_id.

I can not understand if it is possible to do it :-(

    1 answer 1

    I solved the problem myself, who cares, here's the code: https://github.com/sc0rp1d/schedule/blob/e510ad72b95e9491af9ff2f5692d9b0e469140f7/models/Group.php#L95

    public function filled($bool = true) { $schedule_table = ScheduleElement::model()->tableName(); $semester = Semesters::model()->actual(); $criteria = $this->getDbCriteria(); $criteria->params[':semester_id'] = $semester->id; for ($i = 1; $i <= 2; $i++) { for ($j = 1; $j <= 6; $j++) { $sql = "(SELECT COUNT(*) FROM `$schedule_table` WHERE `$schedule_table`.`week_number` = $i AND `$schedule_table`.`week_day` = $j AND `$schedule_table`.`group_id` = `t`.`id` AND `$schedule_table`.`semester_id` = :semester_id) " . ($bool ? ">" : "=") . " 0"; $criteria->addCondition($sql, $bool ? 'AND' : 'OR'); } } return $this; }