php – Yii2:如何缓存ActiveRecord关系产生的查询
发布时间:2020-12-13 13:25:16 所属栏目:PHP教程 来源:网络整理
导读:我有新闻表及其相关的news_comment表. 我已经用news_comment表定义了关系newsComment. 如果我执行此查询: $result = News::getDb()-cache(function () use($id) { return News::find()-with('newsComment')-where(['news.id' = $id])-one();}); 只缓存从新
我有新闻表及其相关的news_comment表.
我已经用news_comment表定义了关系newsComment. 如果我执行此查询: $result = News::getDb()->cache(function () use($id) { return News::find()->with('newsComment')->where(['news.id' => $id])->one(); }); 只缓存从新闻表中获取数据的查询.从相关表中选择的查询不是. 是否可以缓存执行的主查询和查询以从相关表中检索数据,而无需单独编写它们?
试试这个:
$db = News::getDb(); $result = $db->cache(function ($db) use ($id) { $query = new yiidbQuery; $query->select("news.*,newsComment.*") // write table name for newsComment model and also in join ->from('news') ->leftjoin('newsComment','newsComment.id=news.product_id') ->where(['news.id' => $id]) ->one(); $command = $query->createCommand(); $result = $command->queryAll(); return $result; }); (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |