ExportTerm.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. <?php
  2. namespace App\Console\Commands;
  3. use Illuminate\Console\Command;
  4. use Illuminate\Support\Facades\Storage;
  5. use App\Models\DhammaTerm;
  6. use Illuminate\Support\Facades\Log;
  7. class ExportTerm extends Command
  8. {
  9. /**
  10. * The name and signature of the console command.
  11. *
  12. * @var string
  13. */
  14. protected $signature = 'export:term';
  15. /**
  16. * The console command description.
  17. *
  18. * @var string
  19. */
  20. protected $description = 'Command description';
  21. /**
  22. * Create a new command instance.
  23. *
  24. * @return void
  25. */
  26. public function __construct()
  27. {
  28. parent::__construct();
  29. }
  30. /**
  31. * Execute the console command.
  32. *
  33. * @return int
  34. */
  35. public function handle()
  36. {
  37. $exportFile = storage_path('app/public/export/offline/sentence-'.date("Y-m-d").'.db3');
  38. $dbh = new \PDO('sqlite:'.$exportFile, "", "", array(\PDO::ATTR_PERSISTENT => true));
  39. $dbh->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_WARNING);
  40. $dbh->beginTransaction();
  41. $query = "INSERT INTO dhamma_terms ( uuid , word , word_en , meaning ,
  42. other_meaning , note , tag , channel_id,
  43. language, owner, editor_id,
  44. created_at,updated_at,deleted_at)
  45. VALUES ( ? , ? , ? , ? ,
  46. ? , ? , ? , ? ,
  47. ?, ?, ?,
  48. ?, ?, ? )";
  49. try{
  50. $stmt = $dbh->prepare($query);
  51. }catch(PDOException $e){
  52. Log::info($e);
  53. return 1;
  54. }
  55. $bar = $this->output->createProgressBar(DhammaTerm::count());
  56. foreach (DhammaTerm::select(['guid','word','word_en','meaning',
  57. 'other_meaning','note','tag','channal',
  58. 'language',"owner","editor_id",
  59. "created_at","updated_at","deleted_at"
  60. ])
  61. ->cursor() as $row) {
  62. $currData = array(
  63. $row->guid,
  64. $row->word,
  65. $row->word_en,
  66. $row->meaning,
  67. $row->other_meaning,
  68. $row->note,
  69. $row->tag,
  70. $row->channal,
  71. $row->language,
  72. $row->owner,
  73. $row->editor_id,
  74. $row->created_at,
  75. $row->updated_at,
  76. $row->deleted_at,
  77. );
  78. $stmt->execute($currData);
  79. $bar->advance();
  80. }
  81. $dbh->commit();
  82. $bar->finish();
  83. return 0;
  84. }
  85. }