From e9ba4504bf97cf953afa332b0877a6f72ad6e9ef Mon Sep 17 00:00:00 2001 From: "Jonathan.Wage" Date: Mon, 24 Sep 2007 18:22:52 +0000 Subject: [PATCH] Fixes for Doctrine Resource --- lib/Doctrine/Record.php | 4 +- lib/Doctrine/Resource/Client.php | 4 +- lib/Doctrine/Resource/Collection.php | 6 ++- lib/Doctrine/Resource/Record.php | 69 ++++++++++++++++++++++++++-- lib/Doctrine/Resource/Request.php | 56 +++++++++++----------- lib/Doctrine/Resource/Server.php | 44 ++++++++++++++---- playground/index.php | 9 ++-- 7 files changed, 139 insertions(+), 53 deletions(-) diff --git a/lib/Doctrine/Record.php b/lib/Doctrine/Record.php index e3896dc0b..5068f740e 100644 --- a/lib/Doctrine/Record.php +++ b/lib/Doctrine/Record.php @@ -1143,9 +1143,9 @@ abstract class Doctrine_Record extends Doctrine_Record_Abstract implements Count { if (is_array($array)) { foreach ($array as $key => $value) { - if (is_array($value)) { + if ($this->getTable()->hasRelation($key) && $value) { $this->$key->fromArray($value); - } else { + } else if($this->getTable()->hasColumn($key) && $value) { $this->$key = $value; } } diff --git a/lib/Doctrine/Resource/Client.php b/lib/Doctrine/Resource/Client.php index cf7269343..2c42a51f4 100644 --- a/lib/Doctrine/Resource/Client.php +++ b/lib/Doctrine/Resource/Client.php @@ -59,7 +59,7 @@ class Doctrine_Resource_Client extends Doctrine_Resource } else { $request = new Doctrine_Resource_Request(); $request->set('type', 'load'); - $request->set('format', 'xml'); + $request->set('format', $this->getConfig()->get('format')); $schema = $request->execute(); @@ -67,7 +67,7 @@ class Doctrine_Resource_Client extends Doctrine_Resource } $import = new Doctrine_Import_Schema(); - $schema = $import->buildSchema($path, 'xml'); + $schema = $import->buildSchema($path, $this->getConfig()->get('format')); $this->getConfig()->set('schema', $schema); } diff --git a/lib/Doctrine/Resource/Collection.php b/lib/Doctrine/Resource/Collection.php index 21dbf6375..b87e2464b 100644 --- a/lib/Doctrine/Resource/Collection.php +++ b/lib/Doctrine/Resource/Collection.php @@ -31,7 +31,7 @@ * @link www.phpdoctrine.com * @since 1.0 */ -class Doctrine_Resource_Collection extends Doctrine_Access implements Countable, IteratorAggregate +class Doctrine_Resource_Collection extends Doctrine_Resource_Access implements Countable, IteratorAggregate { protected $_data = array(); protected $_config = array(); @@ -84,7 +84,9 @@ class Doctrine_Resource_Collection extends Doctrine_Access implements Countable, $array = array(); foreach ($this->_data as $key => $record) { - $array[$key] = $record->toArray(); + if ($record->exists()) { + $array[$this->_model . '_' .$key] = $record->toArray(); + } } return $array; diff --git a/lib/Doctrine/Resource/Record.php b/lib/Doctrine/Resource/Record.php index ff0439c2a..5835330a2 100644 --- a/lib/Doctrine/Resource/Record.php +++ b/lib/Doctrine/Resource/Record.php @@ -31,7 +31,7 @@ * @link www.phpdoctrine.com * @since 1.0 */ -class Doctrine_Resource_Record extends Doctrine_Record_Abstract implements Countable, IteratorAggregate +class Doctrine_Resource_Record extends Doctrine_Resource_Access implements Countable, IteratorAggregate { protected $_data = array(); protected $_model = null; @@ -119,7 +119,7 @@ class Doctrine_Resource_Record extends Doctrine_Record_Abstract implements Count public function save() { - $format = $this->getConfig('format') ? $this->getConfig('format'):'xml'; + $format = $this->getConfig('format'); $request = new Doctrine_Resource_Request(); $request->set('format', $format); @@ -139,14 +139,73 @@ class Doctrine_Resource_Record extends Doctrine_Record_Abstract implements Count return $this->_model; } + public function identifier() + { + $identifier = array(); + + if (isset($this->_schema['columns']) && is_array($this->_schema['columns'])) { + foreach ($this->_schema['columns'] as $name => $column) { + if ($column['primary'] == true) { + $identifier[$name] = $this->_data[$name]; + } + } + } + + return $identifier; + } + + public function exists() + { + $identifier = $this->identifier(); + + foreach ($identifier as $key => $value) { + if (!$value) { + return false; + } + } + + return true; + } + + public function hasColumn($name) + { + return isset($this->_schema['columns'][$name]) ? true:false; + } + + public function getColumn($name) + { + if ($this->hasColumn($name)) { + return $this->_columns[$name]; + } + } + + public function hasRelation($name) + { + return isset($this->_schema['relations'][$name]) ? true:false; + } + + public function getRelation($name) + { + if ($this->hasRelation($name)) { + return $this->_schema['relations'][$name]; + } + } + public function toArray() { $array = array(); foreach ($this->_data as $key => $value) { - if ($value instanceof Doctrine_Resource_Collection OR $value instanceof Doctrine_Resource_Record) { - $array[$key] = $value->toArray(); - } else { + + if ($this->hasRelation($key) && $value instanceof Doctrine_Resource_Collection) { + if ($value->count() > 0) { + $array[$key] = $value->toArray(); + } + } else if ($this->hasRelation($key) && $value instanceof Doctrine_Resource_Record) { + if ($value->exists()) { + $array[$key] = $value->toArray(); + } + } else if (!$this->hasRelation($key) && $this->hasColumn($key)) { $array[$key] = $value; } } diff --git a/lib/Doctrine/Resource/Request.php b/lib/Doctrine/Resource/Request.php index 03311ff52..717039a8c 100644 --- a/lib/Doctrine/Resource/Request.php +++ b/lib/Doctrine/Resource/Request.php @@ -40,49 +40,49 @@ class Doctrine_Resource_Request extends Doctrine_Resource_Params return Doctrine_Resource_Client::getInstance()->getConfig($key); } - public function getParams() - { - if ($this->_params === null) { - $this->_params = new Doctrine_Resource_Params(); - } - - return $this->_params; - } - - public function get($key) - { - return $this->getParams()->get($key); - } - - public function set($key, $value) - { - return $this->getParams()->set($key, $value); - } - public function execute() { $url = $this->getConfig()->get('url'); - $url .= strstr($this->getConfig()->get('url'), '?') ? '&':'?'; - $url .= http_build_query($this->getParams()->getAll()); + $data = array('type' => $this->get('type'), 'format' => $this->getConfig()->get('format'), 'data' => Doctrine_Parser::dump($this->getAll(), $this->getConfig()->get('format'))); - $response = file_get_contents($url); + $ch = curl_init(); + curl_setopt($ch, CURLOPT_URL, $url); + curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); + curl_setopt($ch, CURLOPT_POST, 1); + + curl_setopt($ch, CURLOPT_POSTFIELDS, $data); + $response = curl_exec($ch); + + if (curl_errno($ch)) { + throw new Doctrine_Resource_Exception('Request failed'); + } + + curl_close($ch); return $response; } public function hydrate(array $array, $model, $passedKey = null) { - $config = $this->getConfig(); + if (empty($array)) { + return false; + } - $collection = new Doctrine_Resource_Collection($model, $config); + $collection = new Doctrine_Resource_Collection($model); foreach ($array as $record) { - $r = new Doctrine_Resource_Record($model, $config); + $r = new Doctrine_Resource_Record($model); foreach ($record as $key => $value) { - if (is_array($value)) { - $r->set($key, $this->hydrate($value, $model, $config, $key)); - } else { + if ($r->hasRelation($key) && !empty($value)) { + $relation = $r->getRelation($key); + + if ($relation['type'] === Doctrine_Relation::MANY) { + $r->set($key, $this->hydrate($value, $relation['class'], $key)); + } else { + $r->set($key, $this->hydrate(array($value), $relation['class'], $key)->getFirst()); + } + } else if($r->hasColumn($key)) { $r->set($key, $value); } } diff --git a/lib/Doctrine/Resource/Server.php b/lib/Doctrine/Resource/Server.php index b7e1e56af..08a868e0a 100644 --- a/lib/Doctrine/Resource/Server.php +++ b/lib/Doctrine/Resource/Server.php @@ -31,7 +31,7 @@ * @link www.phpdoctrine.com * @since 1.0 */ -class Doctrine_Resource_Server extends Doctrine_resource +class Doctrine_Resource_Server extends Doctrine_Resource { static public function getInstance($config = null) { @@ -49,7 +49,29 @@ class Doctrine_Resource_Server extends Doctrine_resource $model = $request->get('model'); $data = $request->get('data'); - $record = new $model(); + $table = Doctrine_Manager::getInstance()->getTable($model); + $identifier = $table->getIdentifier(); + + if (!is_array($identifier)) { + $identifier = array($identifier); + } + + $existing = true; + $pks = array(); + foreach ($identifier as $name) { + if (isset($data[$name])) { + $pks[$name] = $data[$name]; + } else { + $existing = false; + } + } + + if ($existing) { + $record = $table->find($pks); + } else { + $record = new $model(); + } + $record->fromArray($data); $record->save(); @@ -82,20 +104,22 @@ class Doctrine_Resource_Server extends Doctrine_resource return $schema; } - public function execute($request) + public function execute(array $r) { - if (!isset($request['type'])) { - throw new Doctrine_Resource_Exception('You must specify a request type'); - } + if (!isset($r['data'])) { + throw new Doctrine_Resource_Exception('You must specify a data xml string in your request'); + } + + $type = $r['type']; + $format = isset($r['format']) ? $r['format']:'xml'; + $data = Doctrine_Parser::load($r['data'], $format); - $format = isset($request['format']) ? $request['format']:'xml'; - $type = $request['type']; $funcName = 'execute' . Doctrine::classify($type); - $request = new Doctrine_Resource_Request($request); + $requestObj = new Doctrine_Resource_Request($data); if (method_exists($this, $funcName)) { - $result = $this->$funcName($request); + $result = $this->$funcName($requestObj); } else { throw new Doctrine_Resource_Exception('Unknown Doctrine Resource Server function'); } diff --git a/playground/index.php b/playground/index.php index 2b011ce4a..cd8e29472 100644 --- a/playground/index.php +++ b/playground/index.php @@ -18,9 +18,10 @@ if ($action == 'server') { $client = Doctrine_Resource_Client::getInstance($config); - $user = $client->newRecord('User'); - $user->name = 'jonathan h. wage'; - $user->save(); + $query = new Doctrine_Resource_Query(); + $query->from('User u, u.Email e, u.Phonenumber p'); - print_r($user->toArray()); + $users = $query->execute(); + + print_r($users->toArray()); } \ No newline at end of file