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
Related
How can I solve this error?
At the time of cake bake.
This is my database configuration:
class DATABASE_CONFIG {
public $default = array(
'datasource' => 'Database/Mysql',
'persistent' => false,
'host' => 'localhost',
'login' => 'root',
'password' => '',
'database' => 'cakephp2.9',
'prefix' => '',
//'encoding' => 'utf8',
);
}
This is my database configuration class DATABASE_CONFIG { public $default = array( 'datasource' => 'Database/Mysql', 'persistent' => false, 'host' => 'localhost', 'login' => 'root', 'password' => '', 'database' => 'cakephp2.9', 'prefix' => '', //'encoding' => 'utf8', ); }
CakePHP currently doesn't support database names with dots, as the routine that handles quoting names is being used for column names as well, in which dots are treated as separators for aliases and columns, ie for stuff like TableAlias.column_name, which is being converted to `TableAlias`.`column_name`, causing your database name to make it into the query as `cakephp2`.`9` which will of course fail.
Long story short, rename your database.
In my app/Config/database.php I have a setup which looks like this,
public $default = array(
'datasource' => 'Database/Mysql',
'persistent' => false,
'host' => 'localhost',
'login' => 'root',
'password' => '',
'database' => 'default_db',
'prefix' => '',
//'encoding' => 'utf8',
);
public $second = array(
'datasource' => 'Database/Mysql',
'persistent' => false,
'host' => 'localhost',
'login' => 'root',
'password' => '',
'database' => 'second_db',
'prefix' => '',
//'encoding' => 'utf8',
);
Now what I wanted was to simultaneously save the data, wherever and whenever I am saving any kind of data. This databases are identical, that is why I want to save the data to both databases to maintain them being identical. Where would I edit such that all the save/edit/update/delete processes will both save to both databases. Thank you in advance.
EDIT
in my lib/Cake/Model/Model.php is a line of code which looks like this
/**
* The name of the DataSource connection that this Model uses
*
* The value must be an attribute name that you defined in `app/Config/database.php`
* or created using `ConnectionManager::create()`.
*
* #var string
* #link http://book.cakephp.org/2.0/en/models/model-attributes.html#usedbconfig
*/
public $useDbConfig = 'default';
is there where I should start editing to be able to save the data into two databases? Thanks.
Doing this through CakePHP is slow and not necessary, why aren't you using a feature like DB replication? However, you can change the connection the model us using through the model property useDbConfig.
$this->save($data);
$this->useDbConfig = 'second';
$this->save($data, array('callbacks' => false, 'validate' => false);
$this->useDbConfig = 'default';
You could add the last three lines of the above code to your Model::afterSave() callback, or much better, do it via a behavior and attach it to each model that needs it.
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;
}
};
I am writing a PHP application using the Silex framework. I'm using the Doctrine Service Provider, and I can open a connection normally as this:
$app->register(new Silex\Provider\DoctrineServiceProvider(), array(
'dbs.options' => array (
'localhost' => array(
'driver' => 'pdo_mysql',
'host' => 'localhost',
'dbname' => 'test',
'user' => 'root',
'password' => 'root',
'charset' => 'utf8',
)
),
));
That works perfectly. What I want now is to add another database connection afterwards in my code. I know I can do it adding another element to dbs.options, but I want to do it afterwards, in the controllers (as different controllers will use different database connections).
Is that possible? I guess I could use something like DriverManager::getConnection($options, $config, $manager); but there's probably a better way to do it.
Thanks!
$conn = DriverManager::getConnection($params, $config);
this is original code to generate new connection, so what you wrote is ok
Link: http://docs.doctrine-project.org/projects/doctrine-dbal/en/latest/reference/data-retrieval-and-manipulation.html
You can configure multiple db connections using the DoctrineServiceProvider bundled with Silex.
Replace the db.options with an array of configurations where keys are connection names and values configuration options.
$app->register(new Silex\Provider\DoctrineServiceProvider(), array(
'dbs.options' => array (
'mysql_read' => array(
'driver' => 'pdo_mysql',
'host' => 'mysql_read.someplace.tld',
'dbname' => 'my_database',
'user' => 'my_username',
'password' => 'my_password',
'charset' => 'utf8',
),
'mysql_write' => array(
'driver' => 'pdo_mysql',
'host' => 'mysql_write.someplace.tld',
'dbname' => 'my_database',
'user' => 'my_username',
'password' => 'my_password',
'charset' => 'utf8',
),
),
));
Access multiple connections in your controllers:
$app->get('/blog/{id}', function ($id) use ($app) {
$sql = "SELECT * FROM posts WHERE id = ?";
$post = $app['dbs']['mysql_read']->fetchAssoc($sql, array((int) $id));
$sql = "UPDATE posts SET value = ? WHERE id = ?";
$app['dbs']['mysql_write']->executeUpdate($sql, array('newValue', (int) $id));
return "<h1>{$post['title']}</h1>".
"<p>{$post['body']}</p>";
});
Source: http://silex.sensiolabs.org/doc/providers/doctrine.html
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"],
);
}
}