yii框架计划任务

本文详细介绍了如何在Yii框架中配置和执行Cron任务,包括cron.php和console.php文件的设置,以及TestCommand类的具体实现。通过示例展示了数据库连接配置、日志记录方式,以及使用CConsoleCommand基类创建自定义命令行操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

http://happymc.iteye.com/blog/2149924

1.cron.php
<?php

// change the following paths if necessary
$yiic=dirname(__FILE__).'/../../framework/yiic.php';
$config = dirname ( __FILE__ ) . '/../config/console.php';

require_once ($yiic);

2.console.php

<?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.*',

    ),
    'components' => array (
        // Main DB connection
        'db'=>array(
            'connectionString' => 'mysql:host=39.107.44.226:3306;dbname=ddj_app',
            'schemaCachingDuration' => 3600,
            'emulatePrepare' => true,
            'enableParamLogging' => true,
            'username' => 'root',
            'password' => 'DB@KPL$88!2018',
            'charset' => 'utf8',
            'tablePrefix' => 'fst_',

        ),
        'log' => array (
            'class' => 'CLogRouter',
            'routes' => array (
                array (
                    'class' => 'CFileLogRoute',
                    'levels' => 'error, warning'
                )
            )
        )
    )
);

 

3.TestCommand.php

<?php
/**
 * 自动化执行 命令行模式
 */
class TestCommand extends CConsoleCommand
{
    public function run($args)
    {

        $data = array('username' => 'ezreal', 'password' => date("Y-m-d H:i:s"));
        try {
            return Yii::app()->db->createCommand()->insert('gzc_user', $data);
        } catch (Exception $ex) {
            return null;
        }
    }

    public function test2()
    {
        echo 'this is a test';
    }

}

4. php crons.php test test2  运行

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值