2015-05-05 49 views
2

作爲一項練習,以幫助我瞭解如何編寫自定義報告,我寫了一個非常簡單的自定義報告按頁面類型列出頁面。我寫了基於標準的報告CMS /代碼/報告的代碼/ BrokenLinksReport.php(包括作爲CMS的一部分),但我得到一個錯誤:爲什麼我的自定義SS3.1報告返回錯誤「[注意]類GridState_Data的對象無法轉換爲int」

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

我甩的$的數據,以確保它是內容如預期的那樣,事實如此。什麼可能導致這個問題?

我的代碼如下:

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') 
      ) 
     ); 
    } 
} 

回答

3

有誤差在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') 
     ); 
    } 
} 
+0

謝謝!有興趣知道原因。與此同時,你的代碼是一種享受! :-) –

+0

我已經用我發現的內容更新了我的答案。我希望這個問題是有道理的。 – 3dgoo

相關問題