私はyii2を初めて使用しています。私はドキュメントといくつかの答えをsofで読んだが、まだ私はyii2の関係で作業することはできません。私は問題の生のmysqlクエリを作成することができますが、私はyii2関係を使用して同じクエリを作成する方法を知りません。私はvia、joinWith、いくつかの重要な概念と混同しています。私はできるだけわかりやすいように問題を作ります。 私は4つのモデルを持っています。私は必要なもの複数のテーブルのYii2結合関係
Category, CategoryNews, NewsTags, Tags
category table - cat_id, cat_name
news_category table - nc_id, nc_cat_id, nc_news_id
news_tags table - nt_id, nt_news_id, nt_tag_id
tags table - tag_id, tag_name
はそれは私がそのカテゴリに属するすべてのニュースタグを必要とするカテゴリごとにあり、各カテゴリのタグモデルオブジェクトです。リクエストはgridviewからです。
Category Model:
public function getNewsCategory()
{
return $this->hasMany(NewsCategory::className(), ['nc_cat_id' => 'cat_id']);
}
NewsCategory Model:
public function getNcNews()
{
return $this->hasOne(News::className(), ['news_id' => 'nc_news_id']);
}
public function getNcCat()
{
return $this->hasOne(Category::className(), ['cat_id' => 'nc_cat_id']);
}
NewsTags Model:
public function getNtNews()
{
return $this->hasOne(News::className(), ['news_id' => 'nt_news_id']);
}
public function getNtTag()
{
return $this->hasOne(Tags::className(), ['tag_id' => 'nt_tag_id']);
}
News Model:
public function getNewsCategory()
{
return $this->hasMany(NewsCategory::className(), ['nc_news_id' => 'news_id']);
}
public function getNewsTags()
{
return $this->hasMany(NewsTags::className(), ['nt_news_id' => 'news_id']);
}
Tags Model:
public function getNewsTags()
{
return $this->hasMany(NewsTags::className(), ['nt_tag_id' => 'tag_id']);
}
ieです。各カテゴリには複数のニュースが含まれており、各ニュースには複数のタグが含まれており、各カテゴリに関連するすべてのタグが必要です。 もっと正確に言えば、私はすべてのカテゴリと、これらのカテゴリに関連するすべてのタグを表示する列が必要です。 助けてください!