dongyidao1461 2018-06-18 16:59
浏览 56

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

In Laravel, I have a table of articles which I pulled from a database at the start of my project over a month ago.

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.

I've seen that you can have two database connects in Laravel, like so:

<?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?

When pulling from the secondary database, how could I avoid overwriting content?

  • 写回答

1条回答 默认 最新

  • doufangxian4985 2018-06-18 17:21
    关注

    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.

    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

     protected $commands = [
        Commands\ArticlePuller::class
     ];
    
     protected function schedule(Schedule $schedule)
     {
        $schedule->command('articlePuller')->hourly();
     }
    

    Now need to add this scheduler entry in cron job

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

报告相同问题?

悬赏问题

  • ¥15 Oracle中如何从clob类型截取特定字符串后面的字符
  • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
  • ¥15 MATLAB中streamslice问题
  • ¥15 如何在炒股软件中,爬到我想看的日k线
  • ¥15 seatunnel 怎么配置Elasticsearch
  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端
  • ¥15 基于PLC的三轴机械手程序