Вопрос

I need to export the whole data of an dataobject. Database fields and relations.

private static $db = array (
    'URLSegment' => 'Varchar(255)',
    'SKU' => 'Text',
    'Title' => 'Text',
    'Price' => 'Text',
    'Content' => 'HTMLText',
    'ItemConfig' => 'Int',
    'Priority' => 'Int'
);

private static $has_one = array (
    'Visual' => 'Image'
);

private static $has_many = array (
    'Sizes' => 'ShopItem_Size',
    'Colors' => 'ShopItem_Color'
);

private static $many_many = array (
    'Visuals' => 'Image',
    'Categories' => 'ShopCategory'
);

I added everything to getExportFields(). But as expected the result for the relations is "ManyManyList" or "HasManyList"

public function getExportFields() {
    return array(
        'SKU' => 'SKU',
        'Title' => 'Title',
        'Price' => 'Price',
        'Content' => 'Content',
        'ItemConfig' => 'ItemConfig',
        'Visual' => 'Visual',
        'Visuals' => 'Visuals',
        'Sizes' => 'Sizes',
        'Colors' => 'Colors',
        'Categories' => 'Categories'
    );
}

Is it possible to create such an export?

Thank you in advance

Это было полезно?

Решение

You can use any method name instead of a field/relation name in the exportFields array.

In your ModelAdmin class

public function getExportFields() {
    return array(
        'SKU' => 'SKU',
        'Title' => 'Title',
        'CategoryNames' => 'Categories'
    );
}

and just have a method with that name on the DataObject, returning the relation data as a string:

public function CategoryNames(){

    $catNames = array();
    foreach($this->Categories() as $cat){
        $catNames[] = $cat->getField('Title');
    }

    //use a separator that won't break the CSV file
    return join("; ", $catNames);

}

I think this is even way better than the modeladmin creating magic fields in your CSV file,
that would make it inconsistent...

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top