Commit 12cc664b authored by Jonathan.Wage's avatar Jonathan.Wage

Added exportTo() and importFrom() to Collection and Record.

parent 271d3c8e
......@@ -591,33 +591,44 @@ class Doctrine_Collection extends Doctrine_Access implements Countable, Iterator
*/
public function toArray($deep = false, $prefixKey = false)
{
if ($deep) {
$data = array();
foreach ($this->data as $key => $record) {
$key = $prefixKey ? get_class($record) . '_' .$key:$key;
$data[$key] = $record->toArray($deep, $prefixKey);
}
return $data;
} else {
// this is preserved for backwards compatibility
// but could be replaced with above code
return $this->data;
$data = array();
foreach ($this->data as $key => $record) {
$key = $prefixKey ? get_class($record) . '_' .$key:$key;
$data[$key] = $record->toArray($deep, $prefixKey);
}
return $data;
}
public function fromArray($array)
{
$data = array();
foreach ($array as $key => $row) {
foreach ($array as $row) {
$record = $this->_table->getRecord();
$record->fromArray($row);
$data[$key] = $record;
$data[] = $record;
}
$this->data = $data;
}
public function exportTo($type, $deep = false)
{
if ($type == 'array') {
return $this->toArray($deep);
} else {
return Doctrine_Parser::dump($this->toArray($deep, true), $type);
}
}
public function importFrom($type, $data)
{
if ($type == 'array') {
return $this->fromArray($data);
} else {
return $this->fromArray(Doctrine_Parser::load($data, $type));
}
}
public function getDeleteDiff()
{
return array_udiff($this->_snapshot, $this->data, array($this, "compareRecords"));
......
......@@ -87,18 +87,18 @@ class Doctrine_Parser_Xml extends Doctrine_Parser
foreach ($children as $element => $value) {
if ($value instanceof SimpleXMLElement) {
$values = (array)$value->children();
$values = (array) $value->children();
if (count($values) > 0) {
$return[$element] = $this->prepareData($value);
} else {
if (!isset($return[$element])) {
$return[$element] = (string)$value;
$return[$element] = (string) $value;
} else {
if (!is_array($return[$element])) {
$return[$element] = array($return[$element], (string)$value);
$return[$element] = array($return[$element], (string) $value);
} else {
$return[$element][] = (string)$value;
$return[$element][] = (string) $value;
}
}
}
......@@ -111,4 +111,4 @@ class Doctrine_Parser_Xml extends Doctrine_Parser
return array();
}
}
}
}
\ No newline at end of file
......@@ -1153,6 +1153,22 @@ abstract class Doctrine_Record extends Doctrine_Record_Abstract implements Count
}
}
}
public function exportTo($type, $deep = false)
{
if ($type == 'array') {
return $this->toArray($deep);
} else {
return Doctrine_Parser::dump($this->toArray($deep, true), $type);
}
}
public function importFrom($type, $data)
{
if ($type == 'array') {
return $this->fromArray($data);
} else {
return $this->fromArray(Doctrine_Parser::load($data, $type));
}
}
/**
* exists
* returns true if this record is persistent, otherwise false
......
<?php
require_once('playground.php');
if (isset($_REQUEST['server'])) {
require_once('connection.php');
require_once('models.php');
require_once('data.php');
$name = 'Doctrine_Resource_Playground';
$config = array('models' => $tables);
$server = Doctrine_Resource_Server::getInstance($name, $config);
$server->run($_REQUEST);
} else {
$url = 'http://localhost/~jwage/doctrine_trunk/playground/index.php?server';
$config = array('format' => 'xml');
// Instantiate a new client
$client = Doctrine_Resource_Client::getInstance($url, $config);
$query = new Doctrine_Resource_Query();
$users = $query->from('User u, u.Phonenumber p, u.Email e, u.Address a')->execute();
$json = Doctrine_Parser::load(Doctrine_Parser::dump($users->getFirst()->toArray(true, true), 'json'), 'json');
print_r($json);
}
\ No newline at end of file
require_once('connection.php');
require_once('models.php');
require_once('data.php');
\ No newline at end of file
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