Answer the question
In order to leave comments, you need to log in
How to make a convenient data fetch method (Yii)?
Hello. Please tell me how to make a method for selecting data in the MVC style, so that it would be easy to use and easy to extend.
My version:
/**
* Менеджер подзадач
*/
class SubtasksManager
{
/**
* Получение подзадач
*/
public function getSubtasks($params = [])
{
$subtasksCriteria = new CDbCriteria;
/*
* Фильтрация по предопределённым критериям
*/
// Получение задач ожидающих выполнения
if (!empty($params['requires_performing'])) {
// Только выполняемые задачи (есть ещё связующие)
$params['type'] = '1';
// Задачи ожидающие выполнения
$params['performing_status'] = '1';
}
// Получение только задач в стадии подготовки
if (!empty($params['preparing'])) {
$params['performing_status'] = '0';
}
/*
* Фильтрация по определённым полям
*/
// Получение по идентификатору родительской задачи
if (!empty($params['task_id'])) {
$subtasksCriteria->addColumnCondition(['task_id' => $params['task_id']]);
}
// Получение по типу
if (!empty($params['type'])) {
$subtasksCriteria->addColumnCondition(['type' => $params['type']]);
}
// Получение по статусу выполнения
if (!empty($params['performing_status'])) {
$subtasksCriteria->addColumnCondition(['performing_status' => $params['performing_status']]);
}
return SubtasksAr::model()->findAll($subtasksCriteria);
}
Answer the question
In order to leave comments, you need to log in
In MVC, data sampling is done in models . Accordingly, everything that you wrote in the "subtask manager" should be placed in the appropriate model. If you need an additional data retrieval mechanism, you create a new method in the model. It is models that should display the structure of data and relationships.
Yii has a fairly powerful Active Record that allows you to flexibly select data from child tables without additional tricks. With the correct setting of links in the models of a subtask of a given parent, it is quite possible to get it like this: Task::model()->findByPk($id)->Subtask .
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question