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.
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 |
getColumn($columnLabel)
getDataColumns() : mixed
getLowerRow(\yii\data\Pagination $pagination, $rows) : integer
| author | Eddilbert Macharia (http://eddmash.com) |
|---|
\yii\data\Pagination
integergetQueryColumns() : array
getUpperRow(\yii\data\Pagination $pagination, $rows) : integer
| author | Eddilbert Macharia (http://eddmash.com) |
|---|
\yii\data\Pagination
integerinit()
This method will initialize the [[db]] property to make sure it refers to a valid DB connection.
\yii\base\InvalidConfigException |
if [[db]] is invalid |
|---|
conformColumn($name) : mixed
getCleanColumn($column) : mixed
getColumnValue($model, null $column = null) : mixed
getDistinctColumns() : array | array<mixed,\yii\db\ActiveRecord>
we use {see @columnsField } to determine the rows.
| author | Eddilbert Macharia (http://eddmash.com) |
|---|
arrayarray<mixed,\yii\db\ActiveRecord>
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) |
|---|
arrayarray<mixed,\yii\db\ActiveRecord>
isValidVariableName($name) : mixed
prepareKeys(array $models) : array
arraythe available data models
arraythe keysprepareModels() : array
| author | Eddilbert Macharia (http://eddmash.com) |
|---|
\yii\base\InvalidConfigException |
|---|
arraythe available data modelsprepareTotalCount()
transpose($models) : array
The desired output is dictated by : see @groupField see @valuesField see @columnsField
| author | Eddilbert Macharia (http://eddmash.com) |
|---|
array$columnsField :
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.
$columnsQuery : \yii\db\QueryInterface
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.
$extraFields : array
array()For relational fields use the dot notation, [student.role.name] this will add the role name of each student to the transposed data.
$groupField :
e.g. student in the example above.
$labelsField :
$query : \yii\db\QueryInterface
$valuesField :
$_columns :
$_rows :