Answer the question
In order to leave comments, you need to log in
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]);
}
}
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
Here is a good CMS written in Yii www.yupe.ru/. Install for yourself and see how they solve similar problems.
What is our cur_dir?
The strcat_s function has three parameters...
errno_t strcat_s(
char *strDestination,
size_t numberOfElements,
const char *strSource
);
template <size_t size>
errno_t strcat_s(
char (&strDestination)[size],
const char *strSource
); // C++ only
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question