2012-02-16 4 views
4

Zend_Configメソッド__set($ name、$ value)を上書きしたいのですが、同じ問題があります。Zend_Configを上書きして親ノードにアクセスする

$名 - 例えば、上書き設定値の現在のキーを返します。

$this->config->something->other->more = 'crazy variable'; // $name in __set() will return 'more' 

設定内のすべてのノードが新しいのZend_Config()クラスですので。

どのように上書きされた__set()metodから親ノード名にアクセスできますか?

マイアプリケーション: 私は、私が同じ他の設定変数で指定したい、のツリーアレイのコントローラに同じ設定値を上書きする必要がありますが、overwrittingについてcontrollを持つこと、および他の設定変数を上書きすることはできません。許可された設定キーを上書きします。

答えて

3

建設中に$ allowModificationsをtrueに設定しない限り、Zend_Configは読み取り専用です。

$inifile = APPLICATION_PATH . '/configs/application.ini'; 
$section = 'production'; 
$allowModifications = true; 
$config = new Zend_Config_ini($inifile, $section, $allowModifications); 
$config->resources->db->params->username = 'test'; 
var_dump($config->resources->db->params->username); 

結果

文字列 'テスト'(長さ - :これは、あなたがこのような何かをする必要があることを意味

/** The $options parameter may be provided as either a boolean or an array. 
* If provided as a boolean, this sets the $allowModifications option of 
* Zend_Config. If provided as an array, there are three configuration 
* directives that may be set. For example: 
* 
* $options = array(
*  'allowModifications' => false, 
*  'nestSeparator'  => ':', 
*  'skipExtends'  => false, 
*  ); 
*/ 
public function __construct($filename, $section = null, $options = false) 

- :Zend_Config_Ini::__constructor()docblockから

= 4)

あなたは、単にこのような __construct()__set()方法Zend_Config_Iniをを拡張して上書きすることができ、その場合には

コメントに反応して

: -

class Application_Model_Config extends Zend_Config_Ini 
{ 
    private $allowed = array(); 

    public function __construct($filename, $section = null, $options = false) { 
     $this->allowed = array(
      'list', 
      'of', 
      'allowed', 
      'variables' 
     ); 
     parent::__construct($filename, $section, $options); 
    } 
    public function __set($name, $value) { 
     if(in_array($name, $this->allowed)){ 
      $this->_allowModifications = true; 
      parent::__set($name, $value); 
      $this->setReadOnly(); 
     } else { parent::__set($name, $value);} //will raise exception as expected. 
    } 
} 
+0

私はそれについてよく知っていますが、config内の変数全体に変更を許可したくないのですが、変数を指定するだけです。ですから、私はZend_Configをオーバーロードし、どの変数を上書きしたいのか手動でフィルタリングする必要があります。 – BlueMan

+0

その場合 – vascowhite

+0

ああ私の追加を参照してください、私は今あなたの問題を参照してください。返されるオブジェクトは、常にZend_Configです!私の2番目の解決策は動作しません。私は再び試してみる:) – vascowhite

0

は、必ず別の方法:)

$arrSettings = $oConfig->toArray(); 
$arrSettings['params']['dbname'] = 'new_value'; 
$oConfig= new Zend_Config($arrSettings); 
あり
関連する問題