Welcome Guest, Not a member yet? Register   Sign In
On the fly database setup
#1

[eluser]mscahill[/eluser]
I'm attempting to set up my CI application to install itself on first run ala Wordpress. I'm having trouble trying to coneptualize a way of setting the database configuration on the fly during the installation process. Any ideas?
#2

[eluser]mscahill[/eluser]
Hello? Is anybody out there?
#3

[eluser]obiron2[/eluser]
how about checking for the database connection and if it fails on database server found but database not found, redirect to a controller/function that creates the database and the relevant tables.

If you are using MySQL, you may even be able to call a shell script to create the database

Obiron
#4

[eluser]mscahill[/eluser]
I already have the installation function in place, but I need a way to set the actual connection details (host, username, password) in config/database.php
#5

[eluser]mscahill[/eluser]
So is this impossible?
#6

[eluser]Rob W[/eluser]
Would the following work?

* Distribute the app with some specific values in the DB config ('demo_username' for example)
* Find & replace these values with the new ones
* Save the file

Code:
// Open the db config file - mode = read + write from beginning
$dbconfig_file = fopen('path/to/file', 'r+');
$dbconfig_text = fread($dbconfig_file);
// build arrays for str_replace to use
$find = array('demo_username') // values to find
$replace = array('real_username'); // values to replace, need to be in same position as those in $find
// Make the replacements
$new_dbconfig_text = str_replace($find, $replace, $dbconfig_text);
// rewrite the file
fwrite($dbconfig_file, $new_dbconfig_text);
// Close the handler
fclose($dbconfig_file);

Obviously with better error checking - and you'll need to be very careful about validation on the provided options, otherwise somebody could inject some nasty raw PHP code into your config file.

Hope that helps...

-R
#7

[eluser]mscahill[/eluser]
How would you even be able to trap the connection error on autoload, though?
#8

[eluser]mscahill[/eluser]
Still haven't found a solution to this yet. Do I really have to hack the core to trap the first run database error? I need to be able to set up host, username, and password in my installation controller. Any ideas?
#9

[eluser]mkhairul[/eluser]
The best example right now might be Derek's Bamboo Invoice. Check it out.
#10

[eluser]Tom Schlick[/eluser]
you could always load the db values from the db Smile haha jk




Theme © iAndrew 2016 - Forum software by © MyBB