From b98130db1e4676473ec3bdcdd0eb0669d4ae788e Mon Sep 17 00:00:00 2001 From: "Jonathan.Wage" Date: Sat, 22 Sep 2007 01:32:48 +0000 Subject: [PATCH] Fixes for doctrine resource. --- lib/Doctrine/Collection.php | 7 ++- lib/Doctrine/Parser/Xml.php | 2 +- lib/Doctrine/Record.php | 16 ++++--- lib/Doctrine/Resource.php | 25 +++++++++- lib/Doctrine/Resource/Client.php | 5 ++ lib/Doctrine/Resource/Collection.php | 16 ++++++- lib/Doctrine/Resource/Query.php | 27 +---------- lib/Doctrine/Resource/Record.php | 26 ++++++----- lib/Doctrine/Resource/Server.php | 69 +++++++++++----------------- playground/index.php | 21 ++++++++- 10 files changed, 121 insertions(+), 93 deletions(-) diff --git a/lib/Doctrine/Collection.php b/lib/Doctrine/Collection.php index 79d056aa6..383f42853 100644 --- a/lib/Doctrine/Collection.php +++ b/lib/Doctrine/Collection.php @@ -589,12 +589,15 @@ class Doctrine_Collection extends Doctrine_Access implements Countable, Iterator * * @param boolean $deep */ - public function toArray($deep = false) + public function toArray($deep = false, $prefixKey = false) { if ($deep) { $data = array(); foreach ($this->data as $key => $record) { - $data[$key] = $record->toArray($deep); + + $key = $prefixKey ? get_class($record) . '_' .$key:$key; + + $data[$key] = $record->toArray($deep, $prefixKey); } return $data; } else { diff --git a/lib/Doctrine/Parser/Xml.php b/lib/Doctrine/Parser/Xml.php index 84c3ae7c6..e93c3dcc9 100644 --- a/lib/Doctrine/Parser/Xml.php +++ b/lib/Doctrine/Parser/Xml.php @@ -108,7 +108,7 @@ class Doctrine_Parser_Xml extends Doctrine_Parser if (is_array($return)) { return $return; } else { - return $false; + return array(); } } } \ No newline at end of file diff --git a/lib/Doctrine/Record.php b/lib/Doctrine/Record.php index f22358dd7..e3896dc0b 100644 --- a/lib/Doctrine/Record.php +++ b/lib/Doctrine/Record.php @@ -1118,7 +1118,7 @@ abstract class Doctrine_Record extends Doctrine_Record_Abstract implements Count * @param boolean $deep - Return also the relations * @return array */ - public function toArray($deep = false) + public function toArray($deep = false, $prefixKey = false) { $a = array(); @@ -1134,18 +1134,20 @@ abstract class Doctrine_Record extends Doctrine_Record_Abstract implements Count } if ($deep) { foreach ($this->_references as $key => $relation) { - $a[$key] = $relation->toArray($deep); + $a[$key] = $relation->toArray($deep, $prefixKey); } } return array_merge($a, $this->_values); } public function fromArray($array) { - foreach ($array as $key => $value) { - if (!$this->getTable()->hasRelation($key) && $this->getTable()->hasColumn($key)) { - $this->$key = $value; - } else { - $this->$key->fromArray($value); + if (is_array($array)) { + foreach ($array as $key => $value) { + if (is_array($value)) { + $this->$key->fromArray($value); + } else { + $this->$key = $value; + } } } } diff --git a/lib/Doctrine/Resource.php b/lib/Doctrine/Resource.php index 4f9b618ee..da7044887 100644 --- a/lib/Doctrine/Resource.php +++ b/lib/Doctrine/Resource.php @@ -6,6 +6,29 @@ class Doctrine_Resource $url .= strstr($url, '?') ? '&':'?'; $url .= http_build_query($request); - return file_get_contents($url); + $response = file_get_contents($url); + + return $response; + } + + public function hydrate(array $array, $model, $config, $passedKey = null) + { + $collection = new Doctrine_Resource_Collection($model, $config); + + foreach ($array as $record) { + $r = new Doctrine_Resource_Record($model, $config); + + foreach ($record as $key => $value) { + if (is_array($value)) { + $r->data[$key] = $this->hydrate($value, $model, $config, $key); + } else { + $r->data[$key] = $value; + } + } + + $collection->data[] = $r; + } + + return $collection; } } \ No newline at end of file diff --git a/lib/Doctrine/Resource/Client.php b/lib/Doctrine/Resource/Client.php index 3674c2133..ed14a3454 100644 --- a/lib/Doctrine/Resource/Client.php +++ b/lib/Doctrine/Resource/Client.php @@ -17,4 +17,9 @@ class Doctrine_Resource_Client extends Doctrine_Resource { return new Doctrine_Resource_Record($model, $this->config); } + + public function newCollection($model) + { + return new Doctrine_Resource_Collection($model, $this->config); + } } \ No newline at end of file diff --git a/lib/Doctrine/Resource/Collection.php b/lib/Doctrine/Resource/Collection.php index 64256d8a5..7b6219491 100644 --- a/lib/Doctrine/Resource/Collection.php +++ b/lib/Doctrine/Resource/Collection.php @@ -5,9 +5,10 @@ class Doctrine_Resource_Collection extends Doctrine_Access implements Countable, public $config = array(); public $model = null; - public function __construct($model) + public function __construct($model, $config) { $this->model = $model; + $this->config = $config; } public function count() @@ -15,6 +16,18 @@ class Doctrine_Resource_Collection extends Doctrine_Access implements Countable, return count($data); } + public function get($get) + { + if (isset($this->data[$get])) { + return $this->data[$get]; + } + } + + public function set($set, $value) + { + $this->data[$set] = $value; + } + public function getIterator() { $data = $this->data; @@ -30,6 +43,7 @@ class Doctrine_Resource_Collection extends Doctrine_Access implements Countable, public function toArray() { $array = array(); + foreach ($this->data as $key => $record) { $array[$key] = $record->toArray(); } diff --git a/lib/Doctrine/Resource/Query.php b/lib/Doctrine/Resource/Query.php index 3e685ce95..ae5199da7 100644 --- a/lib/Doctrine/Resource/Query.php +++ b/lib/Doctrine/Resource/Query.php @@ -54,8 +54,8 @@ class Doctrine_Resource_Query extends Doctrine_Resource { $array = Doctrine_Parser::load($response, $this->getFormat()); - $hydrated = $this->hydrate($array); - + $hydrated = $this->hydrate($array, $this->getModel(), $this->config); + return $hydrated; } @@ -74,29 +74,6 @@ class Doctrine_Resource_Query extends Doctrine_Resource return $url; } - public function hydrate(array $array, $passedKey = null) - { - $model = $passedKey ? $passedKey:$this->getModel(); - - $collection = new Doctrine_Resource_Collection($model, $this->config); - - foreach ($array as $record) { - $r = new Doctrine_Resource_Record($model, $this->config); - - foreach ($record as $key => $value) { - if (is_array($value)) { - $r->data[$key] = $this->hydrate($value, $key); - } else { - $r->data[$key] = $value; - } - } - - $collection->data[] = $r; - } - - return $collection; - } - public function getModel() { $dql = $this->getDql(); diff --git a/lib/Doctrine/Resource/Record.php b/lib/Doctrine/Resource/Record.php index cf2df2ca3..88f053cb3 100644 --- a/lib/Doctrine/Resource/Record.php +++ b/lib/Doctrine/Resource/Record.php @@ -4,7 +4,6 @@ class Doctrine_Resource_Record extends Doctrine_Record_Abstract implements Count public $data = array(); public $config = array(); public $model = null; - public $changes = array(); public function __construct($model, $config) { @@ -24,8 +23,6 @@ class Doctrine_Resource_Record extends Doctrine_Record_Abstract implements Count public function set($set, $value) { $this->data[$set] = $value; - - $this->changes[$set] = $value; } public function count() @@ -40,22 +37,27 @@ class Doctrine_Resource_Record extends Doctrine_Record_Abstract implements Count return new ArrayIterator($data); } - public function save() + public function newRequest($type) { $request = array(); - $request['format'] = $this->config['format']; - $request['type'] = 'save'; + $request['format'] = isset($this->config['format']) ? $this->config['format']:'xml'; + $request['type'] = $type; $request['model'] = $this->model; - $request['data'] = $this->data; - $request['changes'] = $this->changes; + + return $request; + } + + public function save() + { + $request = $this->newRequest('save'); + $request['data'] = $this->toArray(); $response = Doctrine_Resource::request($this->config['url'], $request); $array = Doctrine_Parser::load($response, $request['format']); - $this->data = array_merge($this->data, $array); - - return $array; + $resource = new Doctrine_Resource(); + $this->data = $resource->hydrate(array($array), $this->model, $this->config)->getFirst()->data; } public function toArray() @@ -63,7 +65,7 @@ class Doctrine_Resource_Record extends Doctrine_Record_Abstract implements Count $array = array(); foreach ($this->data as $key => $value) { - if ($value instanceof Doctrine_Resource_Collection) { + if ($value instanceof Doctrine_Resource_Collection OR $value instanceof Doctrine_Resource_Record) { $array[$key] = $value->toArray(); } else { $array[$key] = $value; diff --git a/lib/Doctrine/Resource/Server.php b/lib/Doctrine/Resource/Server.php index e61fa0313..17daefc9f 100644 --- a/lib/Doctrine/Resource/Server.php +++ b/lib/Doctrine/Resource/Server.php @@ -2,12 +2,35 @@ class Doctrine_Resource_Server extends Doctrine_Resource { public $config = array(); + public $format = 'xml'; public function __construct($config) { $this->config = array_merge($config, $this->config); } + public function executeSave($request) + { + $model = $request['model']; + $data = $request['data']; + + $record = new $model(); + $record->fromArray($data); + $record->save(); + + return $record->toArray(true, true); + } + + public function executeQuery($request) + { + $dql = $request['dql']; + $params = isset($request['params']) ? $request['params']:array(); + + $conn = Doctrine_Manager::connection(); + + return $conn->query($dql, $params)->toArray(true, true); + } + public function execute($request) { if (!isset($request['type'])) { @@ -15,50 +38,10 @@ class Doctrine_Resource_Server extends Doctrine_Resource } $format = isset($request['format']) ? $request['format']:'xml'; + $type = $request['type']; + $funcName = 'execute' . Doctrine::classify($type); - if ($request['type'] == 'query') { - if (isset($request['dql']) && $request['dql']) { - $dql = $request['dql']; - $params = isset($request['params']) ? $request['params']:array(); - - $conn = Doctrine_Manager::connection(); - $result = $conn->query($dql, $params, Doctrine::FETCH_ARRAY); - } else { - throw new Doctrine_Resource_Exception('You must specify a dql query'); - } - } else if ($request['type'] == 'save') { - $model = $request['model']; - $table = Doctrine_Manager::getInstance()->getTable($model); - $pks = (array) $table->getIdentifier(); - $pks = array_flip($pks); - - $hasPk = false; - foreach (array_keys($pks) as $key) { - if (isset($request['data'][$key]) && $request['data'][$key]) { - $pks[$key] = $request['data'][$key]; - - $hasPk = true; - } - } - - if ($hasPk) { - $record = $table->find($pks); - } else { - $record = new $model(); - } - - if (isset($request['changes']) && !empty($request['changes'])) { - $changes = $request['changes']; - - foreach ($changes as $key => $value) { - $record->$key = $value; - } - } - - $record->save(); - - $result = $record->toArray(); - } + $result = $this->$funcName($request); return Doctrine_Parser::dump($result, $format); } diff --git a/playground/index.php b/playground/index.php index 764990286..77f0fe315 100644 --- a/playground/index.php +++ b/playground/index.php @@ -2,4 +2,23 @@ require_once('playground.php'); require_once('connection.php'); require_once('models.php'); -require_once('data.php'); \ No newline at end of file +require_once('data.php'); + +$action = isset($_REQUEST['action']) ? $_REQUEST['action']:'client'; + +if ($action == 'server') { + $config = array(); + + $server = new Doctrine_Resource_Server($config); + $server->run($_REQUEST); +} else { + $config = array('url' => 'http://localhost/~jwage/doctrine_trunk/playground/index.php?action=server'); + + $client = new Doctrine_Resource_Client($config); + $record = $client->newRecord('User'); + $record->name = 'jon wage'; + $record->loginname = 'test'; + $record->save(); + + print_r($record->toArray()); +} \ No newline at end of file