自定义 PHP think 命令无法在命令类文件里面使用 Db 类

thinkPHP 版本是 5.0.14
输入 php thinkAvailable 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 肯定是打开的

phpinfo的信息

有熟悉 thinkPHP 命令行的朋友知道怎么解决吗?

附言 1  ·  5年前

已解决,确实是命令行的 PHP 和项目的 PHP 版本不一致的问题

最佳答案

按你的描述,应该是能执行成功的。有可能是命令行使用的 php 跟 tp 使用的 php 不是同一个,php -v 查看下版本。

5年前 评论
讨论数量: 2

按你的描述,应该是能执行成功的。有可能是命令行使用的 php 跟 tp 使用的 php 不是同一个,php -v 查看下版本。

5年前 评论

跟一楼的想法一样 ,PHP 版本不一致 :thumbsup:

5年前 评论