0

我在使用 appfog 服务配置 cakephp 数据库连接时遇到了一些问题。

AppFog 像这样通过 VCAP_SERVICES 变量提供 JSON 数据库配置

 {
    "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
            );
        }
    ); }

在像这样的 cakephp 数据库配置文件中

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

如何解决这个问题?

4

1 回答 1

2

您需要将 json 解析为一个数组(如果您只有一个 mysql db 绑定到应用程序,如果您有更多,则需要将“0”更改为另一个数字以反映第二个绑定的 mysql 服务):

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

以上将“用户名”更改为“用户”,基本上遵循appfog对Wordpress所做的前提:https ://github.com/appfog/af-php-wordpress/blob/master/wp-config.php

于 2012-10-10T22:44:44.127 回答