Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
Using config files within a module
  • I wrote a quick and dirty module that connects to a database containing Wordpress blogs to pull out the latest 3, but I can't seem to override the main configuration of Fuel. I have modules/blog/config/db.php and then try and load the DB using "Config::load('blog::db');" but can't seem to get it to work. The error that appears says: Database type not defined in {name} configuration I made the default type 'mysql' in every db.php file I can find, but that doesn't seem to be it. I see almost no documentation on configuration files within modules...
  • I don't think anyone tried to store a database configuration in a module before, so you have a first! There are a few gotcha's you have to take into account. The one I think is important here is that the database drivers do a forced load of the config. So if you load your db config from "blog" before the first database call, your config will be overwritten by the one found in app/config.
  • The way it was fixed is editing modules/blog/config/db.php and renaming it to modules/blog/config/blog_db.php, then taking out the multidimensional array and replacing it with one array. return array( /**
    * Base config, just need to set the DSN, username and password in env. config.
    */ // a MySQL driver configuration
    'type' => 'mysql',
    'connection' => array(
    'hostname' => 'localhost',
    'port' => '3306',
    'database' => 'database',
    'username' => 'username,
    'password' => 'password',
    'persistent' => false,
    ),
    'table_prefix' => '',
    'charset' => 'utf8',
    'caching' => false,
    'profiling' => false, ); Then in modules/blog/classes/model/blog.php, load the config and add $db to execute($db): class Model_Blog extends \Model
    {
    public static function get_blog_entries($limit = 3)
    {
    // Load alternative DB connection info from config/blog_db.php
    Config::load('blog::blog_db', 'blog_db');
    $config = Config::get('blog_db');
    $db = \Database_Connection::instance('blog', $config); $result = DB::query('DB QUERY')->execute($db)->as_array(); return $result;
    }
    }

Howdy, Stranger!

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

In this Discussion