http://www.thinkphp.cn/topic/61661.html
1. 下载thinkphp5
http://www.thinkphp.cn/donate/download/id/1278.html
覆盖vendor目录和extend目录
2. 在项目根目录创建文件artisan (把think文件改名)
#!/usr/bin/env php
2. application/command.php
注册命令行文件
指定命令行类的路径
application/oa/command/Sample.php
application/oa/command/Approval.php
3. 创建最基本的命令行文件
application/oa/command/Sample.php
setName('sample')->setDescription("This is a remark");
}
protected function execute(Input $input, Output $output) {
$output->writeln("TestCommand:");
}
}
$ php artisan sample
TestCommand:
4. 再创建一个链接数据库的命令行
setName('approval')->setDescription("schedule task");
}
/**
* @param Input $input An InputInterface instance
* @param Output $output An OutputInterface instance
*/
protected function initialize(Input $input, Output $output) {
// 读取数据库配置文件
$filename = ROOT_PATH . 'config/database.php';
// echo $filename.PHP_EOL; die;
Config::load($filename, 'database');
}
protected function execute(Input $input, Output $output) {
Logger::write(sprintf("Running approval task at %s", date("Y-m-d H:i:s")));
/** @var $g \Generator */
$g = Examine::generator();
while ($g->valid()) {
/** @var $examine Examine */
$examine = $g->current();
printf("%d -- %s\n", $examine->getId(), $examine->content);
$g->next();
}
}
}
运行命令:
$ php artisan approval
3 -- 出去玩
7 -- 借款作为备用金
8 -- 戴玉普通审批
9 -- 找公司借5000块钱备用金
10 -- 报备学校不需要报备金
11 -- 报备学校
13 -- 好机会今年就
15 -- 111
17 -- 报备审批 新增学校
18 -- {"unit":"Beijing EasyTech","state":0}
22 -- {"unit":"\u6d4b\u8bd5\u62a5\u5907\u4eba\u5355\u4f4d","state":0}
26 -- {"unit":"\u7b80\u5355\u79d1\u6280","school":"\u5b89\u5e86\u5e02\u5927\u89c2\u533a\u6c11\u529e\u540c\u5b89\u666e\u901a\u9ad8\u7ea7\u4e2d\u5b66","state":0}
28 -- {"unit":"\u4e92\u7ad9\u7f51","school":"\u5b89\u5e86\u5e02\u5927\u89c2\u533a\u6c11\u529e\u540c\u5b89\u666e\u901a\u9ad8\u7ea7\u4e2d\u5b66","state":0}
43 -- {"examine_id":"42","school":"\u6881\u5e73\u53bf\u804c\u4e1a\u6559\u80b2\u4e2d\u5fc3","state":0}
关键:
在命令行中添加初始化方法:
protected function initialize(Input $input, Output $output) {
// 读取数据库配置文件
$filename = ROOT_PATH . 'config/database.php';
// echo $filename.PHP_EOL; die;
Config::load($filename, 'database');
}
如果没有这个方法
[PDOException]
SQLSTATE[HY000] [2002] Connection refused
打印出来的Connection配置
["config":protected]=>
array(25) {
["type"]=>
string(5) "mysql"
["hostname"]=>
string(9) "127.0.0.1"
["database"]=>
string(0) ""
["username"]=>
string(4) "root"
["password"]=>
string(0) ""
["hostport"]=>
string(0) ""
["dsn"]=>
string(0) ""
["params"]=>
array(0) {
}
明显是默认的配置, 说明数据库配置没有加载上
模型文件定义的生成器方法:
/**
* 返回生成器用于遍历所有审批
*/
public static function generator() {
$a = Examine::where('check_status', '=', 0)
->whereOr('check_status', '=', 1)
->select();
foreach ($a as $e) {
yield $e;
}
}
查看sql:
ubuntu@et-dev-mingzhanghui:/usr/ET/project/zktCrm/runtime/log/201910$ tail -f 24_cli.log
SELECT * FROM `5kcrm_oa_examine` WHERE `check_status` = 0 OR `check_status` = 1
$o = new self();
$c = $o->getConnection();
var_dump($c); die;
simple logger:
application/oa/util/Logger.php
self::MAX_LOG_SIZE) {
copy($logPath, $logDir.'/jobs.archive.log');
ftruncate(self::$handler, 0);
rewind(self::$handler);
}
}
public static function close() {
if (is_resource(self::$handler)) {
fclose(self::$handler);
self::$handler = null;
}
}
}