title: How to show relational data in yii2
body: <p>I'm having trouble understanding how relations work in yii 2</p>
  
  <p>I have 2 tables in a mysql database, author and book.</p>
  
  <p>book has a column named author which links to the id of the author table via foreign key.</p>
  
  <p>I've generated CRUD using gii, and I want the author name to appear in the list view, as well as dropdowns for the author name in the create and update views.</p>
  
  <p>But I cant seem to get the relation working even in the list view.</p>
  
  <p>Here's my code </p>
  
  <p>Book Model:</p>
  
  <pre><code>&lt;?php
  
  namespace app\models;
  
  use Yii;
  use app\models\Author;
  
  /**
   * This is the model class for table "book".
   *
   * @property integer $id
   * @property string $name
   * @property integer $author
   */
  class Book extends \yii\db\ActiveRecord
  {
      /**
       * @inheritdoc
       */
      public static function tableName()
      {
          return 'book';
      }
  
      /**
       * @inheritdoc
       */
      public function rules()
      {
          return [
              [['name', 'author'], 'required'],
              [['author'], 'integer'],
              [['name'], 'string', 'max' =&gt; 11]
          ];
      }
  
      /**
       * @inheritdoc
       */
      public function attributeLabels()
      {
          return [
              'id' =&gt; 'ID',
              'name' =&gt; 'Name',
              'author' =&gt; 'Author',
          ];
      }
  
      public function getAuthor()
      {
          return $this-&gt;hasOne(Author::className(), ['id' =&gt; 'author']);
      }
  }
  </code></pre>
  
  <p>BookSearch Model:</p>
  
  <pre><code>&lt;?php
  
  namespace app\models;
  
  use Yii;
  use yii\base\Model;
  use yii\data\ActiveDataProvider;
  use app\models\Book;
  
  /**
   * BookSearch represents the model behind the search form about `app\models\Book`.
   */
  class BookSearch extends Book
  {
      /**
       * @inheritdoc
       */
      public function rules()
      {
          return [
              [['id', 'author'], 'integer'],
              [['name'], 'safe'],
          ];
      }
  
      /**
       * @inheritdoc
       */
      public function scenarios()
      {
          // bypass scenarios() implementation in the parent class
          return Model::scenarios();
      }
  
      /**
       * Creates data provider instance with search query applied
       *
       * @param array $params
       *
       * @return ActiveDataProvider
       */
      public function search($params)
      {
          $query = Book::find();
          $query-&gt;joinWith('author');
  
          $dataProvider = new ActiveDataProvider([
              'query' =&gt; $query,
          ]);
  
          var_dump($dataProvider);
          if (!$this-&gt;validate()) {
              // uncomment the following line if you do not want to return any records when validation fails
              // $query-&gt;where('0=1');
              return $dataProvider;
          }
  
          $query-&gt;andFilterWhere([
              'id' =&gt; $this-&gt;id,
              'author' =&gt; $this-&gt;author,
          ]);
  
          $query-&gt;andFilterWhere(['like', 'name', $this-&gt;name]);
  
          return $dataProvider;
      }
  }
  </code></pre>
  
  <p>Also, here's the view file:</p>
  
  <pre><code>&lt;?php
  
  use yii\helpers\Html;
  use yii\grid\GridView;
  
  /* @var $this yii\web\View */
  /* @var $searchModel app\models\BookSearch */
  /* @var $dataProvider yii\data\ActiveDataProvider */
  
  $this-&gt;title = 'Books';
  $this-&gt;params['breadcrumbs'][] = $this-&gt;title;
  ?&gt;
  &lt;div class="book-index"&gt;
  
      &lt;h1&gt;&lt;?= Html::encode($this-&gt;title) ?&gt;&lt;/h1&gt;
      &lt;?php // echo $this-&gt;render('_search', ['model' =&gt; $searchModel]); ?&gt;
  
      &lt;p&gt;
          &lt;?= Html::a('Create Book', ['create'], ['class' =&gt; 'btn btn-success']) ?&gt;
      &lt;/p&gt;
  
      &lt;?= GridView::widget([
          'dataProvider' =&gt; $dataProvider,
          'filterModel' =&gt; $searchModel,
          'columns' =&gt; [
              ['class' =&gt; 'yii\grid\SerialColumn'],
  
              'id',
              'name',
              [
                  'attribute' =&gt; 'author',
                  'value'     =&gt; 'author.name',
              ],
  
              ['class' =&gt; 'yii\grid\ActionColumn'],
          ],
      ]); ?&gt;
  
  &lt;/div&gt;
  </code></pre>
  
  <p>Author Model:</p>
  
  <pre><code>&lt;?php
  
  namespace app\models;
  
  use Yii;
  
  /**
   * This is the model class for table "author".
   *
   * @property integer $id
   * @property string $name
   */
  class Author extends \yii\db\ActiveRecord
  {
      /**
       * @inheritdoc
       */
      public static function tableName()
      {
          return 'author';
      }
  
      /**
       * @inheritdoc
       */
      public function rules()
      {
          return [
              [['name'], 'required'],
              [['name'], 'string', 'max' =&gt; 200]
          ];
      }
  
      /**
       * @inheritdoc
       */
      public function attributeLabels()
      {
          return [
              'id' =&gt; 'ID',
              'name' =&gt; 'Name',
          ];
      }
  
  
  }
  </code></pre>
  
  <p>I think I may have to change something somehwhere in the author/authorSearch model.</p>
  
  <p>Can someone help</p>
  
  <p>thanks</p>