Related
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');
I have created my database and edited my database in config file. I have filled database.config with the required details and my localhost/cakephp is also completely green but I dont know what is wrong. Below is my database coding in config.
<?php
class DATABASE_CONFIG {
public $test = array(
'datasource' => 'Database/Mysql',
'persistent' => false,
'host' => 'localhost',
'login' => 'user',
'password' => 'password',
'database' => 'test_database_name',
);
public $default = array(
'datasource' => 'Database/Mysql',
'persistent' => false,
'host' => 'localhost',
'login' => 'root',
'password' => '',
'database' => 'MyKitchen',
);
}
Remove (,) comma after array end , as display below,
public $test = array(
'datasource' => 'Database/Mysql',
'persistent' => false,
'host' => 'localhost',
'login' => 'user',
'password' => 'password',
'database' => 'test_database_name' // remove (,) comma
);
public $default = array(
'datasource' => 'Database/Mysql',
'persistent' => false,
'host' => 'localhost',
'login' => 'root',
'password' => '',
'database' => 'MyKitchen' // remove (,) comma
);
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
I am trying to spin up a application I downloaded from a repository. It is built with Cakephp 2.3.7. The first error I am getting is URL Writing is not properly configured on your server.I have checked my .htaccess files and everything is ok. I have check Apache and LoadModule rewrite_module modules/mod_rewrite.so is as such. Not to sure what else to check. The other notice I am getting is Undefined Index: Datasource. I went to my config file as it is as follows:
class DATABASE_CONFIG {
var $default = array(
'driver' => 'mysql',
'persistent' => false,
'host' => 'localhost',
'login' => 'root',
'password' => '',
'database' => 'icgw',
'prefix' => '',
);
var $array = array(
'datasource' => 'Array'
);
var $test = array(
'driver' => 'mysql',
'persistent' => false,
'host' => 'localhost',
'login' => 'user',
'password' => 'password',
'database' => 'test_database_name',
'prefix' => '',
);
}
That said I did try
var $array = array(
'datasource' => 'Database/MySql'
);
Unfortunately I had no luck. Any thoughts on this? Im not familiar with working in Cakephp 2.3.7. I use Cakephp 1.3.
I fixed the Undefined Index: Datasource error. This is what I did.
Instead of
var $default = array(
'driver' => 'mysql',
'persistent' => false,
'host' => 'localhost',
'login' => 'root',
'password' => '',
'database' => 'icgw',
'prefix' => '',
);
var $array = array(
'datasource' => 'Array'
);
I did this.
var $default = array(
'datasource' => 'Database/Mysql',
'driver' => 'mysql',
'persistent' => false,
'host' => 'localhost',
'login' => 'root',
'password' => '',
'database' => 'icgw',
'prefix' => '',
);
<?php
class DATABASE_CONFIG {
public $default = array(
'datasource' => 'Database/Mysql',
'persistent' => false,
'host' => 'hostname',
'login' => 'username',
'password' => 'password',
'database' => 'database_name',
'prefix' => '',
//'encoding' => 'utf8',
);
public $test = array(
'datasource' => 'Database/Mysql',
'persistent' => false,
'host' => 'hostname',
'login' => 'username',
'password' => 'password',
'database' => 'database_name',
'prefix' => '',
//'encoding' => 'utf8',
);
}
There are some default value in login,password and database. How can i write a script to edit the login,password and database ?
php file contains your example text:
kent$ awk '/login/{l++;if(l>1)gsub(/root/,"newLogin");}
/password/{p++;if(p>1)gsub(/xxx/,"newPwd");}
/database/{d++;if(d>1)gsub(/xxx/,"newDB");} 1' php
<?php
class DATABASE_CONFIG {
public $default = array(
'datasource' => 'Database/Mysql',
'persistent' => false,
'host' => 'localhost',
'login' => 'root',
'password' => 'xxx',
'database' => 'xxx',
'prefix' => '',
//'encoding' => 'utf8',
);
public $test = array(
'datasource' => 'Database/Mysql',
'persistent' => false,
'host' => 'localhost',
'login' => 'newLogin',
'password' => 'newPwd',
'database' => 'cake_test_db',
'prefix' => '',
//'encoding' => 'utf8',
);
}
this only changes the blocks after your $default block. and, only default value will be changed. e.g. in your $test, 'database' => 'cake_test_db' the value is not the default 'xxx', so it would not be changed.
if you like you can put new value in variable, and passing to awk with -v.
edit
if you want to change all values, (including $default block), it would be easier, check the lines below: (same as above, only defalut values (root, xxx,xxx) would be changed.
kent$ awk '/login/{gsub(/root/,"newLogin");}
/password/{gsub(/xxx/,"newPwd");}
/database/{gsub(/xxx/,"newDB");} 1' php
<?php
class DATABASE_CONFIG {
public $default = array(
'datasource' => 'Database/Mysql',
'persistent' => false,
'host' => 'localhost',
'login' => 'newLogin',
'password' => 'newPwd',
'database' => 'newDB',
'prefix' => '',
//'encoding' => 'utf8',
);
public $test = array(
'datasource' => 'Database/Mysql',
'persistent' => false,
'host' => 'localhost',
'login' => 'newLogin',
'password' => 'newPwd',
'database' => 'cake_test_db',
'prefix' => '',
//'encoding' => 'utf8',
);
}
edit2
this works for your new example in question.
sed '/login/{s/username/newLOGIN/}; /password/{s/password/newPWD/2}; /database/{s/database_name/newDB/}' php
<?php
class DATABASE_CONFIG {
public $default = array(
'datasource' => 'Database/Mysql',
'persistent' => false,
'host' => 'hostname',
'login' => 'newLOGIN',
'password' => 'newPWD',
'database' => 'newDB',
'prefix' => '',
//'encoding' => 'utf8',
);
public $test = array(
'datasource' => 'Database/Mysql',
'persistent' => false,
'host' => 'hostname',
'login' => 'newLOGIN',
'password' => 'newPWD',
'database' => 'newDB',
'prefix' => '',
//'encoding' => 'utf8',
);
}
#!/usr/bin/awk -f
BEGIN { FS="'"
usr[0] ="usr1" ; usr[1] = "user2"
pass[0] ="pass1" ; pass[1] = "pass2"
db[0] ="db1" ; db[1] = "db2"
}
{
if ($2 == "login") {
$0="\t'login' => '" usr[u] "',"
u++
}
if ($2 == "password") {
$0="\t'password' => '" pass[p] "',"
p++
}
if ($2 == "database") {
$0="\t'database' => '" db[d] "',"
d++
}
print
}