thinkphp5.0命令行下无法链接数据库
http://www.thinkphp.cn/topic/61661.html1. 下载thinkphp5http://www.thinkphp.cn/donate/download/id/1278.html覆盖vendor目录和extend目录2. 在项目根目录创建文件artisan (把think文件改名)#!/usr/bin/env php<?php/...
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
<?php
// 应用目录
define('APP_PATH', __DIR__.'/application/');
// 定义缓存目录
define('RUNTIME_PATH', __DIR__.'/runtime/');
// 加载框架引导文件
require dirname(__FILE__).'/thinkphp/console.php';
2. application/command.php
注册命令行文件
<?php
return [
'app\oa\command\Sample',
'app\oa\command\Approval',
];
指定命令行类的路径
application/oa/command/Sample.php
application/oa/command/Approval.php
3. 创建最基本的命令行文件
application/oa/command/Sample.php
<?php
namespace app\oa\command;
use think\console\Input;
use think\console\Output;
/**
* php think sample
* Class Sample
* @package app\oa\command
*/
class Sample extends \think\console\Command
{
protected function configure() {
parent::configure(); // TODO: Change the autogenerated stub
$this->setName('sample')->setDescription("This is a remark");
}
protected function execute(Input $input, Output $output) {
$output->writeln("TestCommand:");
}
}
$ php artisan sample
TestCommand:
4. 再创建一个链接数据库的命令行
<?php
/**
* Created by PhpStorm.
* User: mingzhanghui
* Date: 10/24/2019
* Time: 14:48
*/
namespace app\oa\command;
use app\oa\model\Examine;
use think\Config;
use think\console\Command;
use think\console\Input;
use think\console\Output;
use app\oa\util\Logger;
/**
* php artisan approval
* @ref: application/command.php
* Class ApprovalCommand
* @package app\oa\command
*/
class Approval extends Command
{
protected $connection = null;
public function configure() {
$this->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
<?php
namespace app\oa\util;
class Logger
{
const MAX_LOG_SIZE = 1048576;
/** @var resource */
protected static $handler = null;
public static function write($msg) {
self::init();
// datetime
$msg = sprintf("[%s] %s\n", date("Y-m-d H:i:s", time()), $msg);
fwrite(self::$handler, $msg, strlen($msg));
}
private static function init() {
$logDir = RUNTIME_PATH.'log';
if (!is_dir($logDir)) {
mkdir($logDir, 0777, true);
}
$logPath = $logDir.'/jobs.log';
if (!is_resource(self::$handler)) {
self::$handler = fopen($logPath, 'a');
}
$stat = fstat(self::$handler);
if ($stat['size'] > 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;
}
}
}
更多推荐
所有评论(0)