yii consolecommand 控制台命令,实现定时任务。当然,这得结合系统,如XP的计划任务,linux的crontab命令。
1,配置好,要执行的页面。本文为 protected/commands/crons.php
12345678910<?php
defined(
'YII_DEBUG'
)
or
define(
'YII_DEBUG'
,true);
// including Yii
require_once
(dirname(dirname(dirname(
__FILE__
))).
'/framework/yii.php'
);
// we'll use a separate config file
$configFile
=dirname(dirname(
__FILE__
)).
'/config/console.php'
;
// creating and running console application
Yii::createConsoleApplication(
$configFile
)->run();
?>
2,配置好product/config/console.php里面需要用到的组件,像数据库连接,其他类包。
123456789101112131415161718192021222324252627282930313233343536<?php
// This is the configuration for yiic console application.
// Any writable CConsoleApplication properties can be configured here.
return
array
(
'basePath'
=> dirname (
__FILE__
) . DIRECTORY_SEPARATOR .
'..'
,
'name'
=>
'My Console Application'
,
'import'
=>
array
(
'application.models.*'
,
'application.components.*'
,
'application.components.base.*'
,
'application.components.imgthumb.*'
,
'application.models.form.*'
,
'等等,导入所要的类包'
),
'components'
=>
array
(
// Main DB connection
'db'
=>
array
(
'connectionString'
=>
'mysql:host=localhost;dbname=数据库名称'
,
'emulatePrepare'
=> true,
'username'
=>
'数据库名称'
,
'password'
=>
'数据库密码'
,
'charset'
=>
'utf8'
,
'tablePrefix'
=>
'company_'
、
//表前缀
),
'log'
=>
array
(
'class'
=>
'CLogRouter'
,
'routes'
=>
array
(
array
(
'class'
=>
'CFileLogRoute'
,
'levels'
=>
'error, warning'
)
)
)
)
);
3,建立继承CConsoleCommand的类,在commands目录下创建一个文件,执行任务,命名为TestCommand.php
1234567891011<?php
/**
* 自动化执行 命令行模式
*/
class
TestCommand
extends
CConsoleCommand
{
public
function
run(
$args
) {
//所要执行的任务,如数据符合某条件更新,删除,修改
}
}
4,打开你的linux命令窗口,创建自动任务。至于windows系统 ,是计划任务(win系统,可以谷歌如何操作),下面只讲linux系统。
12345crontab -e
##然后输入
1 * * * * php /具体地址/
protected
/commands/crons.php Test >>/具体地址/
protected
/commands/test.log
##上面命令说明,每分钟执行Test任务一次,把日志保存在test.log下
至此,自动化任务已完成。
如需转载请注明: 转载自26点的博客
本文链接地址: Yii框架的计划任务实现 <转载留存>
转载请注明:26点的博客 » Yii框架的计划任务实现 <转载留存>
希望大家踊跃发言,我顶先
还没有机会尝试一下 帮顶