yii2-transpose-dataprovider A Yii2 data provider that transpose data into a table format

Installation

The preferred way to install this extension is through composer.

$ composer require eddmash/transposedataprovider "@dev"
Transposing Query.

Transposes data returned by a query.

Assuming you have a Query that outputs the following :

student | subject | grade
--------------------------
 mat    | cre     | 52
 mat    | ghc     | 40
 mat    | physics | 60
 leon   | cre     | 70
 leon   | ghc     | 80
 leon   | physics | 10

and we need our data to look as below :

student | cre | ghc | physics
------------------------------
 mat    | 52  | 40  | 60
 leon   | 70  | 80  | 10

We achive this by doing :

use Eddmash\TransposeDataProvider;

$dataProvider = new TransposeDataProvider([
     'query' => $query,
     'columnsField' => 'subject',
     'groupField' => 'student',
     'valuesField' => 'grade',
     'pagination' => [
         'pagesize' => $pageSize // in case you want a default pagesize
     ]
]);

By default the transposed output contains only the columns found on the query TransposeDataProvider::$columnsField.

To get other columns present on the query add them to the TransposeDataProvider::$extraFields.

Transposing EAV Data.

The DataProvide also supports EAV setups, assuming we have the following setup.

         Entity
------------------------------
id   | name
-----------------
 1   | cre
 2   | ghc
 3   | physics
 4   | cre
 5   | ghc
 6   | physics
     Value
-----------------------------

entity_id | attribute_id | value
----------------------------------
 1        | 1            | 52
 2        | 2            | yes
 3        | 3            | 100
 4        | 4            | 70
 5        | 5            | it all sucks
 6        | 6            | 10

Attribute

----------------------------------

name         | attribute_id
--------------------------
 maganize    |    1
 range       |    2
 power       |    3
 slogan      |    4
 song        |    5
 fire mode   |    6


To Get the following output ::

entity | magazine | range | power | slogan | song         | fire mode
------------------------------------------------------------------------
  1    |   50     |  yes  | 100   | 70     | it all sucks | 10

Transpose takes another parameter TransposeDataProvider::$columnQuery which should return the columns.

use Eddmash\TransposeDataProvider

$query = Value::find()->joinWith(['attribute attribute', 'attribute.entity entity'])->where(['entity.id'=>5]);

$columnQuery = Attribute::find()->joinWith(['entity entity'])->where(['entity.id'=>5]);

$dataProvider = new TransposeDataProvider([
     'query' => $query,//
     'columnsField' => 'attribute.name',
     'groupField' => 'entity_id',
     'valuesField' => 'value',
     'columnsQuery' => $columnQuery,
     'pagination' => [
         'pagesize' => 10
     ]
]);
0 0
2 followers
0 downloads
Yii Version: 2.0
License: MIT
Category: Others
Developed by: eddmash
Created on: Sep 12, 2017
Last updated: 7 months ago

Related Extensions