drtpbx3606 2016-03-24 11:17
浏览 29
已采纳

创建一个cron以自动启动数据库播种器

In my Laravel project, I have a database that I get through an url. This database is in json. Following my project specification, I have to sotre the database in my local project, create a monthly cron in order to be up to date once by month.

Recover the datas is not so hard, but I have to create some seeder with this datas. is there a way to implement a monthly cron for database seeder like the way I use a monthly cron to be up to date with initail data ?

To be more specific, this is my cron to recover the initial data in my CronController.php :

public static function updateJson(Request $request)

{
  $type="application/json";
  $charset="utf-8";
  $path = 'database/market.json';

  $time=filemtime($path);
  $update=date("Y-M-d",$time);

  $updateMonth= date('Y-M-d',strtotime('+1 month',strtotime($update)));

  $now = date('Y-M-d');

  if($now >= $updateMonth)
  {
    $data = file_get_contents('url/for/the/public/database');

    $data = mb_convert_encoding($data, 'HTML-ENTITIES');
    $res= file_put_contents($path, $data);

    $whatIReallyNeed='update ok';

    return response()->json($whatIReallyNeed)->header('Content-type', $type, 'charset', $charset);

  } else {

    $error = 'no need to update';
    return response()->json($error)->header('Content-type', $type, 'charset', $charset);
  }
}

For example I have a town table in my database, check the migration of this table:

class CreateTownTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('town', function (Blueprint $table) {
            $table->increments('id');
            $table->string('name', 255);
            $table->timestamps();
            $table->softDeletes();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::drop('town');
    }
}

And the seeder to register datas from the json database TownTableSeeder.php :

public function run()
{
  $path = "public/database/market.json";
  // get contents of file
  $json = file_get_contents($path);
  // decode json datas
  $datas = json_decode($json, TRUE);
  // get the multi json array
  $arrayFeatures = $datas["features"];
  // define a new empty array
  $arrayTown = array();
  // populate the new array with values we need
  foreach ($arrayFeatures as $feature) {
    array_push($arrayTown, $feature["properties"]["commune"]);
  }
  // get unique entry for array
  $results = array_unique($arrayTown);
  // get only vales of array after do array_unique (no index)
  $whatIReallyNeed = array_values($results);
  foreach ($results as $result) {
    DB::table('town')->insert([
        'name' => $result,
    ]);
  }
}

All of this works well, now I was wondering if i could launch the seeds migrations when the cron have updated the json database i store in my project.

  • 写回答

1条回答 默认 最新

  • dongmei2957 2016-03-24 11:21
    关注

    You can use Laravel Events. When cron updates json data, just fire an event.

    An example from a manual:

    Event::fire(new PodcastWasPurchased($podcast));
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能
  • ¥15 jmeter脚本回放有的是对的有的是错的
  • ¥15 r语言蛋白组学相关问题
  • ¥15 Python时间序列如何拟合疏系数模型
  • ¥15 求学软件的前人们指明方向🥺
  • ¥50 如何增强飞上天的树莓派的热点信号强度,以使得笔记本可以在地面实现远程桌面连接
  • ¥20 双层网络上信息-疾病传播
  • ¥50 paddlepaddle pinn
  • ¥20 idea运行测试代码报错问题