Py学习  »  MongoDB

如何在Yii2中与mongodb一起使用DbQueryDependency

Rock Yuan • 4 年前 • 847 次点击  

yii\mongodb\Query ,但我明白了:

[error][yii\base\InvalidConfigException] yii\base\InvalidConfigException: "yii\caching\DbQueryDependency::$query" should be an instance of "yii\db\QueryInterface". in /vendor/yiisoft/yii2/caching/DbQueryDependency.php:78
  • Mongodb 4.0.8版
  • 菲律宾比索7.2.6
  • 意2.0.18
  • yiisoft/yii2 mongodb 2.1.7版
  • yiisoft/yii2 redis 2.0.9版
class Test extends yii\mongodb\ActiveRecord {
    // ...
    public static function findByTest($test){
        $dep = new DbQueryDependency([
            'query'=> (new Query())
                ->from('test')
                ->where(['test' => $test])
                ->orderBy(['updated_at' => -1 ])
                ->one(),
        ]);

        $cache = Yii::$app->cache;
        $key = 'test.'.$test;

        return $cache->getOrSet($key, function () use ($test) {
            return static::findOne(['test' => $test, 'status' => static::STATUS_ACTIVE]);

        }, 3600, $dep);
    }
    // ...
}

我希望yii在1小时内缓存mongodb文档,除非 updated_at yii\base\InvalidConfigExc eption: "yii\caching\DbQueryDependency::$query" should be an instance of "yii\db\QueryInterface".

我该怎么解决?谢谢您!

Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/55356
 
847 次点击  
文章 [ 1 ]  |  最新文章 4 年前
rob006
Reply   •   1 楼
rob006    4 年前

你需要移除 one() 从你的代码中调用。 执行查询并返回其结果,而您需要查询本身。

您也可以尝试通过仅选择最大值(或至少设置限制)来优化查询- 一() 不设置限制,因此您的查询返回表ad PHP只使用第一条记录的所有数据)。

'query' => (new Query())
    ->select('MAX(updated_at)')
    ->from('test')
    ->where(['test' => $test]),

'query' => (new Query())
    ->from('test')
    ->where(['test' => $test])
    ->orderBy(['updated_at' => SORT_DESC])
    ->limit(1),