温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

怎么在Yii中对Command任务进行处理

发布时间:2021-02-11 14:20:05 来源:亿速云 阅读:187 作者:Leah 栏目:开发技术

怎么在Yii中对Command任务进行处理?相信很多没有经验的人对此束手无策,为此本文总结了问题出现的原因和解决方法,通过这篇文章希望你能解决这个问题。

1.配置,执行任务所需要的组件

任务配置文件:/protected/config/console.php

配置方法跟配置main文件差不多

<?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',
  // application components
  // 自动载入的模型和组件类
  'import'=>array(
      'application.models.*',//载入"application/models/"文件夹下的所有模型类
      'application.components.*',//载入"application/components/"文件夹下的所有应用组件类
      'application.extensions.*',//载入"application/extensions/"文件夹下的所有应用组件类
  ),
  'components'=>array(
      // uncomment the following to use a MySQL database
      'db'=>array(
          'connectionString' => 'mysql:host=localhost;dbname=dbname',//连接mysql数据库
          'emulatePrepare' => true,
          'username' => 'root',//MySQL数据库用户名
          'password' => '123456',//MySQL数据库用户密码
          'charset' => 'utf8',//MySQL数据库编码
          'tablePrefix' => 'zd_', //MySQL数据库表前缀
          'enableProfiling'=>true,
          'enableParamLogging'=>true,
      ),
      //加载Email组件
      'mailer' => array(
          'class'   => 'application.extensions.mailer.EMailer',
      ),
  ),
);

2.任务文件

放在 /protected/commands/ 文件目录下继承 CConsoleCommand 基类的为任务文件 命名方法为   任务名称+Command

例如 GoCommand.php

<?php
/**
 * 自动运行文件
 */
class GoCommand extends CConsoleCommand
{
  /**
   * 死循环输出
   */
  public function run(){
    for($i=1;$i>0;$i++){
      self::echoWord($i);
      sleep(2);//休眠2秒
      //跳出
      if(i==500){
        break;
      }
    }
  }
  /**
   * 输出hollo word
   */
  public function echoWord($i){
    echo "hollo word --$i\n";
  }
}

3.执行任务

打开命令行工具,进入项目的/protected 目录下 输入yiic命令即出现提示,提示列表显示刚才写的任务文件

E:\project\app\protected>yiic
Yii command runner (based on Yii v1.1.12)
Usage: E:\zeee\zyd\protected\yiic.php <command-name> [parameters...]
The following commands are available:
- go
- mailqueue
- message
- migrate
- shell
- webapp
To see individual command help, use the following:

看完上述内容,你们掌握怎么在Yii中对Command任务进行处理的方法了吗?如果还想学到更多技能或想了解更多相关内容,欢迎关注亿速云行业资讯频道,感谢各位的阅读!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI