Dxx
Dxx

Reputation: 934

Yii2 Combine 2 unrelated models with some shared properties into one Gridview

I have 2 separate models (for 2 separate tables) containing some similar properties, which I want to combine into one GridView. The models are unrelated.

Model 1:

+----+------+----------------+-----+
| id | name |     email      | age |
+----+------+----------------+-----+
|  1 | Bob  | [email protected]    |  22 |
|  2 | Ross | [email protected] |  24 |
+----+------+----------------+-----+

Model 2:

+----+-------+-----------------+----------+-----------+
| id | name  |      email      | location | Interests |
+----+-------+-----------------+----------+-----------+
|  1 | Mr    | [email protected]        | Middle   | Computers |
|  2 | Robot | [email protected] | Nowhere  | Hacking   |
+----+-------+-----------------+----------+-----------+

I want to export the following data to a CSV (using kartik/Grid/Gridview/ExportMenu), which works similarly to a GridView:

+----+-------+-----------------+----------+-----+-----------+
| id | name  |      email      | Location | Age | Interests |
+----+-------+-----------------+----------+-----+-----------+
|  1 | Mr    | [email protected]        | Middle   |     | Computers |
|  2 | Robot | [email protected] | Nowhere  |     | Hacking   |
|  3 | Bob   | [email protected]     |          |  22 |           |
|  4 | Ross  | [email protected]  |          |  24 |           |
+----+-------+-----------------+----------+-----+-----------+

The export widget works the same as a CGridView. You supply a dataProvider (could have pagination) and the exported CSV contains all rows.

Currently I'm using Search models to return 2 ActiveDataProviders, and then I combine them with ArrayDataProvider:

$searchModel = new RegisteredUserSearch([$argumentsArray1]);
$dataProvider1 = $searchModel->search(Yii::$app->request->queryParams);
$dataProvider1->pagination = ['pageSize' => 12];

$collectedEmailSearchModel = new CollectedEmailSearch([$argumentsArray2]);
$dataProvider2 = $collectedEmailSearchModel->search(Yii::$app->request->queryParams);
$dataProvider2->pagination = ['pageSize' => 12];

$data = array_merge($dataProvider1->getModels(), $dataProvider2->getModels());

$dataProvider = new ArrayDataProvider([
    'allModels' => $data,
            'pagination'=>[
                'pageSize'=> 0
            ]
]);

Using $dataProvider1 or $dataProvider2 as the GridView's dataProvider works fine, but using the combined $dataProvider results in exporting only 24 rows. I tried changing the pageSize of the dataProviders to 0, but that doesn't appear to make a difference.

Upvotes: 1

Views: 801

Answers (1)

tigrasti
tigrasti

Reputation: 342

You can make relationship between models on same email. For example

In Model1.php you must define relationship in this case by same email

public function getSameEmail()
{
    return $this->hasOne(Model2::className(),['email'=>'email']);
}

To display in grid view values from other table you must join it in searchModel1.php file. You are joining tables by same email.

after validation you can add relationship you define to query. Like this

$query->joinWith('sameEmail');

And you will get values of columns from another table. Be careful with columns with same name.

Upvotes: 1

Related Questions