doucai7294 2011-10-31 04:48
浏览 32
已采纳

Yii配置文件覆盖

I have a Yii project with a main.php config file and dev.php config file which "inherits" from it. The files are as follows:

main.php:

<?php

// uncomment the following to define a path alias
// Yii::setPathOfAlias('local','path/to/local-folder');
// This is the main Web application configuration. Any writable
// CWebApplication properties can be configured here.
return array(
    'basePath' => dirname( __FILE__ ) . DIRECTORY_SEPARATOR . '..',
    'name' => 'FeedStreem',
    // preloading 'log' component
    'preload' => array( 'log' ),
    // autoloading model and component classes
    'import' => array(
        'application.models.*',
        'application.components.*',
        'application.controllers.*',
    ),
    // application components
    'components' => array(
        'db' => array(
            'connectionString' => 'mysql:host=remote.host.com;dbname=dbnamehere',
            'emulatePrepare' => true,
            'username' => 'username',
            'password' => 'password',
            'charset' => 'utf8',
            /*'enableProfiling' => true*/
        ),
        'user' => array(
            // enable cookie-based authentication
            'allowAutoLogin' => true,
        ),
        'authManager' => array(
            'class' => 'CDbAuthManager',
            'connectionID' => 'db'
        ),
        'urlManager' => array(
            // omitted
        ),
        'errorHandler' => array(
            // use 'site/error' action to display errors
            'errorAction' => 'site/error',
        ),
        'log' => array(
            'class' => 'CLogRouter',
            'routes' => array(
                array(
                    'class' => 'CFileLogRoute',
                    'levels' => 'trace, info, error, warning',
                ),
            ),
        ),
    ),
    // application-level parameters that can be accessed
    // using Yii::app()->params['paramName']
    'params' => array(
        // this is used in contact page
        'adminEmail' => 'webmaster@example.com',
    ),
);

dev.php:

<?php

return CMap::mergeArray(
    require(dirname( __FILE__ ) . '/main.php'),
    array(
        'modules' => array(
            'gii' => array(
                'class' => 'system.gii.GiiModule',
                'password' => 'SECRET',
                // If removed, Gii defaults to localhost only. Edit carefully to taste.
                'ipFilters' => array( '127.0.0.1', '::1' ),
            ),
        ),
        'components' => array(
            'db' => array(
                'connectionString' => 'mysql:host=localhost;dbname=dbname2',
                'emulatePrepare' => true,
                'username' => 'username2',
                'password' => 'password2',
                'charset' => 'utf8',
            ),
            'log' => array(
                'class' => 'CLogRouter',
                'routes' => array(
                    array(
                        'class' => 'CFileLogRoute',
                        'levels' => 'trace, info, error, warning',
                    ),
                    // uncomment the following to show log messages on web pages
                    array(
                        'class' => 'CWebLogRoute',
                    ),
                ),
            ),
        ),
    )
);

However, when I use dev.php locally, I get the following error:

Warning: PDO::__construct() [pdo.--construct]: [2002] No connection could be made because the target machine actively  (trying to connect via tcp://remote.host.com:3306) in C:\web_workspace\lib\yii\framework\db\CDbConnection.php on line 405

Which tells me the dev.php did not overwrite that 'db' config option. How can I make a config file that inherits from main.php but can overwrite options when I merge it?

  • 写回答

7条回答 默认 最新

  • dongtaijiao7140 2011-10-31 13:27
    关注

    As far as i see from source code it should overwrite your config:

    public static function mergeArray($a,$b)
    {
        foreach($b as $k=>$v)
        {
            if(is_integer($k))
                isset($a[$k]) ? $a[]=$v : $a[$k]=$v;
            else if(is_array($v) && isset($a[$k]) && is_array($a[$k]))
                $a[$k]=self::mergeArray($a[$k],$v);
            else
                $a[$k]=$v;
        }
        return $a;
    }
    

    Source: http://code.google.com/p/yii/source/browse/tags/1.1.8/framework/collections/CMap.php

    Also official documentation says so:

    If each array has an element with the same string key value, the latter will overwrite the former (different from array_merge_recursive).

    Source: http://www.yiiframework.com/doc/api/1.1/CMap#mergeArray-detail

    Try to determine the problem via print_r the result array and look at it inner structure. I think the problem is here.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(6条)

报告相同问题?

悬赏问题

  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记