自定义 PHP think 命令无法在命令类文件里面使用 Db 类
thinkPHP版本是5.0.14
输入 php think
在 Available commands:
下面能看到有housePost命令
Available commands:
build Build Application Dirs
clear Clear runtime file
help Displays help for a command
housePost 房源定时批量公布命令
list Lists commands
这是命令行类,直接echo输出没有问题,但是模型或者Db类就会报PDO的错误
namespace app\command\controller;
use think\console\Command;
use think\console\Input;
use think\console\Output;
use think\console\input\Argument;
use app\index\model\HouseVillage;
use think\Db;
class HousePost extends Command
{
protected function configure()
{
$this->addArgument('houseId',Argument::REQUIRED); #必须参数
$this->addArgument('date',Argument::OPTIONAL); #可选参数
$this->setName('housePost')->setDescription('房源定时批量公布命令');
}
protected function execute(Input $input, Output $output)
{
$houseId = $input->getArgument('houseId');
$date = $input->getArgument('date');
#逻辑代码
$result = HouseVillage::all();
// $result = Db::table('house_village')->select();
#输出代码
$output->writeln("HousePost:houseId=".json_encode($result));
// $output->writeln("TestCommand:test2=".json_encode($date));
}
}
调用模型后返回PDO的错误
xxxxxx@DESKTOP-UNTNOIG:/....../www/......../code/lhzs_back$ php think housePost 定时测试任务
[PDOException]
could not find driver
php.ini里面PDO肯定是打开的
有熟悉thinkPHP命令行的朋友知道怎么解决吗?
已解决,确实是命令行的PHP和项目的PHP版本不一致的问题
按你的描述,应该是能执行成功的。有可能是命令行使用的php跟tp使用的php不是同一个,
php -v
查看下版本。