Answer the question
In order to leave comments, you need to log in
Teach how to form a filter in a GridView to a related model?
Hello again!
Tell me (I would even say - TEACH!) the solution to this problem:
There is an Order table and there is a
standard connection for the Portfolio table:
public function getPortfolio()
{
return $this->hasOne(Portfolio::class, ['id' => 'portfolio_id']);
}
[
'attribute' => 'portfolio_id',
'headerOptions' => ['class' => 'col-md-1'],
//'headerOptions' => ['width' => '50'],
'format' => 'text',
'filter' => Html::activeDropDownList($searchModel, 'portfolio_id', Portfolio::getNames(), // возвращаем ВСЕ портфели
['class' => 'form-control', 'prompt' => 'Все']),
'value' => 'portfolio.title'
],
Answer the question
In order to leave comments, you need to log in
In the gridView, set the option 'filterModel' => $searchModel
for the entire table.
What does Portfolio::getNames() return, just the names?
I would rewrite the filter parameters a little differently.
[
'attribute' => 'id_portfolio',
'headerOptions' => ['class' => 'col-md-1'],
'format' => 'text',
'filter' => Portpholio::getAllPortfolio(),
'value' => 'portfolio.title'
],
public static function getAllPortfolio()
{
return ArrayHelper::map(self::find()->orderBy(['name' => SORT_ASC])->all(), 'id', 'name');
}
public $id_portfolio;
$query = Order::find()->with('portfolio');
$query->andFilterWhere('{{%portfolio}}.id' => $this->id_portfolio);
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question