tags:

views:

284

answers:

2

I need to retrieve values from CakePHP's config file database.php from one of my controllers.

I'm maintaining an ex-employee's code, so I'm not positive how much of this adheres to default structures. I'm hoping this is fairly straightforward, but I can't seem to Google the right info.

File: app/config/database.php

class DATABASE_CONFIG
{
    var $db1 =
        array('driver' => 'mysqli',
              'connect' => 'mysql_connect',
              'host' => 'localhost',
              'login' => 'username',
              'password' => 'password',
              'database' => 'schema',
              'prefix' => '');
}

File: app/controllers/my_controller.php

// here is where I need to retrieve
// the database login and password values

What syntax can I use here? Is it even possible to retrieve these values, or are they only accessible to the guts of the CakePHP framework?

+4  A: 
$fields = get_class_vars('DATABASE_CONFIG')

echo $fields['db1']['login'];
RaYell
This solution worked perfectly, no tweaking necessary. Much obliged!
DreadPirateShawn
+1  A: 

Well, I have to say the above answer is a much quicker and simpler method than what I've been using, but just for argument's sake:

 App::Import('ConnectionManager');
 $ds = ConnectionManager::getDataSource('default');
 $dsc = $ds->config;

 echo $dsc['host'];
 echo $dsc['login'];
 echo $dsc['password'];
 echo $dsc['database'];

I guess if anything this protects your code from a change in the name of the 'DATABASE_CONFIG' class.

ebotunes
That's the maintainable Cake way of doing it, very nice.
deceze
I tried this one later, but I kept getting an error that the "App" class was not included. I'm positive that -should- mean something to me, but we're back to inheriting the code and admittedly not understanding the structural requirements.
DreadPirateShawn