feulf / rainframework

Easy MVC Framework for PHP

Home Page:https://rainphp.github.io/rainframework

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Error!: could not find driver

FDiskas opened this issue · comments

I think that you need first to check if a server supporting a PDO
For example here: https://github.com/rainphp/rainframework/blob/master/system/library/DB.php#L249

foreach(PDO::getAvailableDrivers() as $driver)
   {
      echo $driver.'<br />';
   }
commented

It seems a good idea to me, could you do send me the change as a pull request? In case I'll work on it when I can. Thanks!

For getting error - try to use an undefined driver by changing config file. For example $db['default']['driver'] = 'demo';
I restored the default string name for the $db as it was in older versions for better compatibility.

And one more change.

===================================================================
--- system/library/DB.php   (date 1330470493000)
+++ system/library/DB.php   (revision )
@@ -31,8 +31,17 @@
        // load the variables
        require self::$config_dir . self::$config_file;

+       // Return if undefined
+       if ( !isset($db))
+           return;
+
+       // If empty set by default to mysql
+       if ( !isset( $db['default']['driver'] ) ) {
+           $db['default']['driver'] = 'mysql';
+       }
+
        // Check for available driver
-       if (!in_array($db['default']['driver'],PDO::getAvailableDrivers())) {
+       if (isset($db['default']['driver']) && !in_array($db['default']['driver'],PDO::getAvailableDrivers())) {
            die("Error!: could not find a <a href=\"http://php.net/pdo.drivers.php\" target=\"_blank\">" . $db['default']['driver'] . "</a> driver<br/>");
        }
        // Format connection string
\ No newline at end of file
commented

Nice, but need one more change, db::init() should be db::init( $name = "default" ), and inside instead it should use $db[ $name ]['driver'], so if you want to use 2 different database, one for production and one for development, you can do db::init("production") or db::init("development").

Also the new static class DB introduced a new problem, you can't connect to multiple database, where with the previous multiton version you could. Any idea of how to solve this problem as well?

Thanks for your help!

It could help you: http://stackoverflow.com/questions/3524749/pdo-refuses-to-switch-between-multiple-databases

To close the connection, you need to destroy the object by ensuring that all remaining references to it are deleted--you do this by assigning NULL to the variable that holds the object. If you don't do this explicitly, PHP will automatically close the connection when your script ends.

commented

May be I was not clear, closing the connection and manage the memory is not a problem.

The problem is to switch between databases. Although DB as static is designed to work for a single database, I think is good to have this possibility, especially to switch between development and production databases, here how it should work:

// select database 'default'
db::init();
db::query( ... );

db::select_database( "db2" );
// query 'db2'
db::query( ... );
// close connection with db2 and select database 'default'
db::disconnect( "db2" );
// or db::select_database( "your new database" );

// query 'default'
db::query( ... );

DB class is designed to work with single database application, it fit perfectly the needs I have in Rain CMS. For multiple database application, you may want to use a different wrapper, ORM or even a modification of DB as object class, here how it should work:

// DB class used as object for multiple connection
$db = new Rain\Multiple\DB();
$db2 = new Rain\Multiple\DB( "db2" );

$db->query( ... );
$db2->query( ... );

Let me know if you'll implement the select_database() method, in case I will get your pull request and add it.

Sorry, I just wanted to see how you are doing in this framework. Without going too much - I just suggested some solutions for you. Only you can decide how to do it.

Greetings from Lithuania

And multiple database is not a priority I think.

commented

Hey FDiskas, thanks for your help I've just pushed the change on DB class to manage multiple database and multiple connection:
9751b08

commented

Here is the latest commit, thanks
5a12294

Thank you as well. I hope one day to join this project.

commented

Sure and keep one eye on www.raincms.com, that it will go live soon!