vendredi 11 septembre 2015

Return by reference to change CodeIgniter database configs

I'm trying change the database config at execution time in CI. I've had a little progress,but here we use 3 database managers: ActiveRecord(native from CI),Lumine(a small and old ORM) and Doctrine. I'll try explain better.

In CI exists a database static config file,called database.php. In that file you put the configs,in a matrix format:

$db['config_group']['username']
$db['config_group']['password']

Well,I had to create a Class to generate and return that configuration. So I found a way that I could change it dinamically:

DatabaseClass

Then in the database.php I call it:

$database = DatabaseManager::initDatabase('address',"user","db");
$active_record = &$database->getActiveRecord();
$active_group  = &$database->getActiveGroup();
$db = &$database->exportConfigCI();

Fine! It makes and exports the config for me. Note that I call it with return by reference. Now,it's the time to change it. I have to make a change in the user of DB,according to the type of user logged. So I build a hook of type post_controller_constructor.

Hook to change DB config

The problem: The reference is lost in the middle of process. No matter how I try,I can't change the values of CI->db object(the object that holds the database attributes). By polymorphism(how I show in the hook) I can do that,but only to ActiveRecord. The others ORM's are based on database.php file. Here I need of return by reference,to change all dependencies linked at database config file.

What I'm missing? It's possible do what I want?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire