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 TransposeDataProvider::$columnsField the transposed output contains only the columns found on the query.

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 $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
     ]
]);
author Eddilbert Macharia (http://eddmash.com)
package Default

 Methods

Get a column in the query based on a columnLabel provided.

getColumn($columnLabel) 
author Eddilbert Macharia (http://eddmash.com)

Parameters

$columnLabel

Returns all the columns that relate to the data we are handling, this also includes any extra fields that might have been passed.

getDataColumns() : mixed

Gets the row from which to start our data fetch.

getLowerRow(\yii\data\Pagination $pagination, $rows) : integer
author Eddilbert Macharia (http://eddmash.com)

Parameters

$pagination

\yii\data\Pagination

$rows

Returns

integer

Returns columns found in the query.

getQueryColumns() : array

Gets the row at which we stop fetching data.

getUpperRow(\yii\data\Pagination $pagination, $rows) : integer
author Eddilbert Macharia (http://eddmash.com)

Parameters

$pagination

\yii\data\Pagination

$rows

Returns

integer

Initializes the DB connection component.

init() 

This method will initialize the [[db]] property to make sure it refers to a valid DB connection.

Exceptions

\yii\base\InvalidConfigException if [[db]] is invalid

ensures column to be a valid column name.

conformColumn($name) : mixed
Static
author Eddilbert Macharia (http://eddmash.com)

Parameters

$name

Returns

mixed

Creates the field label.

getCleanColumn($column) : mixed
author Eddilbert Macharia (http://eddmash.com)

Parameters

$column

Returns

mixed

Gets a model and column name and returns the value of the column on the model.

getColumnValue($model, null $column = null) : mixed
author Eddilbert Macharia (http://eddmash.com)

Parameters

$model

$column

null

Returns

mixed

gets the columns that will be used in our final transposed data.

getDistinctColumns() : array | array<mixed,\yii\db\ActiveRecord>

we use {see @columnsField } to determine the rows.

author Eddilbert Macharia (http://eddmash.com)

Returns

arrayarray<mixed,\yii\db\ActiveRecord>

gets the rows of data that our data holds.

getDistinctRows() : array | array<mixed,\yii\db\ActiveRecord>

Note, this will not be a direct mapping of the rows of data in a table.

we use {see @groupField } to determine the rows.

author Eddilbert Macharia (http://eddmash.com)

Returns

arrayarray<mixed,\yii\db\ActiveRecord>

Check if a string can be used as a php variable/ class attribute.

isValidVariableName($name) : mixed
Static
author Eddilbert Macharia (http://eddmash.com)

Parameters

$name

Returns

mixed

Prepares the keys associated with the currently available data models.

prepareKeys(array $models) : array

Parameters

$models

array

the available data models

Returns

arraythe keys

Prepares the data models that will be made available in the current page.

prepareModels() : array
author Eddilbert Macharia (http://eddmash.com)

Exceptions

\yii\base\InvalidConfigException

Returns

arraythe available data models

In this case we return the number of distinct rows based on the groupField {@inheritdoc}

prepareTotalCount() 

This transposes the models passed in it desired output.

transpose($models) : array

The desired output is dictated by : see @groupField see @valuesField see @columnsField

author Eddilbert Macharia (http://eddmash.com)

Parameters

$models

Returns

array

 Properties

 

The column in the columnQuery actually contains the records we need to use as column.

$columnsField : 

Default

This should be a string, it also accepts also a relationship separated by "dot notation" e.g user.name.

NOTE :: this only accepts one level deep. so using the field user.role.permission will fail. This is to allow the use of columnsQuery.

also note that if the columnsQuery is used, the $columnsField should be present in both the $query and the $columnQuery.

in cases where the columnsField is a relationship e.g. "user.name" the data provider will look for the end of the relationship in this case "name" on the $columnsQuery and will look for the relation whole "user.name" in the data $query.

 

Query from which to get the columnsField. The Query should return atleast the columnsField, labelsField .

$columnsQuery : \yii\db\QueryInterface

Default

This will come in handy incases where the dataQuery returns null, this will happend incases where we have columns in one table and the values for those columns in anothe table.

in an Entity–attribute–value model(EAV) kind of set up.

 

Other columns found on the $this->query that should be added to the transposed output.

$extraFields : array

Default

array()

For relational fields use the dot notation, [student.role.name] this will add the role name of each student to the transposed data.

 

This fields is used group together records in the $this->query into actual understandable rows of records.

$groupField : 

Default

e.g. student in the example above.

 

The column to be used to get the labels for the column, use this incase the field used for $columnsField does not consist of user friendly labels.

$labelsField : 

Default

 

the query that is used to fetch data models and [[totalCount]] if it is not explicitly set.

$query : \yii\db\QueryInterface

Default

 

The column in the $this->query that actually contains the records we need to use as values for our columns.

$valuesField : 

Default

 

cache for columns.

$_columns : 

Default

 

cache for rows.

$_rows : 

Default