2012-10-03 22 views
0

我有一個很大的問題。好吧,我正在嘗試保存在我的Web應用程序中發生的錯誤。例如(只是一個例子),如果我用零來劃分,或者在查詢或類似事件中發生錯誤。所以,我想抓住它並將它保存在數據庫中。但CakePHP的handleError是靜態的,所以我不能使用$ this-> MyModel-> saveError(...)。如何在CakePHP 2.0中用handleError保存錯誤?

在我/app/Lib/AppError.php我有這樣的:

class AppError extends ErrorHandler 
{ 
    public $uses = array('Errors.Error'); 

    public static function handleError($code, $description, $file = null, $line = null, $context = null) 
    { 
     //This print the error: 
     echo "Code: ".$code." Description: ".$description." File: ".$file." Line: ".$line."<br>"; 
     //I want to do this, (save to the database): 
     $this->Error->saveError($code, $description, $file, $line); 

    } 
} 

沒有$這個 - >錯誤 - > saveError($代碼,$描述,$文件,$線);它的工作原理,但我不僅要顯示錯誤。 我認爲需要一個例子或類似的東西。請幫幫我。 問候並感謝你。 謝謝... PD:對不起,我的英語,我是英語的學生......

+1

那麼屬性'Error'在哪裏? :) ..並且你爲英語學生而感到難過嗎?嗯好的;) – dbf

+0

那麼,'錯誤'是我的模型。這應該保存在表中的錯誤,但問題是,蛋糕說:當不在對象上下文中使用$ this File:/var/www/industrialProcess/app/Lib/AppError.php – jags1988cr

回答

0

你作爲一個靜態函數定義的函數handelError,爲此變量$this不存在,因爲靜態環境無法知道它引用的對象,只有它的類名。在靜態環境中,必須使用self::而不是$this,例如,

self::Error->saveError($code, $description, $file, $line);

這也意味着,物業Error都將被聲明static