Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
Sessions -> database_connection class throwing exceptions left and right
  • I'm having a bit of trouble getting sessions to work with the db driver. My config is set to use db. My config for db is correctly defined. When the session tries to start...first I get an exception saying by database type isn't defined: Fuel\Core\Fuel_Exception [ Error ]: Database type not defined in fmslumberhandling configuration This comes from line 62 in /core/database/connection.php: if ( ! isset($config))
    {
    throw new \Fuel_Exception("Database type not defined in {$name} configuration");
    } But what's weird is that isset($config evaluates to true AND false. If I do this: vardump($config);
    exit; I see my entire config array as expected. So I commented out the exception and went about my happy way. Then I get: ErrorException [ Compile Error ]: Cannot redeclare class Fuel\Core\Database_Connection I do see in the stack trace that an instance of the DB class gets called... COREPATH/classes/database/query.php @ line 193 188 public function execute($db = NULL)
    189 {
    190 if ( ! is_object($db))
    191 {
    192 // Get the database instance
    193 $db = \Database_Connection::instance($db);
    194 }
    195
    196 // Compile the SQL query
    197 $sql = $this->compile($db);
    198 COREPATH/classes/session/db.php @ line 65 60 $this->keys = $this->time->get_timestamp();
    61 $this->keys = $this->keys;
    62 $this->keys = '';
    63
    64 // create the session record
    65 $result = \DB::insert($this->config, array_keys($this->keys))->values($this->keys)->execute($this->config);
    66
    67 // and set the session cookie
    68 $this->_set_cookie();
    69 }
    70 But why is the Database_Connection class getting redeclared? I pulled some code from the develop branch (the new IPV6 compliant IP check), but other than that, I'm using it out of the box. Here are my config settings: // specific configuration settings for database based sessions
    'db' => array(
    'cookie_name' => 'fueldid', // name of the session cookie for database based sessions
    'database' => 'fmslumberhandling', // name of the database name (as configured in config/db.php)
    'table' => 'sessions', // name of the sessions table
    'gc_probability' => 5 // probability % (between 0 and 100) for garbage collection
    ), // if no session type is requested, use the default
    'driver' => 'db', Dunno....
  • Don't know if this helps, but in my session.php file, I have 'database' => null like below
     // specific configuration settings for database based sessions
     'db'   => array(
      'cookie_name'  => 'fueldid',    // name of the session cookie for database based sessions
      'database'   => null,     // name of the database name (as configured in config/db.php)
      'table'    => 'sessions',    // name of the sessions table
      'gc_probability' => 5      // probability % (between 0 and 100) for garbage collection
          ),
    
  • Hah. Actually that fixed it. Thanks, man. I think maybe the comment should be updated to read 'leave this to null to get from configuration in config/db.php'
  • Updated configuration and made pull request to develop. with new message: // name of database. Leave as null to get configuration from config/db.php Thanks for the help, Mitchell. I need to start paying you for the support dude. Seriously, if you ever want some contract work, hit me up.
  • To be more specific, if you set it to null, the current active database will be used. This works because the DB driver re-uses the current instance if no named instance is specified. I find it dangerous to promote the use of null here, as it can lead to a lot of misery when you're using multiple databases, and/or multiple environments. The issue at hand here was caused by misconfiguration, the session documentation clearly defines what has to be configured here. If you specifically want to re-use the current database, use \Config::get('environment') instead, so you'll be sure it always uses whatever is configured as active database. I'll clarify this point in the docs.
  • @WanWizard: That's good to know about using 'Config::get('environment') instead of null. If you're changing the documentation, you may want to fix the example on the Session class configuration page for the "db" field. It shows 'database' => 'dev' as one of the fields (see below). 'dev' was replaced by 'development' a little while ago.
    array(
            'cookie_name'    => 'fueldid',
            'database'       => 'dev',
            'table'          => 'sessions',
            'gc_probability' => 5
         )
    

    @Calvin: You're welcome. I have a feeling you're just slightly behind me in the learning curve.
  • Noticed that as well, it's changed to 'development' now.
  • Hey WanWizard, I configured it with having 'mydb' in place of null. That threw the exceptions. How SHOULD that be configured? \Config::get('environment') gets current database - what if (for whatever reason) someone wanted to use another?
  • You need to configure the database instance name there, as described in the docs, and defined in config/db.php. I don't mean the name of your database, I mean this:
    // The name of this database instance is Fuel::DEVELOPMENT !
    
     Fuel::DEVELOPMENT => array(
      'type'   => 'mysql',
      'connection' => array(
       'hostname'   => 'localhost',
       'database'   => 'DEV_dbname',
       'username'   => 'user',
       'password'   => 'pass',
       'persistent' => false,
      ),
      'table_prefix' => 'core_',
      'charset'      => 'utf8',
      'caching'      => false,
      'profiling'    => false,
     ),
    

Howdy, Stranger!

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

In this Discussion