我有新闻表及其相关的news_comment表.
我已经用news_comment表定义了关系newsComment.
我已经用news_comment表定义了关系newsComment.
如果我执行此查询:
$result = News::getDb()->cache(function () use($id) { return News::find()->with('newsComment')->where(['news.id' => $id])->one(); });
试试这个:
原文链接:https://www.f2er.com/php/137317.html$db = News::getDb(); $result = $db->cache(function ($db) use ($id) { $query = new \yii\db\Query; $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; });