CakePHP如何将错误数据从模型传递回控制器

CakePHP如何将错误数据从模型传递回控制器,php,model-view-controller,cakephp,Php,Model View Controller,Cakephp,在一些仅记录错误还不够的特殊情况下,我希望将错误数组或自定义错误字符串从模型传递到调用控制器,以便在电子邮件中向我发送该数据 我曾想过只发送一封来自模型本身的电子邮件,但我在某处读到它激怒了MVC最佳实践。我查看了CakePHP API,没有找到任何与我需要的内容类似的内容,所以我在这里询问是否遗漏了任何内容 编辑:我正在beforeSave()方法中进行一些特殊处理 谢谢! 杰森哈哈,向前看——电子邮件类将是头等公民,而不是组件 因此,我不会担心从(上帝禁止的)模型或外壳或其他有用的地方发送电

在一些仅记录错误还不够的特殊情况下,我希望将错误数组或自定义错误字符串从模型传递到调用控制器,以便在电子邮件中向我发送该数据

我曾想过只发送一封来自模型本身的电子邮件,但我在某处读到它激怒了MVC最佳实践。我查看了CakePHP API,没有找到任何与我需要的内容类似的内容,所以我在这里询问是否遗漏了任何内容

编辑:我正在beforeSave()方法中进行一些特殊处理

谢谢! 杰森

哈哈,向前看——电子邮件类将是头等公民,而不是组件

因此,我不会担心从(上帝禁止的)模型或外壳或其他有用的地方发送电子邮件会激怒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 <noreply@example.com>';
$email->replyTo  = 'noreply@example.com';
$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();
//我们需要一个控制器,所以让我们制作一个:
应用程序::导入('Core','Controller');
$controller=&new controller();
//让我们抓取电子邮件组件
应用程序::导入(“组件”、“电子邮件”);
$email=&newemailcomponent();
//将其作为控制器的参考
$email->initialize($controller);
//我们走吧。。。
$email->from='Name';
$email->replyTo=noreply@example.com';
$email->sendAs=$format;
$email->to=$destination;
$email->subject=$subject;
//这就是为什么我们需要控制器
$email->template=$template;
$controller->set(压缩('items','subject');
//完成了。
$sent=$email->send();

CakePHP 2.0s电子邮件类允许您从任何地方发送电子邮件