Answer the question
In order to leave comments, you need to log in
How to properly compose a DbDependency in Yii2?
There is the following code, a selection from the database of the most visited articles:
if (!$data = Yii::$app->cache->get('recent_list_post')) {
$data = Post::find()
->with(['imageR', 'categoryR'])
->active()
->orderBy('hits DESC')
->limit(4)
->all();
$dependency = new DbDependency([
'sql' => 'SELECT SUM(hits) FROM ' . Post::tableName() . 'WHERE status=' . Post::STATUS_ACTIVE . ' ORDER BY hits DESC limit 4',
]);
Yii::$app->cache->set('recent_list_post', $data, 60*60, $dependency);
}
$this->data = $data;
Answer the question
In order to leave comments, you need to log in
Here's the solution if anyone needs it:
SELECT SUM(s.id)*SUM(s.hits) FROM (SELECT p.hits,p.id FROM `htc_post`as p WHERE p.status=2 ORDER BY p.hits DESC LIMIT 4) as s
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question