简体   繁体   English

Laravel-定期从数据库表中提取

[英]Laravel - periodically pulling from a database table

In Laravel, I have a table of articles which I pulled from a database at the start of my project over a month ago. 在Laravel,我有一个表格,一个多月前在项目开始时从数据库中提取了这些表格。

This database is separate from my Laravel application but the content may change daily and I've been manually grabbing the content every three days, which as you can imagine takes time. 这个数据库与我的Laravel应用程序是分开的,但是内容可能每天都在变化,我一直每三天手动获取一次内容,这可以想象会花费一些时间。

I've seen that you can have two database connects in Laravel, like so: 我已经看到您可以在Laravel中建立两个数据库连接,如下所示:

<?php
return array(

'default' => 'mysql',

'connections' => array(

    # Our primary database connection
    'mysql' => array(
        'driver'    => 'mysql',
        'host'      => 'host1',
        'database'  => 'database1',
        'username'  => 'user1',
        'password'  => 'pass1'
        'charset'   => 'utf8',
        'collation' => 'utf8_unicode_ci',
        'prefix'    => '',
    ),

    # Our secondary database connection
    'mysql2' => array(
        'driver'    => 'mysql',
        'host'      => 'host2',
        'database'  => 'database2',
        'username'  => 'user2',
        'password'  => 'pass2'
        'charset'   => 'utf8',
        'collation' => 'utf8_unicode_ci',
        'prefix'    => '',
    ),
),

); );

So, If I have my secondary article table that I can connect to is it possible to create a cron job that pulls in new content to my Laravel application every hour? 因此,如果我可以连接到我的二级文章表,是否可以创建一个cron作业,每小时将新内容提取到我的Laravel应用程序中?

When pulling from the secondary database, how could I avoid overwriting content? 从辅助数据库中提取数据时,如何避免覆盖内容?

You can define a model for the secondary database like this 您可以像这样为辅助数据库定义模型

namespace App\Secondary;

class Article extends Model {

    protected $connection = 'mysql2';

    public static function fetchArticles(){
      //fetch articles

      //TODO all other filter to fetch new records
      $articles = Article::all();
       return $articles;
    }
}

How to avoid overwrite content? 如何避免覆盖内容?

if you have id or any identity column in both primary and secondary connection db table then just fetch the latest article id from primary connection article table and fetch new articles after that id from secondary db table. 如果您在主连接数据库表和辅助连接数据库表中都具有ID或任何标识列,则只需从主连接文章表中获取最新的文章ID,然后从辅助数据库表中获取该ID之后的新文章。

Here is the scheduler class 这是调度程序类

namespace App\Console\Commands;

use Illuminate\Console\Command;

class ArticlePuller extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'articlePuller';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Pull article from secondary database';

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        $articles = Secondary/Article::fetchArticles(); //from secondary db table 
        Article::insertArticles($articles); 
    }
}

Define this scheduler in console/Kernel.php 在console / Kernel.php中定义此调度程序

 protected $commands = [
    Commands\ArticlePuller::class
 ];

 protected function schedule(Schedule $schedule)
 {
    $schedule->command('articlePuller')->hourly();
 }

Now need to add this scheduler entry in cron job 现在需要在cron作业中添加此调度程序条目

* * * * * php path_to_artisan/artisan schedule:run >> /dev/null 2>&1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM