简体   繁体   English

Yii2多对多模型关系

[英]Yii2 many-to-many model relation

I've implemented a many to many ralation between two yii2 models: slider, images, sliders_images where sliders_images is the junction table. 我已经实现了两个yii2模型之间的多对多关系:滑块,图像,sliders_images,其中sliders_images是联结表。 Each model extend a basic model generated by Gii, so when i need i can overwrite the base model without lose personal method. 每个模型都扩展了Gii生成的基本模型,因此当我需要时我可以覆盖基本模型而不会丢失个人方法。
Slider.php Slider.php

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

Images.php Images.php

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

SlidersImages.php 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']);
}
...

When I use the function link() to popolate the junction table on create a slider all work fine but the problem occurs when i try to get images from slider ActiveRecord object ( Yii2 documentation ): 当我使用函数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.');
    }
}

If i debug $images variable in the view this is null and not contain the related images. 如果我在视图中调试$ images变量,则为null,并且不包含相关图像。 How i can set the models for obtain the right access at the relations? 我如何设置模型以在关系处获得正确的访问权限?

Edit: 编辑:
when i try to access at slidersImage to get the rows of juncyion table: $slider->sliderImage work fine, miss the access at images row. 当我尝试访问slidersImage以获取juuncion表的行时: $slider->sliderImage工作正常,但是错过了对图像行的访问。

slider table 滑台

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


sliders_images table slides_images表

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


images table 图片表

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

Edit2: 编辑2:

The database sequence from debugger: 来自调试器的数据库序列:

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)

This public $images; public $images; property should be renamed because it coincides with the name of relation getImages() 应该重命名属性,因为它与关系getImages()的名称一致

class Slider extends Sl
{
    const SCENARIO_CREATE = 'create';
    const SCENARIO_VIEW = 'view';
    const SCENARIO_UPDATE = 'update';

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM