modelsManager->createBuilder(); $builder->from(ConsultModel::class); $builder->where('1 = 1'); if (!empty($where['id'])) { $builder->andWhere('id = :id:', ['id' => $where['id']]); } if (!empty($where['course_id'])) { $builder->andWhere('course_id = :course_id:', ['course_id' => $where['course_id']]); } if (!empty($where['user_id'])) { $builder->andWhere('user_id = :user_id:', ['user_id' => $where['user_id']]); } if (isset($where['published'])) { $builder->andWhere('published = :published:', ['published' => $where['published']]); } if (isset($where['deleted'])) { $builder->andWhere('deleted = :deleted:', ['deleted' => $where['deleted']]); } switch ($sort) { default: $orderBy = 'id DESC'; break; } $builder->orderBy($orderBy); $pager = new PagerQueryBuilder([ 'builder' => $builder, 'page' => $page, 'limit' => $limit, ]); return $pager->paginate(); } /** * @param int $id * @return ConsultModel|Model|bool */ public function findById($id) { return ConsultModel::findFirst($id); } /** * @param array $ids * @param array|string $columns * @return ResultsetInterface|Resultset|ConsultModel[] */ public function findByIds($ids, $columns = '*') { return ConsultModel::query() ->columns($columns) ->inWhere('id', $ids) ->execute(); } public function countAgrees($consultId) { $type = ConsultVoteModel::TYPE_AGREE; $count = ConsultVoteModel::count([ 'conditions' => 'consult_id = :consult_id: AND type = :type: AND deleted = 0', 'bind' => ['consult_id' => $consultId, 'type' => $type], ]); return $count; } public function countOpposes($consultId) { $type = ConsultVoteModel::TYPE_OPPOSE; $count = ConsultVoteModel::count([ 'conditions' => 'consult_id = :consult_id: AND type = :type: AND deleted = 0', 'bind' => ['consult_id' => $consultId, 'type' => $type], ]); return $count; } }