Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
Database connection with db.yml file
  • I want to connect database in fuel php with the help of db.yml.
    I followed http://fuelphp.com/docs/classes/config.html
    But i am not able to connect-
    My db.yml is:
    default:
                    connection:
                            dsn : mysql:host=localhost;dbname=user_info
                            username : root
                            password : aaa000

    I am able to connect using  db.php
    db.php
    return array(
            'default' => array(
                    'connection'  => array(
                            'dsn'        => 'mysql:host=localhost;dbname=user_info',
                            'username'   => 'root',
                            'password'   => 'aaa000',
                    ),
            ),
    );


    What is wrong with my yml file. I have added Config::load('db.yml', true) in connection.php

    My Config::get('db.active') is returning null
  • HarroHarro
    Accepted Answer
    A standard db config contains a lot more, this is a standard default entry:

    array (size=5)
      'active' => string 'default' (length=8)
      'default' =>
        array (size=9)
          'type' => string 'mysqli' (length=6)
          'connection' =>
            array (size=6)
              'persistent' => boolean false
              'compress' => boolean false
              'hostname' => string 'localhost' (length=9)
              'database' => string 'databasename' (length=12)
              'username' => string 'user' (length=6)
              'password' => string 'pass' (length=6)
          'identifier' => string '`' (length=1)
          'table_prefix' => string '' (length=0)
          'charset' => string 'utf8' (length=4)
          'collation' => string 'utf8_general_ci' (length=15)
          'enable_cache' => boolean true
          'profiling' => boolean true
          'readonly' => boolean false

    Your get('db.active') returns null because you haven't defined it, it is not in your db.yml.

    If you specify your config files are yaml files, they all have to be, the framework doesn't merge config files of different types. With php files you get a lot of this default because there's a base db.php config file in /fuel/core/config which provides you the defaults. But there is no db.yml file, so no defaults to start with.
  • Thank you very much!! It works
  • BTW, changing framework code is not a good idea, your changes will get lost when it's time to upgrade the framework.

    It's probably better to create a db.php file in your config folder containing:

    Config::load('db.yml', true);
    return
    Config::get('db');

    Or alternatively add to your app bootstrap.php:

    Config::load('db', true);
    Config::load('db.yml', true);

Howdy, Stranger!

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

In this Discussion