0

我已经编写了一个模型和控制器方法,我想通过 cron 作业使用它,但是经过调查,我似乎必须将其作为带有控制台/命令的 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 没有,但现在可以使用。将在另一个问题中探讨这一点。

4

1 回答 1

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
于 2013-06-21T23:57:47.220 回答