Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
Module DB
  • Hi Guys, 

    I'm attempting to load a database config from my module folder, but it doesn't appear to be working. 

    I have a module folder, with a module called "frontend" in but it doesn't get the db configuration, even if i put it in frontend/config or frontend/config/development. 

    I'm autoloading the module within index.php using Module::load('frontend');

    Which as I said appears to be working, other than this thorny DB config issue I'm having. I was wondering if anyone knows where I'm going wrong?
  • By default it will use 'default' connection. If you want to change another connection, use this:

    DBUtil::set_connection('my_database_group');


    Hopefully this will help.

    P.S. I'm not very familiar with DB Util.
  • If you are using Orm Model, you can set which connection you want:


    class Model_Article extends Orm\Model
    {
    protected static $_connection = 'articles_database';
    }
  • You can't put database config in a module, it has to be in app/config. This is because it's read before modules are activated, and since config is cached, it will not read it again.
  • Harro are you certain on this? Seems strange that this would be the case, also given that I can store other config in there. If this is the case it's silly. 

    In that case then, is there anyway I can have one application, but run multiple sites with it (all with their own config dirs).

    A folder structure similar to this..

    - Applications
    - My Application
    - Application
    - Core
    - Modules
    - Etc
    - Sites
    - Site 1
    - Config
    - Db.php
    - Public_HTML
    - Index.php
    - Site 2
    - Config
    - DB.php
    - Public_HTML
    - Index.php

    Any suggestions would be fantastic.
  • argh it's removed all of my tabs... eugh... 

  • I was thinking maybe a way to do this would be to extend the core database connection class?
  • Hi Guys, 

    I just thought I'd let you know how I did this in case someone else wanted to do it.

    In /app/classes I created a new folder called "database" and basically overriden the database connection class by adding a file called "connection.php" in here.

    The contents of this file are:

    <?php
    abstract class Database_Connection extends \Fuel\Core\Database_Connection
    {
    public static function instance($name = null, array $config = null)
    {
      \Config::load('db',true);
    \Config::load('frontend::db', true);
    if ($name === null)
    {
    // Use the default instance name
    $name = \Config::get('db.active');
    }

    if ( ! isset(static::$instances[$name]))
    {
    if ($config === null)
    {
    // Load the configuration for this database
    $config = \Config::get("db.{$name}");
    }
    if ( ! isset($config['type']))
    {
    throw new \FuelException("Database type not defined in {$name} configuration");
    }

    // Set the driver class name
    $driver = '\\Database_' . ucfirst($config['type']) . '_Connection';

    // Create the database connection instance
    new $driver($name, $config);
    }

    return static::$instances[$name];
    }
    }
    ?>

    The only real change to this file is this line:

    \Config::load('frontend::db', true); so not only does it load the normal DB config but also the one for the module.

    Somehow these seem to work in the environment folders (.e.g development or production) which I didn't think they would but it's a good thing.

    I also the had to adjust the bootstrap file for my application - /app/bootstrap.php so that it used my new custom version. 

    I added this section into the file.

    Autoloader::add_classes(array(
    // Add classes you want to override here
    // Example: 'View' => APPPATH.'classes/view.php',
    'Database_Connection' => APPPATH.'classes/database/connection.php',
    ));

    It all appears to be working now!

    regards

    James
  • Module files are only loaded when the module is active, which is only when you get routed to the module, either via the main request or via an HMVC request.

    The db config file get's loaded on first use, so if you can construct it so that you do an HMVC call to the module before any database activity, you can load it there.

Howdy, Stranger!

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

In this Discussion