$query = Folder::find()->joinWith(['documents'])->orderBy(['COUNT(documents.id)' => SORT_ASC])->groupBy('folder.id');