Zend Framework

Zend_Config_Writer_Xml creates illegal nodes if key is numeric

Details

  • Type: Bug Bug
  • Status: Resolved Resolved
  • Priority: Critical Critical
  • Resolution: Fixed
  • Affects Version/s: 1.7.3
  • Fix Version/s: 1.7.7
  • Component/s: Zend_Config_Writer
  • Labels:
    None

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); }
}
}

Activity

Hide
Ben Scholzen added a comment -

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).

Show
Ben Scholzen added a comment - 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).
Hide
Christoph, René Pardon added a comment -

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:

<zend-config>
<notification>
<adress name="firstname lastname" mail="mail@domain.de" />
<adress name="firstname2 lastname2" mail="mail2@domain.de" />
</notification>
</zend-config>

This is the code i used:

$pathtoconfig = realpath(dirname(_FILE_) . '/../../');
$config = new Zend_Config_Xml($pathtoconfig . '/config.xml', null, array('skipExtends' => true, 'allowModifications' => true));
// $configarray = $config->toArray();
// $config = new Zend_Config($configarray,true);
$config->newtry = array();

$config->setExtend('newtry', 'default');

$config->newtry->templateid = 2;
$config->newtry->layout = 'lachmichwech';

$config->newtry->sendmail = array();
$config->newtry->sendmail->reseller = array();

$config->newtry->sendmail->reseller->adress = array('name' => 'firstname lastname, 'mail' => 'mail@domain.net');

$writer = new Zend_Config_Writer_Xml();
$writer->setConfig($config);
$writer->setFilename($pathtoconfig . '/test.xml');

$writer->write();

Thank you for your help.

greets

Show
Christoph, René Pardon added a comment - 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: <zend-config> <notification> <adress name="firstname lastname" mail="mail@domain.de" /> <adress name="firstname2 lastname2" mail="mail2@domain.de" /> </notification> </zend-config> This is the code i used: $pathtoconfig = realpath(dirname(_FILE_) . '/../../'); $config = new Zend_Config_Xml($pathtoconfig . '/config.xml', null, array('skipExtends' => true, 'allowModifications' => true)); // $configarray = $config->toArray(); // $config = new Zend_Config($configarray,true); $config->newtry = array(); $config->setExtend('newtry', 'default'); $config->newtry->templateid = 2; $config->newtry->layout = 'lachmichwech'; $config->newtry->sendmail = array(); $config->newtry->sendmail->reseller = array(); $config->newtry->sendmail->reseller->adress = array('name' => 'firstname lastname, 'mail' => 'mail@domain.net'); $writer = new Zend_Config_Writer_Xml(); $writer->setConfig($config); $writer->setFilename($pathtoconfig . '/test.xml'); $writer->write(); Thank you for your help. greets
Hide
Christoph, René Pardon added a comment -

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

Show
Christoph, René Pardon added a comment - 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

People

Vote (1)
Watch (5)

Dates

  • Created:
    Updated:
    Resolved: