0

有没有办法对 GridField 进行分组(例如,按 ID)?如果它们没有相同的 SortOrder,则以下查询 (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);   
4

1 回答 1

0

GridFieldManyRelationHandler.php 存在问题,并且具有private static $many_many_extraFieldsas 排序字段。

查询在 SortOrder 不同的情况下运行,并且因为它使用 distinct,具有不同值的查询被视为额外的,这就是具有 order 集的查询显示两次的原因。

您需要创建一份 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)。

于 2015-02-02T22:24:41.057 回答