views:

356

answers:

1

Is there any standard method of accessing the database in the bootstrap.php file with CakePHP?

Specifically I want to set "putenv()" to a time zone that's stored in the database. Is there another way of achieving the same thing that I should be using instead?

Thanks.

+1  A: 

I don't think it's a good idea to access database in bootstrap. You cannot use models because they haven't yet been initialized. I think that you could extract the connection data and initialize connection and run queries using PHP's mysql_* but that's an ugly thing.

However if you need to run certain action everytime your app is accessed I would suggest placing it in AppController constructor (__construct function).

class AppController extends Controller {
    public function __construct() {
        // do your magic here

        // call parent constructor
        parent :: __constructor();
    }
}

class YourSpecificController extends AppController {
    public function __construct() {
         // call parent contructor (this) will cause your magic happen
         parent :: __constructor();

         // extra controller initialization instructions
    }
}

If you don't declare constructor in extending class you won't even have to change anything since PHP will automatically call parent (AppController) constructor.

RaYell
Thank you for your answer. Works great.