Answer the question
In order to leave comments, you need to log in
Yii2. How to select through a linked table?
There are three tables
CREATE TABLE `c_site` (
`id_site` int(11) unsigned NOT NULL AUTO_INCREMENT,
`site_domain` varchar(255) DEFAULT NULL,
PRIMARY KEY (`id_site`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8;
CREATE TABLE `c_category` (
`id_category` int(11) unsigned NOT NULL AUTO_INCREMENT,
`name_ru` varchar(255) DEFAULT NULL,
PRIMARY KEY (`id_category`)
) ENGINE=InnoDB AUTO_INCREMENT=20 DEFAULT CHARSET=utf8;
CREATE TABLE `c_category_join_site` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`id_site` int(11) DEFAULT NULL,
`id_category` int(11) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8;
// model Category
public function getSites(){
return $this
->hasMany(Site::className(),['id_site'=>'id_category'])
->viaTable('c_category_join_site', ['id_category' => 'id_category']);
}
Answer the question
In order to leave comments, you need to log in
And you would try to generate your model through Gii
public function getSites() {
return $this
->hasMany(Site::className(),['id_site'=>'id_site']) // !!!
->viaTable('c_category_join_site', ['id_category' => 'id_category']);
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question