我有一个 Yii 项目,其中有一个 main.php 配置文件和一个从它“继承”的 dev.php 配置文件。文件如下:
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' => '<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="027567606f637176677042677a636f726e672c616d6f" rel="noreferrer noopener nofollow">[email protected]</a>',
),
);
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',
),
),
),
),
)
);
但是,当我在本地使用 dev.php 时,出现以下错误:
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
这告诉我 dev.php 没有覆盖“db”配置选项。如何制作一个继承自 main.php 但在合并时可以覆盖选项的配置文件?
最佳答案
据我从源代码中看到,它应该覆盖您的配置:
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;
}
来源:http://code.google.com/p/yii/source/browse/tags/1.1.8/framework/collections/CMap.php
官方文档也这么说:
block 引用>If each array has an element with the same string key value, the latter will overwrite the former (different from array_merge_recursive).
来源:http://www.yiiframework.com/doc/api/1.1/CMap#mergeArray-detail
尝试通过 print_r 结果数组来确定问题并查看其内部结构。我想问题就出在这里。
关于php - Yii 配置文件覆盖,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7950089/