djekl
8/12/2013 - 11:45 AM

Function to pull all SQL files in and pass to a model (use with caution)

Function to pull all SQL files in and pass to a model (use with caution)

<?php
    /**
     * public sql_update()
     */
    function sql_update()
    {
        @header('Content-Type: Text/Plain');

        $sql_dir   = FCPATH . 'sql/';
        $last_file = $sql_dir . '.last';

        print $last_file . PHP_EOL . PHP_EOL;

        if (!file_exists($last_file)) {
            touch($last_file);
        }
        else {
            print 'Last Import = ' . file_get_contents($last_file) . PHP_EOL . PHP_EOL;
        }

        $last_import  = trim(file_get_contents($last_file));
        $remove_count = 0;
        $last_key     = 0;
        $files        = array();

        foreach (scandir($sql_dir) as $key => $value) {
            if(!startsWith($value, '.')) {
                if($value == $last_import) {
                    $last_key = $key;
                }

                $files[] = $value;
            }
            else {
                $remove_count++;
            }
        }

        $last_key = $last_key - $remove_count;

        $this->load->model('data_model');
        foreach ($files as $key => $value) {
            if ($key > $last_key) {
                print $sql_dir . $value . PHP_EOL;
                var_dump($this->data_model->sql_update($sql_dir . $value));
                file_put_contents($last_file, $value);
                print file_get_contents($last_file) . PHP_EOL;
            }
        }
    }
    //------------------------------------------------------------------