How to setup different database connection based on environment - codeigniter 3 - codeigniter

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

Related

Error ORA-12505: TNS:listener does not currently know of SID given in connect descriptor Laravel 5.8 Yajra

I get the following error Yajra\Pdo\Oci8\Exceptions\Oci8Exception ORA-12505: TNS:listener does not currently know of SID given in connect descriptor
What I want is to be able to connect to Laravel 5.8.38 to Oracle (remote), and I'm not sure how to do the setup using the service name
Sql Developer Configuration
'connections' => [
'oracle' => [
'driver' => 'oracle',
'host' => '192.168.0.190',
'port' => '1521',
'database' => 'BDDESARR',
'service_name' => '???',
'username' => 'PAT_GUZ',
'password' => 'ujUYjjdk',
'charset' => '',
'prefix' => '',
],
Installation of Yajra following the step by step from https://github.com/yajra/laravel-oci8/tree/5.8
Terminal output
PS C:\wamp64\www\desarrollo\php\laravel\miproyect> composer require yajra/laravel-oci8:"5.8.*"
./composer.json has been updated
Loading composer repositories with package information
Updating dependencies (including require-dev)
Nothing to install or update
Package jakub-onderka/php-console-color is abandoned, you should avoid using it. Use php-parallel-lint/php-console-color instead.
Package jakub-onderka/php-console-highlighter is abandoned, you should avoid using it. Use php-parallel-lint/php-console-highlighter instead.
Writing lock file
Generating optimized autoload files
File config/database.php
'default' => env('DB_CONNECTION', 'oracle'),
'connections' => [
'oracle' => [
'driver' => 'oracle',
'host' => '192.168.0.190',
'port' => '1521',
'database' => 'BDDESARR',
'service_name' => '???',
'username' => 'PAT_GUZ',
'password' => 'ujUYjjdk',
'charset' => '',
'prefix' => '',
],
Additional information, I made the following code and it runs successfully
<?php
$conn = oci_connect('PAT_GUZ', 'ujUYjjdk', '192.168.0.190/BDDESARR');
if (!$conn) {
$e = oci_error();
var_dump($e);
}
$stid = oci_parse($conn, 'SELECT * FROM MY_TABLE');
oci_execute($stid);
echo "<table border='1'>\n";
while ($row = oci_fetch_array($stid, OCI_ASSOC+OCI_RETURN_NULLS)) {
echo "<tr>\n";
foreach ($row as $item) {
echo " <td>" . ($item !== null ? htmlentities($item, ENT_QUOTES) : "") . "</td>\n";
}
echo "</tr>\n";
}
echo "</table>\n";
Laravel 5.8.38
PHP 7.3.12
Oracle: Oracle Database 11g Enterprise Edition Release 11.2.0.4.0 -
64bit Production
Windows 10 64 bit
Wamp64
I don't know anything about Laravel, Yajra and other things you mentioned, but this:
'database' => 'BDDESARR',
'service_name' => '???',
looks if not wrong then suspicious. SQL Developer connection suggests that Service name = 'bddesarr'. I don't know what "database" is supposed to be. SID, perhaps, as Oracle complains that SID isn't right.

Doctrine : How to update schema without symfony

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.

Codeigniter: What is the best practice to maintain multiple database connection datas?

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

How to set local environment in Laravel 4

I just want to set the local environment into Laravel 4.
In bootstrap/start.php I have:
$env = $app->detectEnvironment(array(
'local' => ['laravel.dev', ''],
));
I tried change local to development index in array, but nothing works.
I tried some tips of this page: http://laravel.com/docs/configuration... nothing.
I'm using artisan in console, that always say me:
**************************************
* Application In Production! *
**************************************
Do you really wish to run this command?
What I might do to teach Lara that I'm on local environment?
You may try this (In bootstrap/start.php file):
$env = $app->detectEnvironment(array(
'local' => ['*.dev', gethostname()],
'production' => ['*.com', '*.net', '*.org']
));
Also this is possible:
$env = $app->detectEnvironment(function() {
return gethostname() == 'your local machine name' ? 'local' : 'production';
});
Following on from #The Alpha's great answer - here's a slight modification using array to check for local machines (when you work from more than one location):
$env = $app->detectEnvironment(function() {
return in_array(
gethostname(),
[
'first local machine name',
'second local machine name'
]
) ?
'local' :
'production';
});
$env = $app->detectEnvironment(function() {
$substr = substr(gethostname(), "-4");
return ($substr == ".com" || $substr == ".net" || $substr == ".org") ? 'production' : 'local';
});

Installing ZFDebug toolbar on ZF 1.10+

I think it'll be good to have a ZFDebug tutorial in SO.
I was wondering if you could use ZFDebug toolbar with ZF 1.10+ (I'm actually using 1.11.2). I have the following code in my bootstrap but nothing seems to happen:
protected function _initZFDebug()
{
$autoloader = Zend_Loader_Autoloader::getInstance();
$autoloader->registerNamespace('ZFDebug');
if ('development' == APPLICATION_ENV) {
$options = array(
'jquery_path' => 'http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js',
'plugins' => array('Variables',
'Html',
'Database' => array(),
'File' => array('basePath' => APPLICATION_PATH . '/application'),
'Memory',
'Time',
'Registry',
//'Cache' => array('backend' => $cache->getBackend()),
'Exception')
);
$debug = new ZFDebug_Controller_Plugin_Debug($options);
$this->bootstrap('frontController');
$frontController = $this->getResource('frontController');
$frontController->registerPlugin($debug);
}
}
Make sure your APPLICATION_ENV is set to 'development'
Make sure to have a valid layout (with <head> and <body>)
the basePath option should be APPLICATION_PATH . '/../'

Resources