Answer the question
In order to leave comments, you need to log in
How to optimize controller in Yii2?
Good day! In the project, some controller actions contain a lot of selections from the database, for example
/** @var Question|null $question */
$question = Question::findOne([
'url' => $slug,
'deleted' => 0,
'moderated' => 0
]);
$id = $question->id;
/** @var Question $question Вопрос */
$question = Question::find()
->with('screenshot')
->select([
Question::tableName() . '.*',
'SUM(views) as totalViews'
])
->joinWith('postView')
->with(['category', 'subcat'])
->where([Question::tableName() . '.slug' => $slug])
->one();
/** @var array $lastQuestions */
$lastQuestions = Question::find()
->where([
'moderated' => 0,
'deleted' => 0
])
->andWhere('id != ' . $question->id)
->orderBy('published_date DESC')
->limit(4)
->all();
/** @var array $similarQuestions */
$similarQuestions = Question::find()
->where([
'moderated' => 0,
'deleted' => 0
])
->andWhere(['not in', 'id', ArrayHelper::getColumn($lastQuestions, 'id')])
->andWhere('id != ' . $question->id)
->limit(6)
->all();
$countComment = QuestionComment::find()
->where([
'id' => $id,
'deleted' => 0,
])
->count();
Answer the question
In order to leave comments, you need to log in
About the material: https://elisdn.ru/blog/search?q=yii2
or use the DTO pattern - and put all the queries you need in the action into it and it will turn out something like this:
$dto->similarQuestions()
or you can go more next and create an Active Query as done here https://github.com/ElisDN/yii2-demo-catalog/blob/m...
and then you can do this:
$dto->questions()->similar();
$dto->questions()->last();
$dto->questions()->count(); //or use a link here and call count as a property.
Well, in the future, if you need to, just optimize the questions queries either in the DTO file or its components directly in the QuestionsSubQuery file (or just call it QuestionsQuery)
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question