mirror of
https://github.com/kamilwylegala/cakephp2-php8.git
synced 2024-11-15 03:18:26 +00:00
Fixed support to save XML data in Models.
This commit is contained in:
parent
b2035a5c96
commit
d771239104
2 changed files with 37 additions and 4 deletions
|
@ -831,8 +831,13 @@ class Model extends Object {
|
|||
return;
|
||||
}
|
||||
if (is_object($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)) {
|
||||
$data = $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.
|
||||
*
|
||||
|
|
|
@ -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));
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
Loading…
Reference in a new issue