Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
Cross-calling (HMVC Style) modules with separate database files...
  • Hello All, My team and I have just started the exploratory phase on a very large scale project that we will need to use HMVC on--we are a CodeIgniter shop currently but CI isn't able to do what we need on this project so I've started testing Fuel. Thus far it looks like Fuel will do everything we need--I just can't figure out how to get around one small problem that I am hoping has a simple fix. By and large we will be trying to use modules as independent MVC projects that will need to cross-call each other in some specific situations. The problem I am having is when I have two modules and module #1 loads it's local config/db.php files and does a query, then in the same method I attempt to reach across to module #2 and have it return a result set from it's own config/db.php load and query. It appears that module #2 tries to use the same connection that module #1 instantiated and fails because the table I am selecting from doesn't exist (obviously) in the original database. First, is this possible, and second how might I go about getting it to work. I'll post my examples below. Thanks in advance!! <b>Module #1</b>
    <code>
    echo "Module #1 Result";
    $result_1 = \DB::query('SELECT * FROM admin;', \DB::SELECT)->execute()->as_array();
    print_r($result_1); \Fuel::add_module('qualmobile');
    echo \Request::factory('qualmobile/stuff/stuff/')->execute();
    </code>
    <b>Module #2</b>
    <code>
    echo "Module #2";
    $result = \DB::query('SELECT * FROM module_2_table;', \DB::SELECT)->execute()->as_array();
    print_r($result);
    </code> If in Module#1 I remove the first section (the DB::query call) then the result from Module #2 works like a charm.... My end goal is to be able to completely encapsulate each module's MVC components (including separate DB configs) without having to centrally load or locate that data.
  • If you use "execute()" without parameters, you're telling the query builder to use the default database instance to execute the query. Which is the one defined in the app/config/db.php as active. You can instruct the query builder to use a specific instance by passing the instance object to the execute method:
    $db = \Database_Connection::instance('moduledb');
    
    $result_1 = \DB::query('SELECT * FROM admin;', \DB::SELECT)->execute($db)->as_array();
    

    where 'mycustomdb' is a section in your config/db.php, either global or in the module. Because when you load it, it will be merged with whatever is already loaded, use a unique db indentifier per module (for example the module name).

Howdy, Stranger!

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

In this Discussion