MarBer
MarBer

Reputation: 595

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. Each model extend a basic model generated by Gii, so when i need i can overwrite the base model without lose personal method.
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']);
}
...

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):

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. 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.

slider table

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


sliders_images table

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:

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)

Upvotes: 1

Views: 1016

Answers (1)

Vitaly
Vitaly

Reputation: 1281

This public $images; property should be renamed because it coincides with the name of relation getImages()

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

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

Upvotes: 2

Related Questions