code: protected static function buildBuckets($models, $linkKey, $multiple)
      {
          $buckets = [];
          
          foreach ($models as $model) {
              $key = $model[$linkKey];
              if (is_scalar($key)) {
                  $buckets[$key][] = $model;
              } elseif (is_array($key)){
                  foreach ($key as $k) {
                      $k = static::normalizeModelKey($k);
                      $buckets[$k][] = $model;
                  }
              } else {
                  $key = static::normalizeModelKey($key);
                  $buckets[$key][] = $model;
              }
          }
          
          if (!$multiple) {
              foreach ($buckets as $i => $bucket) {
                  $buckets[$i] = reset($bucket);
              }
          }
          
          return $buckets;
      }
docs: Сгруппировать найденные модели
  @param array $models
  @param string $linkKey
  @param bool $multiple
  @return array