title: Yii2 many-to-many model relation
body: <p>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.<br>
  Slider.php <br></p>
  
  <pre><code>...
  public function getImages(){
      return $this-&gt;hasMany(Images::className(), ['id' =&gt; 'image_id'])
          -&gt;viaTable('sliders_images', ['slider_id' =&gt; 'id']);
  }
  ...
  </code></pre>
  
  <p>Images.php<br></p>
  
  <pre><code>...
  public function getSlider(){
       return $this-&gt;hasMany(Slider::className(), ['slider_id' =&gt; 'id'])
          -&gt;viaTable('sliders_images', ['image_id' =&gt; 'id']);
  }
  ...
  </code></pre>
  
  <p>SlidersImages.php</p>
  
  <pre><code>...
  /**
   * @return \yii\db\ActiveQuery
   */
  public function getImage()
  {
      return $this-&gt;hasOne(Images::className(), ['id' =&gt; 'image_id']);
  }
  
  /**
   * @return \yii\db\ActiveQuery
   */
  public function getSlider()
  {
      return $this-&gt;hasOne(Slider::className(), ['id' =&gt; 'slider_id']);
  }
  ...
  </code></pre>
  
  <p>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 (<a href="http://www.yiiframework.com/doc-2.0/guide-db-active-record.html#relational-data" rel="nofollow">Yii2 documentation</a>):</p>
  
  <pre><code>public function actionView($id)
  {
      $slider = $this-&gt;findModel($id);
      return $this-&gt;render('view', [
          'model' =&gt; $slider,
          'images' =&gt; $slider-&gt;images
      ]);
  }
  
  protected function findModel($id)
  {
      if (($model = Slider::findOne($id)) !== null) {
          return $model;
      } else {
          throw new NotFoundHttpException('The requested page does not exist.');
      }
  }
  </code></pre>
  
  <p>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?</p>
  
  <p><strong>Edit:</strong><br>
  when i try to access at slidersImage to get the rows of juncyion table:<code>$slider-&gt;sliderImage</code> work fine, miss the access at images row.</p>
  
  <p>slider table <br></p>
  
  <pre>
  id | nome         |  descrizione | active
  -------------------------------------------
  28 | adfjkhbfvòja | JAFNHÒDF     | 1
  </pre>
  
  <p><br>
  sliders_images table</p>
  
  <pre>
  slider_id | image_id | display_order|
  --------------------------------------
  28        | 16       | 3            |
  --------------------------------------
  28        | 17       | 5            |
  </pre>
  
  <p><br>
  images table</p>
  
  <pre>
  id | date                | url     |
  ------------------------------------
  16 | 2016-06-21 16:21:04 | img/url |
  ------------------------------------
  17 | 2016-06-21 16:22:37 | img/url |
  </pre>
  
  <p><strong>Edit2:</strong><br></p>
  
  <p>The database sequence from debugger:<br></p>
  
  <pre>
  
      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)
  
  </pre>