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));
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 2024-五一综合模拟赛
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭