D
D
Dmitry Baibukhtin2014-09-26 22:57:57
Yii
Dmitry Baibukhtin, 2014-09-26 22:57:57

How to get data from database in MVC style?

Hello. I am learning to write high-quality flexible code for reusable use.
Task: Write a high-quality, flexible Feng Shui method for getting users from a database. Framework Yii.
How is it better?
Option 1: use the ActiveRecord model directly in the controller
This method seems to me the most flexible, it is many-time and flexible. What if I only need active users and need to use this in several places. Let's assume that everywhere I have written the necessary criteria, but what if the criteria for searching for active users change?
Is it possible to formalize the receipt of active users in the form of an osprey or apply the second variant of the method?
For example:

class UsersController extends CController
{
  public function actionUsersList()
  {
    $usersCriteria = new CDbCriteria;
    $usersCriteria->addColumnCondition(['nick' => 'PiloT']);
    $usersCriteria->addColumnCondition(['city' => 'Киев']);

    $users = UsersAR::model()->findAll($usersCriteria);

    $this->render('users', ['users' => $users]);
  }
}

Option 2: prohibits using the ActiveRecord object directly, creating a separate model.
Is it more correct? For some reason, it seems to me that these are just unnecessary problems, because if the same ActiveRecord is still called from the controller, only through the Users intermediary, and instead of scopes, a condition is used in the Users::getUsers() method. Perhaps the advantage is complete control over the received parameters, but if ActiveRecord is used directly, then you can either override the method to control the parameters or use the beforeFind () event, which is more convenient and does not create problems.
class Users extends CComponent
{
  public function getUsers($params = [])
  {
    $usersCriteria = new CDbCriteria;

    if (!empty($params['city'])) {
      $usersCriteria->addColumnCondition(['city' => $params['city']]);
    }

    if (!empty($params['only_active'])) {
      $usersCriteria->addCondition('"time_activity" = ' . (time() - 600));
      $usersCriteria->addColumnCondition(['ban' => '0']);
      $usersCriteria->addColumnCondition(['admin' => '0']);
    }

    return UsersAr::model()->findAll($usersCriteria);
  }
}

class UsersController extends CController
{
  public function actionUsersList()
  {
    $usersModel = new Users;
    $users = $usersModel->getUsers(['city' => 'Киев', 'only_active']);

    $this->render('users', ['users' => $users]);
  }
}

Answer the question

In order to leave comments, you need to log in

3 answer(s)
A
Alex, 2014-09-26
@PiloTeZ

Here is a good CMS written in Yii www.yupe.ru/. Install for yourself and see how they solve similar problems.

M
Mercury13, 2016-10-05
@Mercury13

What is our cur_dir?
The strcat_s function has three parameters...

errno_t strcat_s(
   char *strDestination,
   size_t numberOfElements,
   const char *strSource 
);

C++ also has a template overload with two parameters.
template <size_t size>
errno_t strcat_s(
   char (&strDestination)[size],
   const char *strSource 
); // C++ only

The conclusion is this. The two-parameter overload takes only an array as its first parameter, not a char*.
PS And it will be better, of course, to use containers like std::string.

A
AtomKrieg, 2016-10-05
@AtomKrieg

#include <string>

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question