How does DataSource test in cakephp 2 - php

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');

Related

CakePHP on AWS Bitnami LAMP server can't connect to database

I am just getting into PHP and I'm trying to get a server started and go through CakePHP's CMS tutorial. However I cannot get a database connection. I get the error: CakePHP is NOT able to connect to the database.
Connection to database could not be established: SQLSTATE[HY000] [2054] The server requested authentication method unknown to the client
I have been trying to edit my app/config/app.php file. This is what I have:
'Datasources' => [
'default' => [
'className' => Connection::class,
'driver' => Mysql::class,
'persistent' => false,
'host' => 'localhost',
'port' => '3306',
'username' => 'root',
'password' => 'THE DEFAULT PASSWORD I USED TO GET INTO phpMyAdmin',
'database' => 'cake_cms',
'unix_socket' => '/opt/bitnami/mysql/tmp/mysql.sock',
//'encoding' => 'utf8mb4',
'timezone' => 'UTC',
'flags' => [],
'cacheMetadata' => true,
'log' => false,
'quoteIdentifiers' => false,
//'init' => ['SET GLOBAL innodb_stats_on_metadata = 0'],
'url' => env('DATABASE_URL', null),
],
'test' => [
'className' => Connection::class,
'driver' => Mysql::class,
'persistent' => false,
'host' => 'localhost',
'port' => '3306',
'username' => 'root',
'password' => 'THE DEFAULT PASSWORD I USED TO GET INTO phpMyAdmin',
'database' => 'cake_cms',
'unix_socket' => '/opt/bitnami/mysql/tmp/mysql.sock',
//'encoding' => 'utf8mb4',
'timezone' => 'UTC',
'cacheMetadata' => true,
'quoteIdentifiers' => false,
'log' => false,
//'init' => ['SET GLOBAL innodb_stats_on_metadata = 0'],
'url' => env('DATABASE_TEST_URL', null),
],
],
Thanks in advance. I'm super new, so maybe I need to provide more information.

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

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

Cakephp 2.3.7 Url Wiriting Not Properly Config/ Undefined Index: Datasource

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' => '',
);

Cakephp 1.3 appfog database configuration

i just moving my blog to appfog. I am currently using cakephp 1.3.x. I know I need to upgrade, I am already working on it. But in the meantime, I would like to get my blog working. I am not able to configure the database file.
I know we need to add
$services_json = json_decode(getenv('VCAP_SERVICES'),true);
$af_mysql_config = $services_json['mysql-5.1'][0]['credentials'];
// Database settings
Configure::write('Database.config', array(
'default' => array(
'datasource' => 'Database/Mysql',
'persistent' => false,
'host' => $af_mysql_config['hostname'],
'login' => $af_mysql_config['username'],
'password' => $af_mysql_config['password'],
'database' => $af_mysql_config['name'],
'prefix' => '',
'encoding' => 'utf8',
)
));
I just want to know how we can send the setting to database config.
I appreciate any help.
I just did this:
class DATABASE_CONFIG {
public $default = null;
function __construct() {
$services = getenv("VCAP_SERVICES");
$services_json = json_decode($services, true);
$mysql_config = $services_json["mysql-5.1"][0]["credentials"];
$this->default = array(
'driver' => 'mysql',
'persistent' => false,
'host' => $mysql_config["hostname"],
'login' => $mysql_config["user"],
'password' => $mysql_config["password"],
'database' => $mysql_config["name"],
'prefix' => '',
'port' => $mysql_config["port"],
);
}
}

CakePHP question: Can't connect to database, what can be the reason?

First line of the error =>
Access denied for user 'user_user'#'real.superdnssite.com' (using password: YES) [CORE/cake/libs/model/datasources/dbo/dbo_mysql.p
database.php =>
var $default = array(
'driver' => 'mysql',
'persistent' => false,
'host' => 'a.b.c.d',
'login' => 'user_user',
'password' => 'password',
'database' => 'database_name',
'prefix' => '',
);
var $test = array(
'driver' => 'mysql',
'persistent' => false,
'host' => 'a.b.c.d',
'login' => 'user_user',
'password' => 'password',
'database' => 'database_name',
'prefix' => '',
);
What can be the reason for this ? Is it for wrong password or for no database or for ip address ?
Try to use :
'datasource' => 'Database/Mysql'

Categories