user198003
user198003

Reputation: 11151

How to use CakePHP's database.php file for custom scripts?

Can you help me how to use class defined in CakePHP's /config/database.php file, for my custom scripts? I need to use defined array for db connection.

Tnx in adv!

Upvotes: 1

Views: 853

Answers (2)

Chuck Burgess
Chuck Burgess

Reputation: 11574

There is nothing magic about this class. It works the same as any other PHP class.

<?php

include("path/to/cake/config/database.php");
$db = new DATABASE_CONFIG;

echo $db->default['login'];
echo $db->default['password'];
echo $db->default['database'];

?>

Now you can reference the variables like you would with any other class.

Upvotes: 1

Rob Wilkerson
Rob Wilkerson

Reputation: 41236

It sounds like you're looking for something similar to Rails' migrations, i.e. scripts that run within the context of the framework. There have been efforts to create such a thing (see Joel Moss' article in the Bakery).

What I tend to do is to keep a set of DDL scripts in a _meta/ directory of my project. I execute these directly on the database so they have no need to read info from database.php. If you're set on using Cake's runtime context, my way won't help you, but maybe Joel's will.

Upvotes: 0

Related Questions