Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GridView columns title depends of $provider->query->modelClass only, not use both / only $this->grid->filterModel in common case of usage #14829

Closed
bscheshirwork opened this issue Sep 15, 2017 · 3 comments

Comments

@bscheshirwork
Copy link
Contributor

Order of applied ifelse is not ideal for default-like case

/**
* @inheritdoc
* @since 2.0.8
*/
protected function getHeaderCellLabel()
{
$provider = $this->grid->dataProvider;
if ($this->label === null) {
if ($provider instanceof ActiveDataProvider && $provider->query instanceof ActiveQueryInterface) {
/* @var $modelClass Model */
$modelClass = $provider->query->modelClass;
$model = $modelClass::instance();
$label = $model->getAttributeLabel($this->attribute);
} elseif ($provider instanceof ArrayDataProvider && $provider->modelClass !== null) {
/* @var $modelClass Model */
$modelClass = $provider->modelClass;
$model = $modelClass::instance();
$label = $model->getAttributeLabel($this->attribute);
} elseif ($this->grid->filterModel !== null && $this->grid->filterModel instanceof Model) {
$label = $this->grid->filterModel->getAttributeLabel($this->attribute);
} else {
$models = $provider->getModels();
if (($model = reset($models)) instanceof Model) {
/* @var $model Model */
$label = $model->getAttributeLabel($this->attribute);
} else {
$label = Inflector::camel2words($this->attribute);
}
}
} else {
$label = $this->label;
}
return $label;
}

What steps will reproduce the problem?

  1. Gii-generated CRUD: Search model + GridView::widget
  2. Expand Search model with addition field - for example name of relation model.
    order rule + search rule + join in query
  3. Expand Search model attributeLabels
    /**
     * @inheritDoc
     */
    public function attributeLabels()
    {
        return ArrayHelper::merge(parent::attributeLabels(), [
            'relationName' => Yii::t('relation', 'Name'),
        ]);
    }
  1. Use array-definition for column with attribute
        'columns' => [
            [
                'format' => 'raw',
                'attribute' => 'relationName',
                'content' => \backend\views\ViewHelper::relatedName(),//function with view
            ],
        ],

What is the expected result?

Table header fill from searchModel attributeLabels()

What do you get instead?

Table header fill from dataProviders model attributeLabels()
case of line 147

if ($provider instanceof ActiveDataProvider && $provider->query instanceof ActiveQueryInterface)...

and I must put unexpected additional label

        'columns' => [
            [
                'format' => 'raw',
                'attribute' => 'relationName',
                'label' => $searchModel->getAttributeLabel('relationName'),
                'content' => \backend\views\ViewHelper::relatedName(),//function with view
            ],
        ],

Additional info

Q A
Yii version 2.0.13@dev
@klimov-paul
Copy link
Member

Duplicates #4504

@bscheshirwork
Copy link
Contributor Author

@klimov-paul please, see changes in PR.

I read origin thread... So... Same situation year by year.

may be this is mark for review the opinion?

@bscheshirwork
Copy link
Contributor Author

bscheshirwork commented Sep 15, 2017

Data in grid not get from only one model, can be from many case, generated in cascade view, etc
Search model is representation of header. Same for header. (In default case)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants