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 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?