I have this constant value called CLIENT_URL
on boostrap:
$clienturl = explode('/', $_SERVER['REQUEST_URI']);
define("CLIENT_URL",$clienturl[2]);
I've debugged and it gets, for example, 'client1', or 'client2', etc.
I need to use that value when defining a configuration array, such as:
on database.php
class DATABASE_CONFIG {
var $default = array(
'datasource' => 'Database/Mysql',
'persistent' => false,
'host' => 'localhost',
'login' => '*****',
'password' => '****',
'database' => 'client3',
'prefix' => ''
);
var $clientdb = array(
'datasource' => 'Database/Mysql',
'persistent' => false,
'host' => 'localhost',
'login' => '****',
'password' => '****',
'database' => 'client3',
'prefix' => ''
);
public function __construct() {
debug('constant value: '.CLIENT_URL);
debug($this->default['database']);
$this->default['database'] = CLIENT_URL; // This line does nothing !!
//$this->clientdb['database'] = CLIENT_URL; // This lines prevent connection, error: Database connection "Mysql" is missing, or could not be created !!
$this->default = $this->clientdb;
debug($this->default['database']);
}
};
However, database remains unchanged, it keeps initial value from above.
Actually i can do:
$this->default['database'] = 'whatever';
and it won't even try to find 'whatever' database; it seems this assignment is missing some reload or something?
After the assignment on __construct,
I've debugged
debug($this->default['database']);
Before and after the assignment and it does not change.
It only changes doing this:
$this->clientdb['database'] = CLIENT_URL; // Instead of $this->default['database'] = CLIENT_URL;
But then connection fails.
Please note the app does connect to the database given at the very beginning ('client3'), it just don't switch.
I check database via a find, that always return same record from same database, no switch.
$options = array('conditions' => array('User.id' => 1));
$clientArray = $this->User->find('first', $options);
Thank you for any pointers.
You need to do like this
class DATABASE_CONFIG {
var $client1db = array(
'datasource' => 'Database/Mysql',
'persistent' => false,
'host' => 'localhost',
'login' => '*****',
'password' => '*****',
'database' => 'client1',
'prefix' => ''
);
public function __construct() {
$this->default = $this->clientdb;
$this->default['database'] = CLIENT_URL;
}
};
Related
I do not know what put in datasource. What does it mean by datasource? I am using hostinger for my server. Thank you.
<?php
class DATABASE_CONFIG {
public $default = array(
'datasource' => '',
'persistent' => false,
'host' => 'mysql.hostinger.jp',
'login' => 'u88_ana',
'password' => 'secret',
'database' => 'u88_vir',
'encoding' => 'utf8'
);
}
I'm trying to learn something new by going through CakePHP's tutorials and in the section where you link a database.php to your MAMP/SQL, it asks for you to change the information in an array calls public $default. However there is also a public $test array in the same file.
What is the public $test array for? I know I can run my page by just linking the $default but does the $test serve a purpose?
ie:
class DATABASE_CONFIG {
public $default = array(
'datasource' => 'Database/Mysql',
'persistent' => false,
'host' => 'localhost',
'login' => 'admin',
'password' => 'admin',
'database' => 'blog_tutorial',
'prefix' => '',
//'encoding' => 'utf8',
);
public $test = array(
'datasource' => 'Database/Mysql',
'persistent' => false,
'host' => 'localhost',
'login' => 'admin',
'password' => 'admin',
'database' => 'blog_tutorial',
'prefix' => '',
//'encoding' => 'utf8',
);
}
Just curious, thanks!
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"],
);
}
}
For some reason this is giving me the follow error: syntax error, unexpected T_VARIABLE:
$mysql = json_decode(getenv("VCAP_SERVICES"));
$mysql = $mysql["mysql-5.1"][0]["credentials"];
class DATABASE_CONFIG {
public $default = array(
'datasource' => 'Database/Mysql',
'persistent' => false,
'host' => 'localhost',
'port' => $mysql['port'], // <-- Line with error
'login' => $mysql['username'],
'password' => $mysql['password'],
'database' => $mysql['name'],
'prefix' => ''
//'encoding' => 'utf8',
);
public $test = array(
'datasource' => 'Database/Mysql',
'persistent' => false,
'host' => 'localhost',
'login' => 'user',
'password' => 'password',
'database' => 'test_database_name',
'prefix' => '',
//'encoding' => 'utf8',
);
}
I know you can use variables as values in arrays, so what is going on?
It looks like you're trying to set the default value of a property to a variable.
You can't do that, not even inside an array. This is half PHP's parser sucking, a quarter of PHP's lack of appropriate error message, and a bit of sanity.
You'll need to do it from within the constructor instead by passing in $mysql:
$config = new DATABASE_CONFIG($mysql);
class DATABASE_CONFIG {
public $default = array(
'datasource' => 'Database/Mysql',
'persistent' => false,
'host' => 'localhost',
'port' => null,
'login' => null,
'password' => null,
'database' => null,
'prefix' => ''
//'encoding' => 'utf8',
);
public function __construct($mysql) {
$this->default['port'] = $mysql['port']; // etc
}
}
$mysql has no scope inside the class.
You need to inject it as an argument into the class constructor, and then define your array values for the class properties
You're trying to reference a variable inside a class which is defined outside of that class.
PHP has no idea what $mysql is inside of that class definition.
I'have some problem to config cakephp database connection with appfog service.
AppFog provides JSON database config by VCAP_SERVICES variable like this
{
"mysql-5.1" = (
{
credentials = {
host = "ap01-user01.c0ye1hvnkw6z.ap-southeast-1.rds.amazonaws.com";
hostname = "ap01-user01.c0ye1hvnkw6z.ap-southeast-1.rds.amazonaws.com";
name = ?????????????;
password = ????;
port = 3306;
user = ?????;
username = ???????????;
};
label = "mysql-5.1";
name = "???????-mysql-56200";
plan = free;
tags = (
mysql,
"mysql-5.1",
relational
);
}
); }
In cakephp database config file like this
public $default = array(
'datasource' => 'Database/Mysql',
'persistent' => false,
'host' => $mysql_config["hostname"],
'login' => $mysql_config["username"],
'password' => $mysql_config["password"],
'database' => $mysql_config["name"],
'prefix' => '',
//'encoding' => 'utf8',
);
How to fix this problem?
You will want to parse the json into an array (if you only have ONE mysql db bound to the app, if you have more you will need to change the "0" to another number to reflect the second bound mysql service):
$services = getenv("VCAP_SERVICES");
$services_json = json_decode($services,true);
$mysql_config = $services_json["mysql-5.1"][0]["credentials"];
public $default = array(
'datasource' => 'Database/Mysql',
'persistent' => false,
'host' => $mysql_config["hostname"],
'login' => $mysql_config["user"],
'password' => $mysql_config["password"],
'database' => $mysql_config["name"],
'port' => $mysql_config["port"],
'prefix' => '',
//'encoding' => 'utf8',
);
The above changes "username" to "user" and basically follows the premise of what appfog does with Wordpress: https://github.com/appfog/af-php-wordpress/blob/master/wp-config.php