Yii2多对多模型关系

时间:2016-06-23 08:28:28

标签: php activerecord yii2 relational-database yii2-model

我在两个yii2模型之间实现了多对多的关系: 滑块,图像,sliders_images,其中sliderrs_images是联结表。 每个模型都扩展了由Gii生成的基本模型,因此当我需要时,我可以覆盖基本模型而不会丢失个人方法 Slider.php

...
public function getImages(){
    return $this->hasMany(Images::className(), ['id' => 'image_id'])
        ->viaTable('sliders_images', ['slider_id' => 'id']);
}
...

Images.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 documentation)获取图像时出现问题:

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正常工作,错过了图像行的访问权。

滑块表

id | nome         |  descrizione | active
-------------------------------------------
28 | adfjkhbfvòja | JAFNHÒDF     | 1


sliders_images表

slider_id | image_id | display_order|
--------------------------------------
28        | 16       | 3            |
--------------------------------------
28        | 17       | 5            |


图像表

id | date                | url     |
------------------------------------
16 | 2016-06-21 16:21:04 | img/url |
------------------------------------
17 | 2016-06-21 16:22:37 | img/url |

EDIT2:

来自调试器的数据库序列:


    1   11:27:02.666    0.7 ms  SHOW    SHOW FULL COLUMNS FROM `admin`
    /var/www/html/yii_advance/backend/models/Admin.php (65)
-------------------------------------------------------------------------
    2   11:27:02.668    0.6 ms  SHOW    SHOW FULL COLUMNS FROM `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)
--------------------------------------------------------------------------
    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)
    [+] Explain
--------------------------------------------------------------------------
    4   11:27:02.667    0.5 ms  SELECT  SELECT
        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 OR
            (kcu.constraint_catalog IS NULL AND rc.constraint_catalog IS NULL)
        ) AND
        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)
    [+] Explain
--------------------------------------------------------------------------
    5   11:27:02.669    0.5 ms  SELECT  SELECT
        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 OR
            (kcu.constraint_catalog IS NULL AND rc.constraint_catalog IS NULL)
        ) AND
        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)
    [+] Explain
--------------------------------------------------------------------------
    6   11:27:02.669    0.4 ms  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)

1 个答案:

答案 0 :(得分:1)

应重命名此public $images;属性,因为它与关系getImages()

的名称一致
class Slider extends Sl
{
    const SCENARIO_CREATE = 'create';
    const SCENARIO_VIEW = 'view';
    const SCENARIO_UPDATE = 'update';

    public $images; // This should be renamed
    ...