Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
Task Issues
  • Hi all Creating a task to setup my app but ran into two issues. 1. Cannot run core tasks Install or Migrate in my task.
    PHP Fatal error:  Class 'Fuel\Tasks\Install' not found 
    
    Worked around by copy pasting the install task's code into my task. 2. Cannot call Migrate::latest() in task.
    1046 - No database selected [ CREATE TABLE IF NOT EXISTS `migration` (
            `current` int(11) DEFAULT '0' NOT NULL
    ) DEFAULT CHARACTER SET utf8; ] in COREPATH/classes/database/mysql/connection.php on line 203
    
    Could not find a workaround. Full code:
    class Setup {
    
        public static function run() {
            $db_name = 'darkcrm';
    
            // Drop database
            try {
                \Fuel\Core\DBUtil::drop_database($db_name);
                \Fuel\Core\Cli::write('Successfully dropped database.', 'green');
            } catch (\Fuel\Core\Database_Exception $ex) {
                \Fuel\Core\Cli::error($ex->getMessage());
            }
    
            // Create database
            try {
                \Fuel\Core\DBUtil::create_database($db_name);
                \Fuel\Core\Cli::write('Successfully created database.', 'green');
            } catch (\Fuel\Core\Database_Exception $ex) {
                \Fuel\Core\Cli::error($ex->getMessage());
            }
    
            // Install
            // Install::run(); // Fails - PHP Fatal error:  Class 'Fuel\Tasks\Install' not found
            // Workaround:
            $writable_paths = array(APPPATH . 'cache', APPPATH . 'logs', APPPATH . 'tmp', APPPATH . 'config');
            foreach ($writable_paths as $path) {
                if (@chmod($path, 0777)) {
                    \Cli::write('Made writable: ' . $path, 'green');
                } else {
                    \Cli::write('Failed to make writable: ' . $path, 'red');
                }
            }
    
            // Migrate
            \Fuel\Core\Migrate::latest();
    
            // Insert core data
            $core_data = \Fuel\Core\File::read(APPPATH . 'sql' . DS . 'core_data.sql', true);
            $insert_array = explode(';', $core_data);
            \Fuel\Core\DB::start_transaction();
            try {
                foreach ($insert_array as $insert_statement) {
                    $result = \Fuel\Core\DB::query($insert_statement, \Fuel\Core\DB::INSERT)->execute();
                    if (!$result) {
                        throw new \Fuel\Core\Database_Exception('Failed to execute INSERT query');
                    }
                }
                \Fuel\Core\DB::commit_transaction();
                \Fuel\Core\Cli::write('Successfully executed core data INSERT queries.', 'green');
            } catch (\Fuel\Core\Database_Exception $ex) {
                \Fuel\Core\DB::rollback_transaction();
                \Fuel\Core\Cli::error($ex->getMessage());
            }
        }
    
    }
    

    Would appreciate any help.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!