0

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:"";
    }




}
?>
Tim Lytle
  • 17,549
  • 10
  • 60
  • 91
JoeLoco
  • 2,116
  • 4
  • 31
  • 59

1 Answers1

0

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();
Tim Lytle
  • 17,549
  • 10
  • 60
  • 91
  • Tim look my code, the 2 comments on sync method is your sugestion but i get this error when uncommented: Catchable fatal error: Argument 1 passed to Zend_Config_Writer::setConfig() must be an instance of Zend_Config, array given – JoeLoco Sep 26 '11 at 20:34
  • The problem is not save the file , but put a nem content on self::$data variable – JoeLoco Sep 26 '11 at 20:41
  • @CrazyJoe I think I've seen that before, I'll update when I remember what it was. – Tim Lytle Sep 26 '11 at 23:12