ZF-5643: Zend_Config_Writer_Xml creates illegal nodes if key is numeric
Description
If you try to create an XML config using a multidimensional array ( say from a database select ), and the keys are numeric, it generates an XML config that cannot be read into Zend_Config_Xml ( because it has illegal nodes ).
I believe a fix could be as such:
protected function addBranch(Zend_Config $config, SimpleXMLElement $xml) { foreach ($config as $key => $value) { if ($value instanceof Zend_Config) { if (is_numeric($key)) { $child = $xml->addChild('node' . $key); } else { $child = $xml->addChild($key); } $this->_addBranch($value, $child); } else { $xml->addChild($key, (string) $value); } } }
Comments
Posted by Ben Scholzen (dasprid) on 2009-03-09T03:47:18.000+0000
Adding prefixes is not a good solution, as the user doesn't expect it.
Actually, Zend_Config supports numeric arrays with any key-order, thus it allows you to fill in the database result without any problem. Zend_Config_Xml now supports numeric arrays as well, not with any key-order by repeating an element multiple times, which will result in an numeric array starting from 0 to X.
So we have those two options:
a) Zend_Config_Writer_Xml sees a numeric key and throws an exception. b) Zend_Config_Writer_Xml looks up the current array, and if it finds a numeric key, it ignores the key value itself and just creates the numeric array in XML, so that Zend_Config_Xml can read it. Tho this will loose the numeric indexes.
Any suggestions? Personally I'd use case b).
Posted by Christoph, René Pardon (renepardon) on 2009-05-22T00:41:53.000+0000
Hello,
I get the same error as described above:
Catchable fatal error: Object of class Zend_Config could not be converted to string in /usr/share/php/Zend/Config/Writer/Xml.php on line 182
I tried to overwrite my config.xml file with multidimensional array included like:
This is the code i used:
// $configarray = $config->toArray(); // $config = new Zend_Config($configarray,true); $config->newtry = array();
Thank you for your help.
greets
Posted by Christoph, René Pardon (renepardon) on 2009-05-22T01:04:34.000+0000
Hello again,
we (our company) has fixed the problem now. Here is the solution:
// FIXME Zend/Config/Writer/Xml.php line (182) $child = $parent->addChild($branchName -,(string) $value-);
Now it's possible to write childs with numeric keys and array values.
greets