为什么我的自定义 SS3.1 报告返回错误“[Notice] class GridState_Data 的对象无法转换为 int”

Why is my custom SS3.1 report returning an error "[Notice] Object of class GridState_Data could not be converted to int"

作为帮助我学习编写自定义报告的练习,我编写了一个非常简单的自定义报告来按页面类型列出页面。我根据标准报告 cms/code/reports/BrokenLinksReport.php(作为 CMS 的一部分包含在内)编写了代码,但出现错误:

[Notice] Object of class GridState_Data could not be converted to int

我转储了 $data 的内容以确保它符合预期,确实如此。可能是什么导致了这个问题?

我的代码如下:

class PageListByType extends SS_Report {

    function title() {
        return "Page List by Type";
    }

    function description() {
        return "List all the pages in the site, along with their page type";
    }

    public function sourceRecords($params = array(), $sort = null, $limit = null) {
        $data = Page::get()->sort($sort);
        $returnSet = new ArrayList();
        if ($data) foreach ($data as $record) {
            $returnSet->push($record);
        }
        return $returnSet;
    }

    public function columns() {
        return array(
            array(
                'title'=>_t('PageListByTypeReport.PageName', 'Page name')
            ),
            array(
                'className'=>_t('PageListByTypeReport.ClassName', 'Page type')
            )
        );
    }
}

错误是columns函数中的二维数组设置不正确。缺少变量名称,并且每列的变量名称不正确。

您也可以这样设置列:

public function columns() {
    return array(
        'Title' => array(
            'title'=>_t('PageListByTypeReport.PageName', 'Page name')
        ),
       'ClassName' =>  array(
            'title'=>_t('PageListByTypeReport.ClassName', 'Page type')
        )
    );
}

或者像这样更简单:

public function columns() {
    return array(
        'Title' => _t('PageListByTypeReport.PageName', 'Page name'),
        'ClassName' => _t('PageListByTypeReport.ClassName', 'Page type')
    );
}

当前的 sourceRecords 函数可以工作,尽管我们可以通过像这样返回 Page::get() 的结果来简化这件事:

public function sourceRecords($params = array(), $sort = null, $limit = null) {
    $pages = Page::get()->sort($sort);
    return $pages;
}

这是报告代码的工作和简化版本:

class PageListByType extends SS_Report {

    function title() {
        return 'Page List by Type';
    }

    function description() {
        return 'List all the pages in the site, along with their page type';
    }

    public function sourceRecords($params = array(), $sort = null, $limit = null) {
        $pages = Page::get()->sort($sort);
        return $pages;
    }

    public function columns() {
        return array(
            'Title' => _t('PageListByTypeReport.PageName', 'Page name'),
            'ClassName' => _t('PageListByTypeReport.ClassName', 'Page type')
        );
    }
}