Skip to content

Commit

Permalink
Model::set now accept XML data (SimpleXMLElement or DONNode).
Browse files Browse the repository at this point in the history
  • Loading branch information
basuke authored and Yosuke Basuke Suzuki committed Sep 7, 2011
1 parent 0a71c4c commit 8303549
Showing 1 changed file with 43 additions and 1 deletion.
44 changes: 43 additions & 1 deletion cake/libs/model/model.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
App::import('Core', array('ClassRegistry', 'Validation', 'Set', 'String'));
App::import('Model', 'ModelBehavior', false);
App::import('Model', 'ConnectionManager', false);
App::uses('Xml', 'Utility');

/**
* Object-relational mapper.
Expand Down Expand Up @@ -1028,7 +1029,11 @@ public function set($one, $two = null) {
return;
}
if (is_object($one)) {
$one = Set::reverse($one);
if ($one instanceof SimpleXMLElement || $one instanceof DOMNode) {
$one = $this->_normalizeXmlData(Xml::toArray($one));
} else {
$one = Set::reverse($one);
}
}

if (is_array($one)) {
Expand Down Expand Up @@ -1065,6 +1070,26 @@ public function set($one, $two = null) {
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.
*
Expand Down Expand Up @@ -1242,6 +1267,23 @@ public function hasField($name, $checkVirtual = false) {
return false;
}

/**
* Check that a method is callable on a model. This will check both the model's own methods, its
* inherited methods and methods that could be callable through behaviors.
*
* @param string $method The method to be called.
* @return boolean True on method being callable.
*/
public function hasMethod($method) {
if (method_exists($this, $method)) {
return true;
}
if ($this->Behaviors->hasMethod($method)) {
return true;
}
return false;
}

/**
* Returns true if the supplied field is a model Virtual Field
*
Expand Down

0 comments on commit 8303549

Please sign in to comment.