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 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看