Added exportTo() and importFrom() to Collection and Record.
This commit is contained in:
parent
271d3c8e05
commit
12cc664bf1
4 changed files with 50 additions and 47 deletions
|
@ -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();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
|
@ -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
|
||||
|
|
|
@ -1,29 +1,5 @@
|
|||
<?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);
|
||||
}
|
||||
require_once('connection.php');
|
||||
require_once('models.php');
|
||||
require_once('data.php');
|
Loading…
Add table
Reference in a new issue