123456789101112131415161718192021222324252627282930313233343536373839404142434445 |
- <?php
- namespace app\admin\command;
- use app\common\model\Book;
- use think\console\Command;
- use think\console\Input;
- use think\console\Output;
- use think\Db;
- use think\Request;
- class BookEnum extends Command
- {
- /**
- * 配置指令
- */
- protected function configure()
- {
- $this->setName('BookEnum')->setDescription('遍历 book mysql 与 book change redis');
- }
- protected function execute(Input $input, Output $output)
- {
- Request::instance()->module('admin'); //cli模式下无法获取到当前的项目模块,手动指定一下
- try {
- /** @var Book $bookModel */
- $bookModel = model('book');
- Db::table('book')->field('id,name,realname')->chunk(100, function ($bookList) use ($bookModel, $output) {
- foreach ($bookList as $book) {
- $bookInfo = $bookModel::getBookInfo($book['id']);
- $output->info("mysql book:" . json_encode($book,
- JSON_UNESCAPED_UNICODE) . " redis book:" . json_encode($bookInfo, JSON_UNESCAPED_UNICODE));
- }
- });
- } catch (\Throwable $Th) {
- $output->info('error msg:' . $Th->getMessage().' trace:'.$Th->getTraceAsString());
- }
- $output->info('book enum over');
- }
- }
|