Sort gridview with data from 1:n relationship from junction table
i have a 3 table
-
product_tbl // hold product data
-
specification_tbl // hold all type of specification that my product can have
-
product_specification_tbl // (kind of Junction table)hold each Product Specification Value based on [$name => $value]format. relation between product_tbl and product_specification_tbl is 1:n
product-id specification-id name value 1 10 Tire Width 250 1 11 Aspect Ratio 50 1 12 Rim Size 16 2 10 Tire Width 195 2 11 Aspect Ratio 70 2 12 Rim Size 14
now i need combine this specification as follow:
| product_id | [Tire Width]/[Aspect Ratio]R[Rim Size] |
| | as Tire Size |
|------------|----------------------------------------|
| 1 | 250/50R16 |
| 2 | 195/70R14 |
so now i have need to have new column in gridView with ability of sorting based on new combined value (Tire Size Column)
problem is that sorting generally perform on the column but here i need to Sort my products based on a specification value (tire width, aspect ratio, rim size and ... )that is stored in database junction table rows not column.
i think i should first create temporary table that holds specifications indexed by product id and store each data in separate column. or i don't know maybe there is another way?!?
either way i don't know how to do it!!!
i will be glad if someone can help me on this topic. thank you.
<---- GridView Code -------------------------------------------------->
<?php
echo GridView::widget([
'dataProvider' => $dataProvider,
'columns' => [
['class' => 'yii\grid\SerialColumn'],
[
'attribute' => 'brand.name_en',
'label' => 'Brand'
],
[
'attribute' => 'family.name_en',
'label' => 'Model',
],
[
'attribute' => 'productTreadWidth.value_en',
'label' => Yii::t('app' , 'Tire Width'),
'content' => function($model){
return $model->productTreadWidth['value_en'];
}
],
[
'attribute' => 'productAspectRatio.value_en',
'label' => Yii::t('app' , 'Aspect Ratio'),
'content' => function($model){
return $model->productAspectRatio['value_en'];
}
],
[
'attribute' => 'productDiameter.value_en',
'label' => Yii::t('app' , 'Diameter'),
'content' => function($model){
return $model->productDiameter['value_en'];
}
],
'name_en',
'product_id',
],
]);
?>
<---- Relation Code -------------------------------------------------->
<?php
public function getProductTreadWidth()
{
return $this->hasOne(ProductSpecification::className(), ['product_id' => 'product_id'])->where(['specification_id' => 2]);
}
public function getProductAspectRatio()
{
return $this->hasOne(ProductSpecification::className(), ['product_id' => 'product_id'])->where(['specification_id' => 3]);
}
public function getProductDiameter()
{
return $this->hasOne(ProductSpecification::className(), ['product_id' => 'product_id'])->where(['specification_id' => 5]);
}
?>
now new problem rise :D i can sort separatley by Tread Width | Aspect Ratio | Diameter
but i need all this 3 column sort ASC or DESC at the same time.
Database Table: enter image description here
select Query Result :
Column i need perform sort on it:
Comments
Post a Comment