D
D
Dmitry Baibukhtin2014-09-25 18:14:11
Yii
Dmitry Baibukhtin, 2014-09-25 18:14:11

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);
  }

This method has the ability to receive both by predefined criteria and by certain fields of the table.
Questions:
1. Is this the right approach or is it better to remove getting by certain fields of the table from outside, because the flexibility of the script is lost, the name of some field suddenly changes, then you have to change its name in the entire system, in each file.
2. If, for example, I need to get subtasks by a certain parent, then in the method parameters is it better to get only the identifier of the parent task or the whole object with all its data? The second option seems to be more flexible.
I want to know if my train of thought is going right or is it just another bydlokod

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Andrey Ezhgurov, 2014-09-25
@eandr_67

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 question

Ask a Question

731 491 924 answers to any question