我已经编写了一个我想通过cron作业使用的模型和控制器方法,但是在调查中看来我必须将它作为一个没有Console / Command的shell。我已经阅读了一些不同的SO帖子和手册,但这一切都在我脑海中。
这就是我到目前为止所做的 - 几乎与我在模型和控制器中的内容相差无几。有人可以给我一些关于如何使其发挥作用的指示吗?提前谢谢。
****编辑 - 这现在有效****
<?php
App::uses('Shell', 'Console');
App::uses('CakeEmail', 'Network/Email');
class OldClaimsShell extends AppShell {
// Include the ExpesesCLaim mdel
public $uses = array('ExpenseClaim');
/**
* When fired it checks for claims over 7 days that are still "submitted" and have therefore not been dealt with. Function has to be called MAIN - see cake docs
*/
public function main() {
// Find all claims with status "submitted" that are older than 7 days
$answer = $this->ExpenseClaim->haveClaimsOlderThan7Days();
//debug($answer);
if ($answer === true) {
$Email = new CakeEmail();
$Email->domain('www.xxx.co.uk');
$Email->template('oldExpenseClaimReminder');
$Email->to('xxx@xxx.co.uk');
$Email->from('xx@xx.co.uk');
$Email->subject('There are outstanding claims over 7 days old - Please Review');
$Email->viewVars(array('link' => 'http://xxx.xxx.co.uk'));
$Email->emailFormat('both');
$Email->send();
//$this->out('Email sent');
}
}
}
?>
基于答案和评论的修正问题 - 尽管Cron没有这个问题现在有效。将在另一个问题中探讨这一点。
答案 0 :(得分:1)
在2.x中你应该不 use app::import()
你应该不在shell中使用控制器
最新的2.x分支具有CakeEmail类,用于来自shell,模型,控制器等的电子邮件(例如:您喜欢的任何地方)EmailComponent现在是一个保持向后兼容性的包装器。
就代码而言,shell类似于控制器。您可以像在控制器中一样指定要使用的模型,并且获取数据完全相同。
检查发送电子邮件的文档,非常简单......
$Email = new CakeEmail();
// set config, add users, etc
$Email->send();
然后用普通的
在cron中运行它0 * * * * /path/to/app/Console/cake FooBar --params... etc