arogachev / yii2-excel

ActiveRecord import and export based on PHPExcel for Yii 2 framework
Other
65 stars 25 forks source link

Update StandardModel.php #9

Closed psfpro closed 9 years ago

psfpro commented 9 years ago

custom labels for columns For exemples:


$exporter = new Exporter([
            'query' => Device::find(),
            'filePath' => Yii::getAlias('@app/StatsExport.xlsx'),
            'sheetTitle' => 'Tests',
            'standardModelsConfig' => [
                [
                    'className' => Device::className(),
                    'extendStandardAttributes' => false,
                    'standardAttributesConfig' => [
                        [
                            'name' => 'user_id',
                            'valueReplacement' => function ($model) {
                                /* @var $model Device */
                                return $model->user->name;
                            },
                            'label' => 'Name',
                        ],
                        [
                            'name' => 'user_id',
                            'valueReplacement' => function ($model) {
                                /* @var $model Device */
                                return $model->user->surname;
                            },
                            'label' => 'Surname',
                        ],
                        [
                            'name' => 'user_id',
                            'valueReplacement' => function ($model) {
                                /* @var $model Device */
                                return $model->user->email;
                            },
                            'label' => 'Email',
                        ],
                        [
                            'name' => 'identifier',
                            'valueReplacement' => function ($model) {
                                /* @var $model Device */
                                return $model->identifier;
                            },
                            'label' => 'Device ID',
                        ],
                        [
                            'name' => 'juice_level',
                            'valueReplacement' => function ($model) {
                                /* @var $model Device */
                                return $model->juice_level;
                            },
                            'label' => 'Juice level',
                        ],
                        [
                            'name' => 'updated',
                            'valueReplacement' => function ($model) {
                                /* @var $model Device */
                                return $model->updated;
                            },
                            'label' => 'Updated',
                        ],
                    ],
                ],
            ],
        ]);
        $exporter->run();
arogachev commented 9 years ago

What do you mean by that? Standard attributes list should be unique regardless of extendStandardAttributes option.

psfpro commented 9 years ago

When I make a custom field, I have no unique attributes. For exemple attribute: 'name' => 'user_id'.

                       [
                            'name' => 'user_id',
                            'valueReplacement' => function ($model) {
                                /* @var $model Device */
                                return $model->user->name;
                            },
                            'label' => 'Name',
                        ],
                        [
                            'name' => 'user_id',
                            'valueReplacement' => function ($model) {
                                /* @var $model Device */
                                return $model->user->surname;
                            },
                            'label' => 'Surname',
                        ],

What do you think about this? What other configuration options?

arogachev commented 9 years ago

To be honest, I was in hurry when writing exporter. I didn't even update the README with basic example of usage.

The main problem is that used StandardModel in export is from import (see #6). It's better to use separate extended StandardModel for export, it will solve this problem.

Your solution will also touch import.

If you need this urgently, you can use fork.

I will try to fix it as soon as possible.

arogachev commented 9 years ago

Fixed in b84f2eb. Use this instead:

[
    'name' => 'user_name',
    'label' => 'Name',
    'valueReplacement' => function ($model) {
        /* @var $model Device */
        return $model->user->name;
    },

],
[
    'name' => 'user_surname',
    'label' => 'Surname',
    'valueReplacement' => function ($model) {
        /* @var $model Device */
        return $model->user->surname;
    },
],
arogachev commented 9 years ago

Will update README soon.