我正在尝试从Cake
PHP shell发送一封电子邮件,就像从Controller那样.
下面的代码大部分是从this dated article on the Bakery改编而来的.电子邮件正在发送,但是$controller-> set(‘result’,$results [$i]);引发以下通知:
Notice: Undefined property:
View::$webroot in
/home/jmccreary/www/intranet.sazerac.com/cakePHP/cake/libs/view/view.PHP
on line 813PHP Notice: Undefined
variable: result in
/home/jmccreary/www/intranet.sazerac.com/cakePHP/app/views/elements/email/text/nea/task_reminder_it.ctp
on line 2
所以我没有得到传递给我的电子邮件视图的任何变量.
我该怎么做,理想地遵循蛋糕惯例?
class NotificationShell extends Shell { var $uses = array('Employee','Task'); function main() { // run if no action is passed } function nea_task_reminder() { // build Task to Employee relationship $this->Task->bindModel(array('belongsTo' => array('Employee' => array('className' => 'Employee','foreignKey' => 'object_id')))); $results = $this->Task->find('all',array('conditions' => array('application_id' => 1,'completed_by_id' => 0),'contain' => array('Employee' => array('Contact','Position')))); $count = count($results); if ($count) { App::import('Core','Controller'); App::import('Component','Email'); $controller =& new Controller(); $email =& new EmailComponent(); $email->startup($controller); // send email $email->from = Configure::read('Email.from'); $email->to = 'jmccreary@whatever.com'; $email->replyTo = 'no-reply@whatever.com'; $email->template = 'nea/task_reminder_it'; $email->sendAs = 'text'; for ($i = 0; $i < $count; ++$i) { $email->subject = 'NEA Notification: Task Reminder for ' . $results[$i]['Employee']['Contact']['full_name']; $controller->set('result',$results[$i]); $email->send(); } } } }
问题是初始化EmailComponent类的方式.如果你看源代码,startup()方法实际上并不具有一个主体,所以它什么都不做.您的控制器实际上并未分配给EmailComponent.问题不是$controller-> set(‘results’,…);.您需要使用EmailComponent :: initialize()而不是EmailComponent :: startup().
$controller =& new Controller(); $email =& new EmailComponent(null); $email->initialize($controller);
资料来源:
> http://bakery.cakephp.org/articles/Jippi/2007/12/02/emailcomponent-in-a-cake-shell的评论
> EmailComponent::startup() Source