Fixed support to save XML data in Models.

This commit is contained in:
Juan Basso 2010-10-17 22:47:43 -02:00
parent b2035a5c96
commit d771239104
2 changed files with 37 additions and 4 deletions

View file

@ -831,7 +831,12 @@ class Model extends Object {
return;
}
if (is_object($one)) {
$one = Set::reverse($one);
if ($one instanceof SimpleXMLElement || $one instanceof DOMNode) {
App::import('Core', 'Xml');
$one = $this->_normalizeXmlData(Xml::toArray($one));
} else {
$one = Set::reverse($one);
}
}
if (is_array($one)) {
@ -868,6 +873,26 @@ class Model extends Object {
return $data;
}
/**
* Normalize Xml::toArray() to use in Model::save()
*
* @param array $xml XML as array
* @return array
*/
protected function _normalizeXmlData(array $xml) {
$return = array();
foreach ($xml as $key => $value) {
if (is_array($value)) {
$return[Inflector::camelize($key)] = $this->_normalizeXmlData($value);
} elseif ($key[0] === '@') {
$return[substr($key, 1)] = $value;
} else {
$return[$key] = $value;
}
}
return $return;
}
/**
* Deconstructs a complex data type (array or object) into a single field value.
*

View file

@ -1019,12 +1019,20 @@ class ModelWriteTest extends BaseModelTest {
App::import('Core', 'Xml');
$Article = new Article();
$Article->save(new Xml('<article title="test xml" user_id="5" />'));
$result = $Article->save(new Xml('<article title="test xml" user_id="5" />'));
$result = $Article->save(Xml::build('<article title="test xml" user_id="5" />'));
$this->assertFalse(empty($result));
$results = $Article->find(array('Article.title' => 'test xml'));
$this->assertFalse(empty($results));
$result = $Article->save(Xml::build('<article><title>testing</title><user_id>6</user_id></article>'));
$this->assertFalse(empty($result));
$results = $Article->find(array('Article.title' => 'testing'));
$this->assertFalse(empty($results));
$result = $Article->save(Xml::build('<article><title>testing with DOMDocument</title><user_id>7</user_id></article>', array('return' => 'domdocument')));
$this->assertFalse(empty($result));
$results = $Article->find(array('Article.title' => 'testing with DOMDocument'));
$this->assertFalse(empty($results));
}
/**