Yii2 multiple table in gridview

Multi tool use
Yii2 multiple table in gridview
I have 2 tables with strategy
and risk_colors
.
strategy
risk_colors
I generated Model and CRUD with GII and modified it just a little to get risk_value
column in my GridView widget.
risk_value
Here is my Strategy.php
Strategy.php
<?php
namespace backendmodels;
use Yii;
use yiidbActiveRecord;
use yiihelpersUrl;
use yiihelpersHtml;
use yiihelpersArrayHelper;
use yiidbExpression;
/**
* This is the model class for table "strategy".
*
* @property int $id
* @property string $strategy_title
* @property string $strategy_description
* @property string $strategy_current_money
* @property int $risk_colors_id
*
* @property SelectedStrategies $selectedStrategies
* @property RiskColors $riskColors
*/
class Strategy extends yiidbActiveRecord
{
/**
* {@inheritdoc}
*/
public static function tableName()
{
return 'strategy';
}
/**
* {@inheritdoc}
*/
public function rules()
{
return [
[['strategy_title', 'strategy_description', 'strategy_current_money', 'risk_colors_id'], 'required'],
[['strategy_current_money', 'risk_colors_id'], 'integer'],
[['strategy_title'], 'string', 'max' => 255],
[['strategy_description'], 'string', 'max' => 2055],
[['risk_colors_id'], 'exist', 'skipOnError' => true, 'targetClass' => RiskColors::className(), 'targetAttribute' => ['risk_colors_id' => 'id']],
];
}
/**
* {@inheritdoc}
*/
public function attributeLabels()
{
return [
'id' => 'ID',
'strategy_title' => 'Strategy Title',
'strategy_description' => 'Strategy Description',
'strategy_current_money' => 'Strategy Current Money',
'risk_colors_id' => 'Risk Color ID',
'riskValue' => Yii::t('app', 'Risk'),
'colorNumber' => 'Color Number',
];
}
/**
* @return yiidbActiveQuery
*/
public function getSelectedStrategies()
{
return $this->hasMany(SelectedStrategies::className(), ['strategy_id' => 'id']);
}
/**
* @return yiidbActiveQuery
*/
public function getRiskColors()
{
return $this->hasOne(RiskColors::className(), ['id' => 'risk_colors_id']);
}
/**
* @return risk value
*/
public function getRiskValue()
{
return $this->riskColors->risk_value;
}
/**
* @return Get risk value list for dropdown
*/
public function getRiskValueList()
{
$droptions = RiskColors::find()->asArray()->all();
return ArrayHelper::map($droptions, 'id', 'risk_value');
}
/**
* @return Get risk value list for dropdown
*/
public function getColorNumberList()
{
$droptions = RiskColors::find()->asArray()->all();
return ArrayHelper::map($droptions, 'id', 'color_number');
}
}
here is my index.php.
After all i got risk_value
column in my GridView, but it looks like i cant sort my table by this field. Here is .
risk_value
Here is my Search Model
So my question is what should i do to make this field sortable?
Post your code directly in question.
– rob006
38 mins ago
yiiframework.com/wiki/653/…
– scaisEdge
37 mins ago
your
index.php
points to the wrong link, it shows model rather than the view, and you should add the StrategySearch
model rather than Strategy
model– Muhammad Omer Aslam
17 mins ago
index.php
StrategySearch
Strategy
Thank you, i edited question.
– Zurab Gostohov
11 mins ago
1 Answer
1
In your StrategySearch.php
public function search($params)
{
........
// create ActiveQuery
$query = Strategy::find();
$query->joinWith(['riskColors']);
$dataProvider = new ActiveDataProvider([
'query' => $query,
]);
$dataProvider->sort->attributes['risk_value'] = [
'asc' => ['risk_colors.col_name' => SORT_ASC],
'desc' => ['risk_colors.col_name' => SORT_DESC],
];
...........
}
Refere GridView Sorting
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
please give your StrategySerch.php model
– vishuB
39 mins ago