我在两个 yii2 模型之间实现了多对多关系:滑块、图像、滑块图像,其中滑块图像是连接表。每个模型都扩展了 Gii 生成的基本模型,所以当我需要时,我可以覆盖基本模型而不会丢失个人方法。
滑块.php
...
public function getImages(){
return $this->hasMany(Images::className(), ['id' => 'image_id'])
->viaTable('sliders_images', ['slider_id' => 'id']);
}
...
图片.php
...
public function getSlider(){
return $this->hasMany(Slider::className(), ['slider_id' => 'id'])
->viaTable('sliders_images', ['image_id' => 'id']);
}
...
SlidersImages.php
...
/**
* @return \yii\db\ActiveQuery
*/
public function getImage()
{
return $this->hasOne(Images::className(), ['id' => 'image_id']);
}
/**
* @return \yii\db\ActiveQuery
*/
public function getSlider()
{
return $this->hasOne(Slider::className(), ['id' => 'slider_id']);
}
...
当我使用函数 link() 在创建滑块时填充连接表时,一切正常,但是当我尝试从滑块 ActiveRecord 对象(Yii2 文档)获取图像时出现问题:
public function actionView($id)
{
$slider = $this->findModel($id);
return $this->render('view', [
'model' => $slider,
'images' => $slider->images
]);
}
protected function findModel($id)
{
if (($model = Slider::findOne($id)) !== null) {
return $model;
} else {
throw new NotFoundHttpException('The requested page does not exist.');
}
}
如果我在视图中调试 $images 变量,这是 null 并且不包含相关图像。我如何设置模型以在关系中获得正确的访问权限?
编辑:
当我尝试访问 slidersImage 以获取 juncyion 表的行时:$slider->sliderImage
工作正常,错过图像行的访问。
滑块表
编号 | 名称 | 描述 | 积极的 ------------------------------------------ 28 | adfjkhbfvòja | JAFNHÒDF | 1
sliders_images 表
滑块 ID | image_id | 显示顺序| -------------------------------------- 28 | 16 | 3 | -------------------------------------- 28 | 17 | 5 |
图像表
编号 | 日期 | 网址 | ---------------------------------- 16 | 2016-06-21 16:21:04 | 图片/网址 | ---------------------------------- 17 | 2016-06-21 16:22:37 | 图片/网址 |
编辑2:
来自调试器的数据库序列:
1 11:27:02.666 0.7 ms 显示来自 `admin` 的完整列 /var/www/html/yii_advance/backend/models/Admin.php (65) -------------------------------------------------- ---------------------- 2 11:27:02.668 0.6 ms 显示“滑块”中的完整列 /var/www/html/yii_advance/common/modules/sliders/controllers/SliderController.php (173) /var/www/html/yii_advance/common/modules/sliders/controllers/SliderController.php (79) -------------------------------------------------- ---------------------- 3 11:27:02.665 0.6 ms SELECT SELECT * FROM `admin` WHERE (`id`=2) AND (`status`=10) /var/www/html/yii_advance/backend/models/Admin.php (65) [+] 解释 -------------------------------------------------- ---------------------- 4 11:27:02.667 0.5 毫秒选择选择 kcu.constraint_name, kcu.column_name, kcu.referenced_table_name, kcu.referenced_column_name FROM information_schema.referential_constraints AS rc JOIN information_schema.key_column_usage AS kcu ON ( kcu.constraint_catalog = rc.constraint_catalog 或 (kcu.constraint_catalog 为空且 rc.constraint_catalog 为空) ) 和 kcu.constraint_schema = rc.constraint_schema AND kcu.constraint_name = rc.constraint_name WHERE rc.constraint_schema = database() AND kcu.table_schema = database() AND rc.table_name = 'admin' AND kcu.table_name = 'admin' /var/www/html/yii_advance/backend/models/Admin.php (65) [+] 解释 -------------------------------------------------- ---------------------- 5 11:27:02.669 0.5 毫秒选择选择 kcu.constraint_name, kcu.column_name, kcu.referenced_table_name, kcu.referenced_column_name FROM information_schema.referential_constraints AS rc JOIN information_schema.key_column_usage AS kcu ON ( kcu.constraint_catalog = rc.constraint_catalog 或 (kcu.constraint_catalog 为空且 rc.constraint_catalog 为空) ) 和 kcu.constraint_schema = rc.constraint_schema AND kcu.constraint_name = rc.constraint_name WHERE rc.constraint_schema = database() AND kcu.table_schema = database() AND rc.table_name = 'slider' AND kcu.table_name = 'slider' /var/www/html/yii_advance/common/modules/sliders/controllers/SliderController.php (173) /var/www/html/yii_advance/common/modules/sliders/controllers/SliderController.php (79) [+] 解释 -------------------------------------------------- ---------------------- 6 11:27:02.669 0.4 毫秒 SELECT SELECT * FROM `slider` WHERE `id`='28' /var/www/html/yii_advance/common/modules/sliders/controllers/SliderController.php (173) /var/www/html/yii_advance/common/modules/sliders/controllers/SliderController.php (79)