简体   繁体   中英

Zend_Config_ini add a section and(or) key on fly?

I have an ini file for each client on my system, and add a new section and key to be created on the fly.

I need some think like this:

Current ini file:

[section_a]
key_a=1
key_b-2

And need to change this (with a php/zend) code, to that:

[section_a]
key_a =1
key_b = 2
[section_b]
key_a = 1

Need to add a new section named section_b whith a new key named key_a , but i don't find any mthod on Zend_Ini_Config like "$ini->add('section_b','key_a')".

Obs:

Php "magic" like $ini->$new_prop->$new_prop = "1" , dont work to!

Any Help!!

Update

<?php
class SystemConfigHelper
{
    public static $data;

    public static function load()
    {
        if(!defined("ACCOUNT_ID"))
            return true;

        try
        {
            $url = explode('.', $_SERVER['HTTP_HOST']);
            self::$data = new Zend_Config_Ini(ACCOUNTS_PATH . "/" . ACCOUNT_ID . "/system-config.ini",null,array("allowModifications" => true));
            return true;
        }
        catch(Exception $e)
        {
            die($e->getMessage());
            return false;
        }
    }

    public static function save()
    {
        try
        {
            $url = explode('.', $_SERVER['HTTP_HOST']);
            $writer = new Zend_Config_Writer_Ini(array('config'   => self::$data,
                                       'filename' => ACCOUNTS_PATH . "/" . ACCOUNT_ID . "/system-config.ini"));
            $writer->write();

        }
        catch(Exception $e)
        {
            die($e->getMessage());
            return false;
        }

    }

    public static function getParam($section,$key)
    {
        return self::$data->$section->$key;
    }

    public static function sync($data)
    {
        //self::$data = $data;
        //return;

        foreach(self::$data as $section => $param)
        {
            foreach($param as $key => $value)
            {
               self::$data->$section->$key = $data[$section][$key];
            }
        }

    }

    public static function getParamAs($section,$key,$as)
    {
        return self::$data->$section->$key==1?$as:"";
    }




}
?>

Have you read the docs on Zend_Config_Writer ? Assuming you've loaded the entire config file (and not just a section) you should be able to do this (pulled right from the docs):

$writer = new Zend_Config_Writer_Ini(array('config'   => $config,
                                       'filename' => 'config.ini'));
$writer->write();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM