boot_strap
(Lereddragon)
1
Hi everybody,
I have some data in tow different Model one in Job (Department, Company…) and Employee (EmpName, EmpAge, EmpPosition…)
How can I use both of the models inside one gridview in order to have a table like (EmpName, EmpPosition, Company, Department…)
I know How to create gridview from one Model
<?= GridView::widget([
'dataProvider' => $dataProvider,
'columns' => [
['class' => 'yii\grid\SerialColumn'],
'EmpName',
'EmpPosition', ........... ],
]); ?>
Thanks in advance
boehsermoe
(Boehsermoe)
2
boot_strap
(Lereddragon)
3
thanks for the reply.
but it’s not clear I don’t understand how can I apply it in my case 
boehsermoe
(Boehsermoe)
4
Well, I assume that each Department have a relation to the a Company, right?
Now you can get all Department from your database with something like that
$query = Department::find()->with('company');
$dataProvider = new ActiveDataProvider(['query' => $query]);
or with the search method
$department = new DepartmentSearch();
$dataProvider = $department->search(Yii::$app->request->queryParams)
In your view you can access the Company of each Department
<?= GridView::widget([
'dataProvider' => $dataProvider,
'columns' => [
['class' => 'yii\grid\SerialColumn'],
'DepartName',
'DepartCompany.name',
'DepartCompany.city',
...
],
]); ?>
The same thing you can do with the Employee, instead of Department use the Employee and access the Department and Company.
I think the Yii2 Docu could help you: http://www.yiiframework.com/doc-2.0/guide-db-active-record.html#working-with-relational-data
boot_strap
(Lereddragon)
5
I am not sure you get what I want (or maybe it’s me who didn’t understand, if it’s the case sorry)
The columns Company and Department(and others) are in the same table (Model) called JOB
The columns EmpName,EmpName, EmpAge, EmpPosition (and others) are in the table EMPLOYEE
what I want is to combine (join)the columns of the tow tables (models) in order to get one Gridview with all the columns from the tow Models
something like
EmpName, EmpAge, EmpPosition Company Department
John Doe 33 Geek MilkeSoft HR
I know how to do to create a GridView for each model separately in tow different views
remram
(Mster)
6
boehsermoe
(Boehsermoe)
7
Exacly what I mean.The tables/models EMPLOYEE and JOB have to be a relation (the EMPLOYEE table should have a column for a JOB id).
Your $dataProvider contains your EMPLOYEE objects, on that way you can access the JOB.
<?= GridView::widget([
'dataProvider' => $dataProvider,
'columns' => [
['class' => 'yii\grid\SerialColumn'],
'EmpName',
'EmpAge',
'EmpPosition',
'JOB.Company',
'JOB.Department',
],
]); ?>
I think you should read the docu, it is really helpfully.
boot_strap
(Lereddragon)
8
Now I got It
thanks guys for your help