dongpao1918 2014-05-15 21:12
浏览 34
已采纳

Silverstripe 3.1 - 与所有关系导出Dataobject?

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

  • 写回答

1条回答 默认 最新

  • dousigan0499 2014-05-16 17:27
    关注

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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 Pwm双极模式H桥驱动控制电机
  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题