From 208fa1eef8081bf08bc774642bffe2991cd9f1f8 Mon Sep 17 00:00:00 2001 From: "Jonathan.Wage" Date: Fri, 21 Sep 2007 18:19:19 +0000 Subject: [PATCH] Fixes for saving models. --- lib/Doctrine/Resource/Client.php | 7 +++++++ lib/Doctrine/Resource/Collection.php | 5 +++++ lib/Doctrine/Resource/Query.php | 8 ++------ lib/Doctrine/Resource/Record.php | 10 ++++++++++ lib/Doctrine/Resource/Server.php | 28 ++++++++++++++++++++-------- 5 files changed, 44 insertions(+), 14 deletions(-) diff --git a/lib/Doctrine/Resource/Client.php b/lib/Doctrine/Resource/Client.php index 20c1213be..9a8321fc8 100644 --- a/lib/Doctrine/Resource/Client.php +++ b/lib/Doctrine/Resource/Client.php @@ -12,4 +12,11 @@ class Doctrine_Resource_Client extends Doctrine_Resource { return new Doctrine_Resource_Query($this->config); } + + public function newRecord($model) + { + $record = new Doctrine_Resource_Record($model, $this->config); + + return $record; + } } \ No newline at end of file diff --git a/lib/Doctrine/Resource/Collection.php b/lib/Doctrine/Resource/Collection.php index 359e6b202..2bc5fab91 100644 --- a/lib/Doctrine/Resource/Collection.php +++ b/lib/Doctrine/Resource/Collection.php @@ -5,6 +5,11 @@ class Doctrine_Resource_Collection extends Doctrine_Access implements Countable, public $config = array(); public $model = null; + public function __construct($model) + { + $this->model = $model; + } + public function count() { return count($data); diff --git a/lib/Doctrine/Resource/Query.php b/lib/Doctrine/Resource/Query.php index e146eb520..3e685ce95 100644 --- a/lib/Doctrine/Resource/Query.php +++ b/lib/Doctrine/Resource/Query.php @@ -78,14 +78,10 @@ class Doctrine_Resource_Query extends Doctrine_Resource { $model = $passedKey ? $passedKey:$this->getModel(); - $collection = new Doctrine_Resource_Collection(); - $collection->model = $model; - $collection->config = $this->config; + $collection = new Doctrine_Resource_Collection($model, $this->config); foreach ($array as $record) { - $r = new Doctrine_Resource_Record(); - $r->config = $this->config; - $r->model = $model; + $r = new Doctrine_Resource_Record($model, $this->config); foreach ($record as $key => $value) { if (is_array($value)) { diff --git a/lib/Doctrine/Resource/Record.php b/lib/Doctrine/Resource/Record.php index 41d7b5c09..cf2df2ca3 100644 --- a/lib/Doctrine/Resource/Record.php +++ b/lib/Doctrine/Resource/Record.php @@ -6,6 +6,12 @@ class Doctrine_Resource_Record extends Doctrine_Record_Abstract implements Count public $model = null; public $changes = array(); + public function __construct($model, $config) + { + $this->model = $model; + $this->config = $config; + } + public function get($get) { if (!isset($this->data[$get])) { @@ -46,6 +52,10 @@ class Doctrine_Resource_Record extends Doctrine_Record_Abstract implements Count $response = Doctrine_Resource::request($this->config['url'], $request); $array = Doctrine_Parser::load($response, $request['format']); + + $this->data = array_merge($this->data, $array); + + return $array; } public function toArray() diff --git a/lib/Doctrine/Resource/Server.php b/lib/Doctrine/Resource/Server.php index 2e73f8b80..e61fa0313 100644 --- a/lib/Doctrine/Resource/Server.php +++ b/lib/Doctrine/Resource/Server.php @@ -27,25 +27,37 @@ class Doctrine_Resource_Server extends Doctrine_Resource throw new Doctrine_Resource_Exception('You must specify a dql query'); } } else if ($request['type'] == 'save') { - $table = Doctrine_Manager::getInstance()->getTable($request['model']); + $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) { - $pks[$key] = $request['data'][$key]; + if (isset($request['data'][$key]) && $request['data'][$key]) { + $pks[$key] = $request['data'][$key]; + + $hasPk = true; + } } - $record = $table->find($pks); + if ($hasPk) { + $record = $table->find($pks); + } else { + $record = new $model(); + } - $changes = $request['changes']; - - foreach ($changes as $key => $value) { - $record->$key = $value; + if (isset($request['changes']) && !empty($request['changes'])) { + $changes = $request['changes']; + + foreach ($changes as $key => $value) { + $record->$key = $value; + } } $record->save(); - $result = array('success' => true); + $result = $record->toArray(); } return Doctrine_Parser::dump($result, $format);