Answer the question
In order to leave comments, you need to log in
How to cache yii\db\ActiveQuery query?
$query = Material::find()
->where(['in', 'id_material', $ids])
->andWhere('start_publish < :time')
->params([':time' => time()])
->orderBy(['start_publish' => SORT_DESC]);
$count = $query->count();
$query = Material::find()
->where(['in', 'id_material', $ids])
->andWhere('start_publish < :time')
->params([':time' => time()])
->orderBy(['start_publish' => SORT_DESC]);
$count = Material::getDb()->cache(function() use ($query) {
return $query->count();
});
$query = Material::find()
->where(['in', 'id_material', $ids])
->orderBy(['start_publish' => SORT_DESC]);
$count = Material::getDb()->cache(function($db) use ($query) {
return $query->count();
});
Answer the question
In order to leave comments, you need to log in
Request or received data, so as not to make a request all the time?
Everything is here:
www.yiiframework.com/doc-2.0/guide-caching-data.html
Alternatively:
$data = $cache->get($key);
if ($data === false) {
$query = Material::find()
->where(['in', 'id_material', $ids])
->andWhere('start_publish < :time')
->params([':time' => time()])
->orderBy(['start_publish' => SORT_DESC])
->all();
$cache->set($key, $data);
}
print_r($data);
$data= Material::getDb()->cache(function ($db) {
return Material::find()->where(['in', 'id_material', $ids])
->andWhere('start_publish < :time')
->params([':time' => time()])
->orderBy(['start_publish' => SORT_DESC])->all();
});
$result = Customer::getDb()->cache(function ($db) {
return Customer::find()->where(['id' => 1])->one();
});
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question