Kohana 3.3 Database::instance('name') not working - php

I have an issue with Kohana 3.3 and using different database configurations.
I have a config/database.php with 'default' config and 'other' like this:
return array
(
'default' => array
(
'type' => 'MySQL',
'connection' => array(
'hostname' => 'localhost',
'database' => 'database-one',
'username' => 'root',
'password' => 'password',
'persistent' => FALSE,
),
'table_prefix' => '',
'charset' => 'utf8',
'caching' => FALSE,
),
'other' => array
(
'type' => 'MySQL',
'connection' => array(
'hostname' => 'localhost',
'database' => 'database-two',
'username' => 'root',
'password' => 'password',
'persistent' => FALSE,
),
'table_prefix' => '',
'charset' => 'utf8',
'caching' => FALSE,
));
But in a Controller or Model when trying to use:
Database::instance('other');
Kohana will still use the 'default' configuration. What am I doing wrong?
Thanks!

If you would like to change currently used connection by kohana try this:
Database::$default = 'other';
From this line your code will use 'other' connection till you will switch it again to 'default' using same way.
You can also use another DB configuration once when executing the query in simple way:
DB::...->execute('other');
Or if you store your DB instance earlier:
$other = Database::instance('other');
DB::...->execute($other);
By ... I mean your query.

You need to store the connection in a variable, or the default connection will be used.
Documentation

Related

How does DataSource test in cakephp 2

We have custom DataSource and want to test that verifies the data after find method execute equals to one not customized.As I thought it, it would be possible if we can change Datasource in test code.I try many case, but I couldn't make it work.
public $default = [
'datasource' => 'CustomPostgres',
'persistent' => false,
'host' => 'localhost',
'port' => '5432',
'login' => 'hoge',
'password' => 'hogehoge',
'database' => 'prod',
'schema' => 'public',
'prefix' => '',
'encoding' => 'utf8'
];
public $test = [
'datasource' => 'CustomPostgres',
'persistent' => false,
'host' => 'localhost',
'port' => '5432',
'login' => 'hoge',
'password' => 'hogehoge',
'database' => 'test_prod',
'schema' => 'public',
'prefix' => '',
'encoding' => 'utf8'
];
public $old = [
'datasource' => 'Database/Postgres',
'persistent' => false,
'host' => 'localhost',
'port' => '5432',
'login' => 'hoge',
'password' => 'hogehoge',
'database' => 'dev',
'schema' => 'public',
'prefix' => '',
'encoding' => 'utf8'
];
public $test_old = [
'datasource' => 'Database/Postgres',
'persistent' => false,
'host' => 'localhost',
'port' => '5432',
'login' => 'hoge',
'password' => 'hogehoge',
'database' => 'test_dev',
'schema' => 'public',
'prefix' => '',
'encoding' => 'utf8'
];
//try1
$this->Model->setDataSource('test_old');
//try2
ConnectionManage::create('test_old',///);
//try3
////in fixture File and ClassRegistry:Init("Model")
$useDbConfig = "old";
////inner start::up
ClassRegistry:Init("Model");
//try4 extends model
ModelForOldSetting extends Model{
$useDbConfig = "old";
}
Above codes didn't work and it always emit error says
MissingConnectionException: Database connection "Postgres" is missing, or could not be created ,when running test however it works fine when setting default datasource Database/Postgres running local browser without test.
So, I'm really confused why Database connection "Postgres" is missing.Any idea makes me appreciate to them.
I finally found irritating and stupid irritating code by outer database.php file overrides datasources' setting.Following code works fine after appropriate settings.
ModelForOldSetting extends Model{
$useDbConfig = "dev";
$useTables = "models";
}
ClassRegistry:Init("ModelForOldSetting");
$model = $this->getMockForModel('ModelForOldSetting');

FuelPHP can't setup database to work

I'm using FuelPHP and I can't set up my database so it would work. I know that i'm using right information but i'm not sure if I put it in right place.
<?php
return array(
'default' => array(
'connection' => array(
'dsn' => 'mysql:host=modernt#moderntalking.lt;dbname=modernt',
'username' => 'modernt',
'password' => 'pass',
),
),
);
This is my db.php in APP/config/db.php
Environment configurations are merged, and the environment wins.
So if you have ./app/config/db.php containing return array('a'); and you'll have ./app/config/development/db.php containing return array('b');, then after reading your config, you'll end up with "b".
So if you use environment based config (like DB does by default), only add configuration to the global file that is truely global, otherwise it gets overwritten in the merge.
Check this
'dsn' => 'mysql:host=moderntalking.lt;dbname=modernt',
// a MySQL driver configuration
'development' => array(
'type' => 'mysqli',
'connection' => array(
'hostname' => 'localhost',
'port' => '3306',
'database' => 'fuel_db',
'username' => 'your_username',
'password' => 'y0uR_p#ssW0rd',
'persistent' => false,
'compress' => false,
),
'identifier' => '`',
'table_prefix' => '',
'charset' => 'utf8',
'enable_cache' => true,
'profiling' => false,
'readonly' => false,
),
// a PDO driver configuration, using PostgreSQL
'production' => array(
'type' => 'pdo',
'connection' => array(
'dsn' => 'pgsql:host=localhost;dbname=fuel_db',
'username' => 'your_username',
'password' => 'y0uR_p#ssW0rd',
'persistent' => false,
'compress' => false,
),
'identifier' => '"',
'table_prefix' => '',
'charset' => 'utf8',
'enable_cache' => true,
'profiling' => false,
'readonly' => array('slave1', 'slave2', 'slave3'),
),
'slave1' => array(
// configuration of the first production readonly slave db
),
'slave2' => array(
// configuration of the second production readonly slave db
),
'slave3' => array(
// configuration of the third production readonly slave db
),
Basic procedure of setup FuelPHP..may be its help you .Thank you
First, check your environment configuration in app/bootstrap.php:
/**
* Your environment. Can be set to any of the following:
*
* Fuel::DEVELOPMENT
* Fuel::TEST
* Fuel::STAGING
* Fuel::PRODUCTION
*/
Fuel::$env = (isset($_SERVER['FUEL_ENV']) ? $_SERVER['FUEL_ENV'] : Fuel::DEVELOPMENT);
Then modify the db.php file accordingly (app/development/db.php, app/production/db.php, app/staging/db.php, app/test/db.php)

laravel Undefined variable: host

My problem is that I get the following error in MySqlConnector.php.
Undefined variable: host error
I am using two different connections for both read/write. I merged them in config/database.php file depending on environment that system uses. Here is the mysql connection codes.
<?php
switch($_SERVER['LARAVEL_ENV']){
case 'production':
$connections = array(
'mysql' => array(
'read' => array(
'host' => '127.0.0.1',
),
'write' => array(
'host' => 'hosturl',
),
'driver' => 'mysql',
'database' => 'app_system',
'username' => 'username',
'password' => 'password',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
),
'mysql2' => array(
'read' => array(
'host' => '127.0.0.1',
),
'write' => array(
'host' => 'hosturl',
),
'driver' => 'mysql',
'database' => 'app_userdata',
'username' => 'username',
'password' => 'password',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
)
);
break;
case 'beta':
$connections = array(
'mysql' => array(
'read' => array(
'host' => '127.0.0.1',
),
'write' => array(
'host' => 'hosturl',
),
'driver' => 'mysql',
'database' => 'app_system',
'username' => 'username',
'password' => 'password',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
),
'mysql2' => array(
'read' => array(
'host' => '127.0.0.1',
),
'write' => array(
'host' => 'hosturl',
),
'driver' => 'mysql',
'database' => 'app_userdata',
'username' => 'username',
'password' => 'password',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
)
);
break;
case 'development':
$connections = array(
'mysql' => array(
'driver' => 'mysql',
'host' => 'localhost',
'database' => 'app_system',
'username' => 'root',
'password' => 'root',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
),
'mysql2' => array(
'driver' => 'mysql',
'host' => 'localhost',
'database' => 'app_userdata',
'username' => 'root',
'password' => 'root',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
)
);
break;
}
return array(
'fetch' => PDO::FETCH_CLASS,
'default' => 'mysql',
'connections' => $connections,
'migrations' => 'migrations',
'redis' => array(
'cluster' => true,
'default' => array(
'host' => '127.0.0.1',
'port' => 6379,
'database' => 0,
),
),
);
All I wanted to do is using a different host for reading and another one for writing. When I use one connection in localhost, I get no error. But in multiple connections, I get the error. What is the reason for the error?
Ok I found the problem. It was because of laravel version. I updated laravel 4.0 to 4.1 and problem was solved.
just for the record of your environment implementation.
You have a build in laravel the environmen detection and loading of specific conf files.
you can read here: http://laravel.com/docs/configuration#environment-configuration
but the general idea is to define your environments in the :
$env = $app->detectEnvironment(array(
'local' => array('your-local-machine-name'),
'production' => array('your--prod-machine-name'),
));
and than you go to config folder and create inside it a folder foreach environment you have.
create a production folder, and than each file you want to override simply copy paste it into the folder and edit the things you want to override.
implement this, and than check your errors back here again

Select array value by key name

Before I receive a hail of snark please believe me - I have googled and used SO search.
How do I select an array value by name?
If I wanted to call particular values (for a db connection) how would I do that here? The following is the database.php file in config folder for PHP based CMS site.
I'd like to create the connection for a mysqli query
$db_connection = #mysqli_connect(host,user,pass,database)
The code below exists in a separate file in the site's config folder config/database.php.
$config['default'] = array(
'benchmark' => TRUE,
'persistent' => FALSE,
'connection' => array(
'type' => 'mysqli',
'user' => 'myname',
'pass' => 'somepass123',
'host' => 'localhost',
'port' => FALSE,
'socket' => FALSE,
'database' => 'local_sitename',
),
'character_set' => 'utf8',
'table_prefix' => '',
'object' => TRUE,
'cache' => FALSE,
'escape' => TRUE
);
The internet, and my text book, are full of examples like this: http://www.homeandlearn.co.uk/php/php6p3.html
Where, for example, is a seasons array:
<?php
$seasons = array("Autumn", "Winter", "Spring", "Summer");
print $seasons[0];
?>
I know that I could select say Spring by using $seasons[2];
But using the array in my sites config file uses arrays within an array. I need something like (This will be syntactically wrong but I hope conveys what I need)
$db_connection = $config(connection(host)),$config(connection(user)),$config(connection(pass)),$config(connection(database))
How would I call these values?
$config['default'] = array(
'benchmark' => TRUE,
'persistent' => FALSE,
'connection' => array(
'type' => 'mysqli',
'user' => 'myname',
'pass' => 'somepass123',
'host' => 'localhost',
'port' => FALSE,
'socket' => FALSE,
'database' => 'local_sitename',
),
'character_set' => 'utf8',
'table_prefix' => '',
'object' => TRUE,
'cache' => FALSE,
'escape' => TRUE
);
echo $config['default']['connection']['user'] // prints myname
echo $config['default']['connection']['pass'] // prints pass
echo $config['default']['connection']['host'] // prints host
Use strings to index your array by key:
$config['default']['connection']['host'];// === 'localhost'

FuelPHP - php oil refine migrate PDO Error

I'm trying to setup my fuelphp on ubuntu12, nginx in a development environment.
Everything was working for me except when I try to do php oil refine migrate.
I was faced with the following error message:
Error - invalid data source name in COREPATH/classes/database/pdo/connection.php on line 94
My development/db.php:
return array(
'default' => array(
'connection' => array(
'dsn' => 'mysql:host=localhost;dbname=fuel_intro',
'username' => 'root',
'password' => '',
),
),
);
I searched the Internet and fuelphp docs, and still no luck.
Any help will be appreciated.
It seems you're doing database configuration incorrectly. It shouldn't be 'host:localhost', it should be like 'hostname'=>'localhost'. And please use mysql or PDO instead of mysql... (because mysql_* functions are deprecated.
It should be something like:
'default' => array(
'type' => 'mysqli',
'connection' => array(
'hostname' => 'localhost',
'port' => '3306',
'database' => 'fuel_db',
'username' => 'your_username',
'password' => 'y0uR_p#ssW0rd',
'persistent' => false,
'compress' => false,
),
'identifier' => '`',
'table_prefix' => '',
'charset' => 'utf8',
'enable_cache' => true,
'profiling' => false,
),
Take a look at:
http://fuelphp.com/docs/classes/database/introduction.html for further information.

Categories