diff --git a/cake/libs/model/model.php b/cake/libs/model/model.php index 823f8d2d6..c4b693a0c 100644 --- a/cake/libs/model/model.php +++ b/cake/libs/model/model.php @@ -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. * diff --git a/cake/tests/cases/libs/model/model_write.test.php b/cake/tests/cases/libs/model/model_write.test.php index fcf7c8e8f..4693bf30a 100644 --- a/cake/tests/cases/libs/model/model_write.test.php +++ b/cake/tests/cases/libs/model/model_write.test.php @@ -1019,12 +1019,20 @@ class ModelWriteTest extends BaseModelTest { App::import('Core', 'Xml'); $Article = new Article(); - $Article->save(new Xml('
')); - $result = $Article->save(new Xml('
')); + $result = $Article->save(Xml::build('
')); $this->assertFalse(empty($result)); - $results = $Article->find(array('Article.title' => 'test xml')); $this->assertFalse(empty($results)); + + $result = $Article->save(Xml::build('
testing6
')); + $this->assertFalse(empty($result)); + $results = $Article->find(array('Article.title' => 'testing')); + $this->assertFalse(empty($results)); + + $result = $Article->save(Xml::build('
testing with DOMDocument7
', array('return' => 'domdocument'))); + $this->assertFalse(empty($result)); + $results = $Article->find(array('Article.title' => 'testing with DOMDocument')); + $this->assertFalse(empty($results)); } /**