Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
Fuel Task don't have acces to config (using oil refine)
  • Hi there, 
    I'm using fuelphp since three weeks and everything's just great and fine, but I have a strange problem when I create tasks.

    Here is the code : 

    <?php
    namespace Fuel\Tasks;
    use \Fuel\Core\Config;

    class Com
    {
    public static function run($args = null)
    {
    $cfg = Config::get("db");
    print_r($cfg);

    }
    }

    The config is empty

    Tried out with \Mongo_DB::instance()
    And getting : Error - Invalid instance name given.

    So it looks like config ain't loaded

    On my bashrc I have done : env FUEL_ENV=production php oil -v
    Fuel: 1.5 running in "production" mode

    The website is running fine with this env

    Thank you for some help
    Vincent
  • HarroHarro
    Accepted Answer
    You can only "get" a config item if it is loaded. The classes that use it will load it automatically, but if you want to access it before you call it, you have to load it manually:

    Config::load('db');

    before you can get values from it.

  • Thanks for the answer, but it's not my big problem, here is the code I really want (access my mongo_db collection)

    <?php
    namespace Fuel\Tasks;
    use \Fuel\Core\Config;
    use \Fuel\Core\Mongo_Db;

    class Com
    {
    public static function run($args = null)
    {
    $db = Mongo_DB::instance();

    }
    }
    I get Error - Invalid instance name given. in COREPATH/classes/mongo/db.php on line 125

    That mean the mongo_db cannot find a valid config for mongo default when it loads and get this config

    So I tried to dump the config and there is nothing even if do this

    <?php
    namespace Fuel\Tasks;
    use \Fuel\Core\Config;
    use \Fuel\Core\Mongo_Db;

    class Com
    {
    public static function run($args = null)
    {
    Config::load('db');
    $cfg = Config::get("db");
    print_r($cfg);

    }
    }

    It behaves like the task doesn't know what is the environement or the config file

    Thanks a lot
    Vincent
  • For starters, NEVER use \Fuel\Core classes. All Fuel core classes live in the global namespace.

    If you don't specify an instance name, the Mongo driver, like all other DB drivers, will use the 'default' instance.

    Which means in your db,php config file, it will look for a 'mongo.default' entry. And it will throw this exception if that entry does not exist.
  • Ok find out the right solution :
    this way everything works


  • The trick is that Task use app/config/db.php
    Not the other one : (staging || test || production) 

    <?php
    namespace Fuel\Tasks;

    class Com
    {
    public static function run($args = null)
    {
    $db = \Mongo_Db::instance();
    print_r($db);

    }
    }

    thanks a lot Harro and have a great week
  • The Config class is used to load the configuration files, and that will always use the file in the environment folder if it exists.

    If you don't tell oil which environment to use, it will use "development".

    You can switch enviroments on the commandline using:

    env FUEL_ENV=staging php oil refine yourtask

  • hmmm it was the case (see my first post) Think I miss something somewhere
    But thank you it's more clear and it's working now :)

Howdy, Stranger!

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

In this Discussion