SilverStripe网格域多类编辑器具有无效的sort()列

时间:2015-10-12 07:49:24

标签: silverstripe

我无法设置嵌套has_many关系的SortOrder,其中子项可以通过Orderable RowsGridfield Extensions模块进行拖放重新排序。我也在使用Multi Class Editor

我的Page有很多ComplexStrips。这些ComplexStrip个对象有很多' LinkedObjects'。

问题:

当我尝试通过GridField添加ComplexStrip时,我收到以下错误: Uncaught InvalidArgumentException:无效的sort()列

我的设置:

Page Class: 当我浏览在CMS中编辑它时,这个类工作正常。

<?php
class Page extends SiteTree {

    private static $has_many = array(
        'ComplexStrips' => 'ComplexStrip'
    );

    public function getCMSFields() {

        $fields = parent::getCMSFields();

        $dataColumns = new GridFieldDataColumns();
        $dataColumns->setDisplayFields(
            array(
                'Title'     => 'Title',
                'ClassName' => 'Class Name'
            )
        );

        $multiClassConfig = new GridFieldAddNewMultiClass();
        $multiClassConfig->setClasses(
            array(
                'ComplexStrip'
            )
        );

        $stripsGridField = GridField::create('ComplexStrips', 'Complex strips', $this->Strips(),
            GridFieldConfig::create()->addComponents(
                new GridFieldToolbarHeader(),
                new GridFieldDetailForm(),
                new GridFieldEditButton(),
                new GridFieldDeleteAction('unlinkrelation'),
                new GridFieldDeleteAction(),
                new GridFieldOrderableRows('SortOrder'), // This line causes the issue
                new GridFieldTitleHeader(),
                $multiClassConfig,
                $dataColumns
            )
        );

        $fields->addFieldsToTab(
            'Root.Strips',
            array(
                $stripsGridField
            )
        );

        return $fields;
    }
}

TextBlocksStrip类: 一旦我尝试通过Page上的gridfield添加其中一个,我就会收到错误: Uncaught InvalidArgumentException:无效的sort()列

<?php
class ComplexStrip extends DataObject {

    private static $db = array(
        'SortOrder' => 'Int'
    );

    private static $has_many = array(
        'TextBlocks' => 'TextBlock'
    );

//-------------------------------------------- CMS Fields

    public function getCMSFields() {

        $fields = parent::getCMSFields();

//---------------------- Main Tab

        $textBlocks = GridField::create('TextBlocks', 'Text blocks', $this->TextBlocks(), 
            GridFieldConfig::create()->addComponents(
                new GridFieldToolbarHeader(),
                new GridFieldAddNewButton('toolbar-header-right'),
                new GridFieldDetailForm(),
                new GridFieldDataColumns(),
                new GridFieldEditButton(),
                new GridFieldDeleteAction('unlinkrelation'),
                new GridFieldDeleteAction(),
                new GridFieldOrderableRows('SortOrder'),
                new GridFieldTitleHeader(),
                new GridFieldAddExistingAutocompleter('before', array('Title'))
            )
        );

        $fields->addFieldsToTab('Root.Main',
            array(
                $textBlocks
            )
        );

        return $fields;
    }
}

TextBlock类:

<?php
class TextBlock extends DataObject {

    private static $db = array(
        'SortOrder' => 'Int'
    );

    private static $default_sort = 'Sort';

    private static $has_one = array(
        'TextBlocksStrip' => 'TextBlocksStrip'
    );
}

如果我删除行new GridFieldOrderableRows('SortOrder')所有作品但显然无法重新排序。

我把头发拉过这一头。对我来说没有任何意义,为什么这不起作用。

2 个答案:

答案 0 :(得分:3)

我做了一个相当优雅的解决方法。

无法查询SortOrder,因为在尝试加入已排序表时父对象ComplexStrip不存在。因此,将GridFieldOrderableRows包装在if中,以检查父对象是否已分配了ID。

// Need to wait until parent object is created before trying to set sort order
if($this->ID) {
    $textBlocks->addComponents(
        new GridFieldOrderableRows('Sort')
    );
}

之后,一切都与以前在CMS中的工作方式相同。我现在将在模块上打开一个问题,看看是否有更持久的解决方案。

答案 1 :(得分:0)

我遇到与使用$many_many而非$has_many相同的错误。我可以看到您使用了$ has_many,因此无法解决您的问题,但可能会帮助其他人遇到与我相同的错误消息。