2011-09-22 37 views
2

在某些僅記錄錯誤的特殊情況下,我想將模型中的錯誤數組或自定義錯誤字符串按順序傳遞給調用Controller通過電子郵件將這些數據發送給我。CakePHP如何將錯誤數據從模型傳遞迴控制器

我想過只是從模型本身發送一封電子郵件,但我讀過的地方,它憤怒的MVC最佳實踐的神。我環顧了CakePHP API並沒有發現任何看起來像我需要的東西,所以我在這裏問我是否錯過了任何東西。

編輯:我在beforeSave()方法中做了一些特殊的處理。

謝謝! 傑森

+1

CakePHP的2.0s的電子郵件類,可以從任何地方發送電子郵件 – Dunhamzzz

回答

3

哈哈,前進 - in CakePHP 2.0 - 電子郵件類將是一流的公民,而不是一個組成部分。因此,我不會擔心通過發送來自(god-forbid)模型或shell或其他有用的地方的電子郵件來激怒MVC神。

你必須通過一些箍雖然跳:

// we will need a controller, so lets make one: 
App::import('Core', 'Controller'); 
$controller =& new Controller(); 
// lets grab the email component 
App::import('Component', 'Email'); 
$email =& new EmailComponent(); 
// give it the reference to the controller 
$email->initialize($controller); 
// off we go... 
$email->from  = 'Name <[email protected]>'; 
$email->replyTo = '[email protected]'; 
$email->sendAs = $format; 
$email->to  = $destination; 
$email->subject = $subject; 
// oh, this is why we needed the controller 
$email->template = $template; 
$controller->set(compact('items', 'subject')); 
// done. 
$sent = $email->send(); 
相關問題