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

Allow specifying related data in a gridview data column #802

Closed
wants to merge 1 commit into from

Conversation

cebe
Copy link
Member

@cebe cebe commented Aug 23, 2013

DataColumn uses ArrayHelper::getValue() which is now able to get related
data via relation.attribute syntax.

This feature was available in yii 1.1. and should also be in 2.0 imo

TODO:

  • Documentation (will add docs when we agree to take this)

DataColumn uses ArrayHelper::getValue() which is now able to get related
data via `relation.attribute` syntax.

TODO:

 - [ ] Documentation
@coveralls
Copy link

Coverage Status

Coverage increased (+0.04%) when pulling 543ca7a on cebe:gridview-related-data into 92a1bef on yiisoft:master.

@ghost ghost assigned qiangxue Aug 23, 2013
@qiangxue
Copy link
Member

I actually wanted to avoid introducing this syntax because it will come with side effects and in most cases it can be easily implemented with explicit expression. For example for DataColumn, when you use this expression, you will face the problem with how to specify the header label and how to sort the column. So this new syntax doesn't really save you much.

@rawtaz
Copy link
Contributor

rawtaz commented Aug 23, 2013

A common solution to the related attribute label "problem" is to define labels in the current class. For example:

  • The related model Country's attribute 'name' has the label "Name" in that related class.
  • If we were to use that label in the "main" model's context, we'd get a very misleading label for the field or whatever, since "Name" seems to be about the current/main model rather than the Country one.
  • By adding an entry 'country.name'=>'Country' in the main model's attributeLabels(), this can be used whenever rendering a label or similar for the attribute 'country.name'.

So the label stuff should IMO be solved that way; Have Yii look for an attribute label for these "related notions" in the current class, and fall back on the one in the related class.

That said, there's definately the issue of related attributes not being sortable or filterable by default though, in Yii1. This is a super common thing that people ask how to do, and if we can solve it in Yii2 it wold be awesome.

@qiangxue
Copy link
Member

@rawtaz Your proposed label resolution algorithm is something I want to avoid because it is a bit magical, error prone and may confuse users.

Besides the sorting issue, there's also another tricky issue: filtering. So basically with the introduction of notation x.y.z, we are not really bringing much benefit (with anonymous functions, it's easy and clear to get related attribute value for DataColumn.)

@cebe
Copy link
Member Author

cebe commented Aug 24, 2013

Current solution for displaying and sorting:

echo \yii\grid\GridView::widget(array(
    'dataProvider' => new \yii\data\ActiveDataProvider(array(
        'query' => \app\models\Invoice::find()->join('LEFT OUTER JOIN', 'customer', 'customer.id = invoice.customerId'),
        'sort' => array(
            'attributes' => array(
                'nr',
                'customerId' => array(
                    'asc' => array('customer.name' => Sort::ASC),
                    'desc' => array('customer.name' => Sort::DESC),
                ),
// ...
            )
        )
    )),
    'columns' => array(
        array(
            'attribute' => 'nr',
            'value' => function($data) { return empty($data->nr) ? '#'.$data->id : $data->nr; },
            'format' => 'text',
        ),
        array(
            'attribute' => 'customerId',
            'value' => function($data) { return $data->customer->name; },
        ),
// ...
    ),
));

@samdark
Copy link
Member

samdark commented Oct 14, 2013

Any decision about this one?

@cebe
Copy link
Member Author

cebe commented Oct 14, 2013

Not yet, will try to work on that later.

@DrMabuse23
Copy link

Hi, Is there any news about Sorting relations ?

@qiangxue qiangxue closed this in 9e66e08 Dec 31, 2013
@cebe cebe deleted the gridview-related-data branch December 31, 2013 17:06
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

Successfully merging this pull request may close these issues.

6 participants