Silverstripe GridField中具有相同ID的多行

时间:2013-09-30 12:56:18

标签: silverstripe

有没有办法对GridField进行分组(例如,通过ID)?如果它们没有相同的SortOrder,则以下Query(many_many)会返回一些数据对象两次或更多次。这是一些代码。我希望有人可以提供帮助; - )

private static $many_many = array(
    'Mitarbeiter'         => 'Mitarbeiter',
);

private static $many_many_extraFields = array(
    'Mitarbeiter' => array(
        'SortOrder' => "Int"
    )
)

        // Start Mitarbeiterzuordnung
        $MitarbeiterFieldConfig = GridFieldConfig::create()->addComponents(
          new GridFieldToolbarHeader(),
          //new GridFieldSortableHeader(),
          new GridFieldDataColumns(),
          new GridFieldDeleteAction('unlinkrelation'),
          new GridFieldSortableRows('SortOrder'),
          new GridFieldManyRelationHandler(), 'GridFieldPaginator',
          new GridFieldPaginator(20)
        );
        $MitarbeiterField = new GridField("Mitarbeiter", "Mitarbeiter", $this->Mitarbeiter()->sort('SortOrder'), $MitarbeiterFieldConfig);
        $fields->addFieldToTab('Root.Mitarbeiter', $MitarbeiterField);   

1 个答案:

答案 0 :(得分:0)

GridFieldManyRelationHandler.php存在问题,并且private static $many_many_extraFields作为排序字段。

查询运行时SortOrder不同,因为它使用distinct,具有不同值的查询被视为额外的,这就是具有订单集的那些显示两次的原因。

您需要创建GridFieldManyRelationHandler.php的副本(我称之为GridFieldManyManyRelationHandler.php)并编辑以下函数:

    public function getManipulatedData(GridField $gridField, SS_List $list) {
    if(!$list instanceof RelationList) {
        user_error('GridFieldManyRelationHandler requires the GridField to have a RelationList. Got a ' . get_class($list) . ' instead.', E_USER_WARNING);
    }

    $state = $this->getState($gridField);

    // We don't use setupState() as we need the list
    if($state->FirstTime) {
        $state->RelationVal = array_values($list->getIdList()) ?: array();
    }
    if(!$state->ShowingRelation && $this->useToggle) {
        return $list;
    }

    $baseClass = $list->dataClass();
    return $baseClass::get();


}

然后从您的代码中调用该新类,即

private static $many_many = array(
    'Mitarbeiter'         => 'Mitarbeiter',
);

private static $many_many_extraFields = array(
    'Mitarbeiter' => array(
        'SortOrder' => "Int"
    )
)

        // Start Mitarbeiterzuordnung
        $MitarbeiterFieldConfig = GridFieldConfig::create()->addComponents(
          new GridFieldToolbarHeader(),
          //new GridFieldSortableHeader(),
          new GridFieldDataColumns(),
          new GridFieldDeleteAction('unlinkrelation'),
          new GridFieldSortableRows('SortOrder'),
          new GridFieldManyManyRelationHandler(), 'GridFieldPaginator',
          new GridFieldPaginator(20)
        );
        $MitarbeiterField = new GridField("Mitarbeiter", "Mitarbeiter", $this->Mitarbeiter()->sort('SortOrder'), $MitarbeiterFieldConfig);
        $fields->addFieldToTab('Root.Mitarbeiter', $MitarbeiterField);

这可能应该重构,两个类应该分开(一个类处理many_many,另一个类has_many)。