Allow specifying related data in a gridview data column #802

Closed
wants to merge 1 commit into
from

Conversation

Projects
None yet
6 participants
@cebe
Member

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)
Allow specifying related data in a gridview data column
DataColumn uses ArrayHelper::getValue() which is now able to get related
data via `relation.attribute` syntax.

TODO:

 - [ ] Documentation
@coveralls

This comment has been minimized.

Show comment
Hide comment
@coveralls

coveralls Aug 23, 2013

Coverage Status

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

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

This comment has been minimized.

Show comment
Hide comment
@qiangxue

qiangxue Aug 23, 2013

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.

Member

qiangxue commented Aug 23, 2013

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

This comment has been minimized.

Show comment
Hide comment
@rawtaz

rawtaz Aug 23, 2013

Contributor

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.

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

This comment has been minimized.

Show comment
Hide comment
@qiangxue

qiangxue Aug 23, 2013

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.)

Member

qiangxue commented Aug 23, 2013

@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

This comment has been minimized.

Show comment
Hide comment
@cebe

cebe Aug 24, 2013

Member

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; },
        ),
// ...
    ),
));
Member

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

This comment has been minimized.

Show comment
Hide comment
@samdark

samdark Oct 14, 2013

Member

Any decision about this one?

Member

samdark commented Oct 14, 2013

Any decision about this one?

@cebe

This comment has been minimized.

Show comment
Hide comment
@cebe

cebe Oct 14, 2013

Member

Not yet, will try to work on that later.

Member

cebe commented Oct 14, 2013

Not yet, will try to work on that later.

@DrMabuse23

This comment has been minimized.

Show comment
Hide comment
@DrMabuse23

DrMabuse23 Nov 26, 2013

Hi, Is there any news about Sorting relations ?

Hi, Is there any news about Sorting relations ?

@Ragazzo Ragazzo referenced this pull request Dec 30, 2013

Closed

added multi array feature #1702

@qiangxue qiangxue closed this in 9e66e08 Dec 31, 2013

@cebe cebe deleted the cebe:gridview-related-data branch Dec 31, 2013

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment