【问题标题】:How to config cakephp 2.2 database connection with appfog service如何使用 appfog 服务配置 cakephp 2.2 数据库连接
【发布时间】:2012-10-03 15:55:47
【问题描述】:

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

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

如何解决这个问题?

【问题讨论】:

    标签: php cakephp cakephp-2.0 appfog


    【解决方案1】:

    您需要将 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',
    );
    

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-06-17
      • 1970-01-01
      • 1970-01-01
      • 2014-01-14
      • 2020-10-01
      • 2022-12-17
      • 1970-01-01
      相关资源
      最近更新 更多