Commit d9b13a5c authored by meus's avatar meus

Added getRecordAsXml that returns an record as XML.

In order for this to work specify the xml option in a record. 
The xml option is an array that has 2 keys. 

ignore_fields is an array of fields that are not displayed in the xml
include_relations(optional) is an array of relations that should be included in
the xml. 
parent 4a93637e
...@@ -39,21 +39,21 @@ class Doctrine_Lib ...@@ -39,21 +39,21 @@ class Doctrine_Lib
public static function getRecordStateAsString($state) public static function getRecordStateAsString($state)
{ {
switch ($state) { switch ($state) {
case Doctrine_Record::STATE_PROXY: case Doctrine_Record::STATE_PROXY:
return "proxy"; return "proxy";
break; break;
case Doctrine_Record::STATE_CLEAN: case Doctrine_Record::STATE_CLEAN:
return "persistent clean"; return "persistent clean";
break; break;
case Doctrine_Record::STATE_DIRTY: case Doctrine_Record::STATE_DIRTY:
return "persistent dirty"; return "persistent dirty";
break; break;
case Doctrine_Record::STATE_TDIRTY: case Doctrine_Record::STATE_TDIRTY:
return "transient dirty"; return "transient dirty";
break; break;
case Doctrine_Record::STATE_TCLEAN: case Doctrine_Record::STATE_TCLEAN:
return "transient clean"; return "transient clean";
break; break;
} }
} }
/** /**
...@@ -72,6 +72,44 @@ class Doctrine_Lib ...@@ -72,6 +72,44 @@ class Doctrine_Lib
$r[] = "</pre>"; $r[] = "</pre>";
return implode("\n",$r)."<br />"; return implode("\n",$r)."<br />";
} }
public static function getRecordAsXml(Doctrine_Record $record, $xml = null)
{
$recordname = $record->getTable()->name;
if ($xml == null) {
$xml = new SimpleXMLElement("<" . $recordname . "></" . $recordname . ">");
}
$xml->addChild("id", $record->getOID());
$xml_options = $record->option("xml");
foreach ($record->getData() as $field => $value) {
if (isset($xml_options["ignore_fields"]) && !in_array($field, $xml_options["ignore_fields"])) {
$xml->addChild($field, $value);
}
}
if (!isset($xml_options["include_relations"])) {
return $xml->asXML();
}
$relations = $record->getTable()->getRelations();
foreach ($relations as $name => $relation) {
if (in_array($name, $xml_options["include_relations"])) {
$relation_type = $relation->getType();
$related_records = $record->get($name);
if ($relation_type == Doctrine_Relation::ONE) {
$related_xml = $xml->addChild($name);
Doctrine_Lib::getRecordAsXml($related_records, $related_xml);
} else {
$xml_collection = $xml->addChild($name . "s"); //this could be fixed to plurelize in some better way i guess
foreach ($related_records as $related_name => $related_record) {
$related_xml = $xml_collection->addChild($name);
Doctrine_Lib::getRecordAsXml($related_record, $related_xml);
}
}
}
}
return $xml->asXML();
}
/** /**
* getStateAsString * getStateAsString
* returns a given connection state as string * returns a given connection state as string
...@@ -80,15 +118,15 @@ class Doctrine_Lib ...@@ -80,15 +118,15 @@ class Doctrine_Lib
public static function getConnectionStateAsString($state) public static function getConnectionStateAsString($state)
{ {
switch ($state) { switch ($state) {
case Doctrine_Transaction::STATE_SLEEP: case Doctrine_Transaction::STATE_SLEEP:
return "open"; return "open";
break; break;
case Doctrine_Transaction::STATE_BUSY: case Doctrine_Transaction::STATE_BUSY:
return "busy"; return "busy";
break; break;
case Doctrine_Transaction::STATE_ACTIVE: case Doctrine_Transaction::STATE_ACTIVE:
return "active"; return "active";
break; break;
} }
} }
/** /**
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment