My typical development pattern is:
1 - local work machine
2 - online testing server
3 - production server.
Each of these has a database with unique connection data.
So for my projects I use a config file that implements a switch( what_environment ){ // assign appropriate db connection info } that assigns the proper db connection info (host, username, password, db name, ...) depending on the environment.
However in codeigniter's /config/database.php, there is no such construct, but rather just one set of properties. Obviously I can put my own server-sniffing switch statement in there. But I am presuming that there is a codeigniter way of handling this.
Can anyone shed light on the proper codeigniter way of maintaining multiple db connection infos?
On your application/config/constants.php write:
define('ENVIRONMENT', 'development');
switch(ENVIRONMENT):
case 'development':
# DB
defined('DB_HOST') ? null : define('DB_HOST', 'localhost');
defined('DB_USER') ? null : define('DB_USER', 'root');
defined('DB_PASSWORD') ? null : define('DB_PASSWORD', '');
defined('DB_NAME') ? null : define('DB_NAME', 'dev_db');
break;
case 'production':
# DB
defined('DB_HOST') ? null : define('DB_HOST', 'production');
defined('DB_USER') ? null : define('DB_USER', 'production_user');
defined('DB_PASSWORD') ? null : define('DB_PASSWORD', '12345');
defined('DB_NAME') ? null : define('DB_NAME', 'production_db');
break;
endswitch;
Then on your application/config/database.php
$db['default'] = array(
'hostname' => DB_HOST,
'username' => DB_USER,
'password' => DB_PASSWORD,
'database' => DB_NAME,
);
I do not know if this is the best solution, however, it is the simplest to implement and always worked very well for me:
After the declaration of $db['default']:
$db['default'] = array(
'dsn' => '',
'hostname' => 'localhost',
'username' => '...',
'password' => '...',
...
);
I add this code:
if (ENVIRONMENT == 'development'){
$db['default']['username'] = 'root';
$db['default']['password'] = 'XXXXX';
$db['default']['database'] = 'developmentdatabasename';
}
In the index.php of your project:
define('ENVIRONMENT', isset($_SERVER['CI_ENV']) ? $_SERVER['CI_ENV'] : 'development');
if you use apache - activate SetEnvIf (if it itsn't already)
after that put in your htaccess
SetEnvIf Host localhost$ CI_ENV=development
SetEnvIf Host testhost$ CI_ENV=testing
SetEnvIf Host productionhost$ CI_ENV=production
now just simply create in your application/config/ folder 3 folder named development, testing and production
and if you put now your different config files in this folders - ci accesses it depending on your current working environment
please follow the CI Documentation for further Information
At your config/database.php you can set multiple environment like this
$active_group = 'local';
if(ENVIRONMENT=='development')
{
$active_group = 'online';
}
elseif(ENVIRONMENT=='production')
{
$active_group = 'production';
}
$db['local'] = array(
'dsn' => '',
'hostname' => 'xxxx',
'username' => '',
//other configs
);
$db['online'] = array(
'dsn' => '',
//other configs
);
$db['production'] = array(
'dsn' => '',
//other configs
);
You can do it lots of way.See documentation
Related
I have set up environment on my codeigniter project as development, production.
I have used this code.
switch($_SERVER["HTTP_HOST"])
{
case "localhost":
define('ENVIRONMENT', 'development');
break;
default:
define('ENVIRONMENT', 'production');
break;
}
Now i have to set different database connection for development and production.
Please help.
Database connections information defined in application/config/database.php file.
you can apply condition on bases of ENVIRONMENT
Open application/config/database.php file.
$db['database2'] = array(
'hostname' => (ENVIRONMENT !== 'production') ? 'localhost' : 'live_host',
'username' => (ENVIRONMENT !== 'production') ? 'root' : 'live_user',
'password' => (ENVIRONMENT !== 'production') ? 'local_password' : 'live_password',
'database' => (ENVIRONMENT !== 'production') ? 'local_db' : 'live_db',
'dbdriver' => 'mysqli',
.......
);
May it helps, Thanks
I am trying to update my database schema without passing from symfony structure.
php bin/console doctrine:schema:update --force
because am using doctrine inside a custom php application
i have a static function that return the entity manager :
$paths = array("services/Entity");
$isDevMode = true;
// the connection configuration
$dbParams = array(
'host' => '',
'port' => '',
'driver' => '',
'user' => '',
'password' => '',
'dbname' => '',
'charset' => 'UTF8',
);
$config = \Doctrine\ORM\Tools\Setup::createConfiguration($isDevMode,"cache/Proxies");
$driver = new \Doctrine\ORM\Mapping\Driver\AnnotationDriver(new \Doctrine\Common\Annotations\AnnotationReader(), $paths);
// registering noop annotation autoloader - allow all annotations by default
\Doctrine\Common\Annotations\AnnotationRegistry::registerLoader('class_exists');
$config->setMetadataDriverImpl($driver);
//$entityManager = \Doctrine\ORM\EntityManager::create($dbParams, $config);
$entityManager = \Doctrine\ORM\EntityManager::create($dbParams, $config);
return $entityManager;
I am using annotations in my entities.
thanks
You could find the command
$ vendor/bin/doctrine orm:schema-tool:update --force --dump-sql
in the doctrine's getting started guide.
I am setting a cookie like so:
$cookie = array(
'name' => 'test',
'value' => 'test',
'expire' => 86500,
'domain' => '.iwantaspeaker.com',
'path' => '/'
//'secure' => TRUE
);
$this->input->set_cookie($cookie);
var_dump($this->input->cookie('test', false));
Which returns bool(false)
I get absolutely no response. I have in the config:
$config['cookie_prefix'] = "iwas_";
And the cookie is stored as iwas_test, so I have also tried $this->input->cookie("iwas_test",true); to no avail.
I can also see that the cookie is set in chrome:
Furthermore, I have also tried using the cookie helper. What must I do to retrieve the cookie correctly? the URL is on a local network machine, so the domain is pointed to the local IP with an entry in my hosts file, if this makes any difference.
ah-ha! make sure you dont pass expire as a string
$cookie = array(
'name' => 'test',
'value' => 'test',
'expire' => 86500, <--
'domain' => 'www.iwantaspeaker.com',
'path' => '/',
'secure' => TRUE <-- will only be set on https
);
// $this->ci->db->insert("UserCookies", array("CookieUserEmail"=>$userEmail, "CookieRandom"=>$randomString));
$this->input->set_cookie($cookie);
var_dump($this->input->cookie('iwas_test', false));
I'm trying out cookies for first time in CI. Cant get it to work. Here's my code:
class Site extends CI_Controller {
public function index(){
$this->load->view('input_view');
$this->load->helper('cookie');
$this->test();
}
public function test(){
$cookie = array(
'name' => 'Test',
'value' => 'The Value',
'expire' => '86500',
//'domain' => '.some-domain.com',
'path' => '/',
'prefix' => 'myprefix_',
'secure' => TRUE
);
$this->input->set_cookie($cookie);
var_dump(get_cookie('Test'));
}
var_dump... returns false. I understand it does this the first time but should'nt it be true after reloading the page? I can't see any "Test" cookie in my web tools in Chrome either. Do I need the "domain" row? I googled some and if I understood it was optional? If it isn't, what am i suppose to write there? Localhost? In my config file i have this settings for cookies
$config['cookie_prefix'] = "";
$config['cookie_domain'] = "";
$config['cookie_path'] = "/";
$config['cookie_secure'] = FALSE;
I'm guessing you're not running https for development.
The secure flag on your cookie forces the cookie to be sent over HTTPS only;
Secure:
If this cookie can ONLY be sent over HTTPS (SSL), set this value to true.
Set it to false, and the cookie should work.
Basically I just want to connect to the same host but a different database in a Joomla module. So how can i get the user/pass info from the config to avoid the need to hard code or parametrize that info, since its already there.
Thanks
You can use this code ( it's the same code that's in the JFactory::getDBO() method ).
jimport('joomla.database.database');
jimport('joomla.database.table');
$conf = JFactory::getConfig();
$host = $conf->get('host');
$user = $conf->get('user');
$password = $conf->get('password');
$database = 'YOUR_DATABASE_NAME';
$prefix = $conf->get('dbprefix'); //***Change this if the dbprefix is not the same!***
$driver = $conf->get('dbtype');
$options = array ('driver' => $driver, 'host' => $host, 'user' => $user, 'password' => $password, 'database' => $database, 'prefix' => $prefix);
$db = JDatabase::getInstance($options);
I hope it helped!
Get config values like this:
$app = JFactory::getApplication();
echo $app->getCfg('user');
echo $app->getCfg('password');