Yii2 - How to get data from 2 tables

[Introduction, the real question is below, if you want just skip this part]

Ok, so I know that there are already some posts about this, but the thing is that Im new to yii and I’ve been having this problem for 3 days now, I already check in several forums, the definitive guide, manuals and all that stuff, but for some reason nothing works for me (I know the problem it’s me)

[Intro end]

[Real question begins here]:

So I’m usign the CRUD models, views and controllers from the Gii CRUD generator, and what I want to do is really simple.

I want this Id Author field in the index view to show the Author’s Name instead.

I have just 2 tables on the data base. author and post, so post has a foreign key to id_author an the author table and Gii already generated the relations on the code, but I’m not quite sure how to use them.

Let me give you know code and screen captures.

Author Model


class Author extends \yii\db\ActiveRecord

{

    /**

     * @inheritdoc

     */

    public static function tableName()

    {

        return 'author';

    }


    /**

     * @inheritdoc

     */

    public function rules()

    {

        return [

            [['firstName', 'lastName', 'username', 'password'], 'string', 'max' => 45],

            [['authKey'], 'string', 'max' => 10],

            [['fullName'], 'string', 'max' => 90],

        ];

    }


    /**

     * @inheritdoc

     */

    public function attributeLabels()

    {

        return [

            'id_author' => 'Id Author',

            'firstName' => 'First Name',

            'lastName' => 'Last Name',

            'username' => 'Username',

            'password' => 'Password',

            'authKey' => 'Auth Key',

            'fullName' => 'Full Name',

        ];

    }


    /**

     * @return \yii\db\ActiveQuery

     */

    public function getPosts()

    {

        return $this->hasMany(Post::className(), ['id_author' => 'fk_post_author']);

    }

    public function getAuthKey(){

        return (string)rand(100000, 999999);

    }

Post Model


class Post extends \yii\db\ActiveRecord

{

    /**

     * @inheritdoc

     */

    public static function tableName()

    {

        return 'post';

    }


    /**

     * @inheritdoc

     */

    public function rules()

    {

        return [

            [['id_author'], 'required'],

            [['id_author'], 'integer'],

            [['title'], 'string', 'max' => 45],

            [['desciption'], 'string', 'max' => 500],

            [['id_author'], 'exist', 'skipOnError' => true, 'targetClass' => Author::className(), 'targetAttribute' => ['id_author' => 'id_author']],

        ];

    }


    /**

     * @inheritdoc

     */

    public function attributeLabels()

    {

        return [

            'id_post' => 'Id Post',

            'id_author' => 'Id Author',

            'title' => 'Title',

            'desciption' => 'Desciption',

        ];

    }


    /**

     * @return \yii\db\ActiveQuery

     */

    public function getAuthor()

    {

        return $this->hasOne(Author::className(), ['fk_post_author' => 'id_author']);

    }

    

    

}

Post index View


$this->title = $model->title;

$this->params['breadcrumbs'][] = ['label' => 'Posts', 'url' => ['index']];

$this->params['breadcrumbs'][] = $this->title;

?>

<div class="post-view">


    <h1><?= Html::encode($this->title) ?></h1>


    <p>

        <?= Html::a('Update', ['update', 'id' => $model->id_post], ['class' => 'btn btn-primary']) ?>

        <?= Html::a('Delete', ['delete', 'id' => $model->id_post], [

            'class' => 'btn btn-danger',

            'data' => [

                'confirm' => 'Are you sure you want to delete this item?',

                'method' => 'post',

            ],

        ]) ?>

    </p>


    <?= DetailView::widget([

        'model' => $model,

        'attributes' => [

            'id_post',

            'id_author',

            'title',

            'desciption',

            

            

            ],

    ]) ?>


</div>

And finally this is the page where I want to show the Author’s name instead of the Author ID

6965

Captura de pantalla de 2016-05-13 14-06-08.png

As per comment at top of DetailView class, try:




<?= DetailView::widget([

    'model' => $model,

    'attributes' => [

        'id_post',

        [

            'label' => $model->author->getAttributeLabel('fullName'),

            'value' => $model->author->fullName,

        ],

        'title',

        'desciption',




    ],

]); ?>



BTW, missing ‘r’ out of Description :)