Create two simple tables (employee, company) based on the [[A simple migration example]]\\

Then use Gii to create the models and the CRUD for these two tables.

Now in the //Employee// view, we want to get the company name in the GridView.

In the ''/models/Employee.php'' file, put in a function at the bottom to show the relation
<code php>
    /**
    * @return \yii\db\ActiveQuery
    */
    public function getCompany()
    {
        return $this->hasOne(Company::className(), ['id' => 'company_id']);
    }
</code>

Next in ''\models\EmployeeSearch.php'' at the top add
<code php>
use app\models\Company;
</code>

Then modify to put a public variable for the name string as shown
<code php>
 class EmployeeSearch extends Employee
 {
    public $company;
</code>

Under ''rules'' add ''company'' as safe so it is displayed
<code php>
            [['emp_name', 'emp_email', 'company', 'expiry_date', 'created_at', 'modified_at'], 'safe'],
</code>

In the ''search'' function, modify like so
<code php>
    public function search($params)
    {
        $query = Employee::find();
        $query->joinWith(['company']);
</code>

In the ''\views\employee\index.php'' file, put in a column for the Company Name in the ''columns'' block
<code php>
            [
                'attribute' => 'Company Name',
                'value' => 'company.name'
            ],
</code> 

In the ''\views\employee\view.php'' file, put into the ''DetailView::widget'' under ''attributes''
<code php>
            'company.name',
</code>

Back to [[yii|Yii Main Index]]