From b40fd36efa853885a5e71ebcac438360935fdf6d Mon Sep 17 00:00:00 2001 From: "Jonathan.Wage" Date: Mon, 24 Sep 2007 19:29:56 +0000 Subject: [PATCH] Fixes to server. --- lib/Doctrine/Resource/Client.php | 14 +-- lib/Doctrine/Resource/Query.php | 142 ++++--------------------------- lib/Doctrine/Resource/Server.php | 2 +- playground/index.php | 11 +-- 4 files changed, 32 insertions(+), 137 deletions(-) diff --git a/lib/Doctrine/Resource/Client.php b/lib/Doctrine/Resource/Client.php index 3a1411d17..f142213b8 100644 --- a/lib/Doctrine/Resource/Client.php +++ b/lib/Doctrine/Resource/Client.php @@ -63,13 +63,17 @@ class Doctrine_Resource_Client extends Doctrine_Resource $schema = $request->execute(); - file_put_contents($path, $schema); + if ($schema) { + file_put_contents($path, $schema); + } } - $import = new Doctrine_Import_Schema(); - $schema = $import->buildSchema($path, $this->getConfig()->get('format')); - - $this->getConfig()->set('schema', $schema); + if (file_exists($path) && $schema) { + $import = new Doctrine_Import_Schema(); + $schema = $import->buildSchema($path, $this->getConfig()->get('format')); + + $this->getConfig()->set('schema', $schema); + } } public function newQuery() diff --git a/lib/Doctrine/Resource/Query.php b/lib/Doctrine/Resource/Query.php index f6d564281..1a199fd77 100644 --- a/lib/Doctrine/Resource/Query.php +++ b/lib/Doctrine/Resource/Query.php @@ -35,7 +35,6 @@ class Doctrine_Resource_Query { protected $_parts = array(); protected $_dql = null; - protected $_params = array(); public function getConfig($key = null) { @@ -51,6 +50,8 @@ class Doctrine_Resource_Query public function execute($params = array()) { + $params = is_array($params) ? $params:array($params); + $request = new Doctrine_Resource_Request(); $request->set('dql', $this->getDql()); $request->set('params', $params); @@ -59,9 +60,15 @@ class Doctrine_Resource_Query $response = $request->execute(); - $array = Doctrine_Parser::load($response, $this->getConfig()->get('format')); - - return $request->hydrate($array, $this->getModel()); + // If we have a response then lets parse it and hydrate it + if ($response) { + $array = Doctrine_Parser::load($response, $this->getConfig()->get('format')); + + return $request->hydrate($array, $this->getModel()); + // Otherwise lets return an empty collection for the queried for model + } else { + return new Doctrine_Resource_Collection($this->getModel()); + } } public function getDql() @@ -138,41 +145,8 @@ class Doctrine_Resource_Query * @param mixed $params an array of parameters or a simple scalar * @return Doctrine_Query */ - public function addWhere($where, $params = array()) + public function addWhere($where) { - if (is_array($params)) { - $this->_params['where'] = array_merge($this->_params['where'], $params); - } else { - $this->_params['where'][] = $params; - } - return $this->parseQueryPart('where', $where, true); - } - /** - * whereIn - * adds IN condition to the query WHERE part - * - * @param string $expr - * @param mixed $params an array of parameters or a simple scalar - * @return Doctrine_Query - */ - public function whereIn($expr, $params = array()) - { - $params = (array) $params; - $a = array(); - foreach ($params as $k => $value) { - if ($value instanceof Doctrine_Expression) { - $value = $value->getSql(); - unset($params[$k]); - } else { - $value = '?'; - } - $a[] = $value; - } - - $this->_params['where'] = array_merge($this->_params['where'], $params); - - $where = $expr . ' IN (' . implode(', ', $a) . ')'; - return $this->parseQueryPart('where', $where, true); } /** @@ -194,13 +168,8 @@ class Doctrine_Resource_Query * @param mixed $params an array of parameters or a simple scalar * @return Doctrine_Query */ - public function addHaving($having, $params = array()) + public function addHaving($having) { - if (is_array($params)) { - $this->_params['having'] = array_merge($this->_params['having'], $params); - } else { - $this->_params['having'][] = $params; - } return $this->parseQueryPart('having', $having, true); } /** @@ -234,72 +203,7 @@ class Doctrine_Resource_Query */ public function distinct($flag = true) { - $this->_parts['distinct'] = (bool) $flag; - - return $this; - } - - /** - * forUpdate - * Makes the query SELECT FOR UPDATE. - * - * @param bool $flag Whether or not the SELECT is FOR UPDATE (default true). - * @return Doctrine_Query - */ - public function forUpdate($flag = true) - { - $this->_parts[self::FOR_UPDATE] = (bool) $flag; - - return $this; - } - /** - * delete - * sets the query type to DELETE - * - * @return Doctrine_Query - */ - public function delete() - { - $this->type = self::DELETE; - - return $this; - } - /** - * update - * sets the UPDATE part of the query - * - * @param string $update Query UPDATE part - * @return Doctrine_Query - */ - public function update($update) - { - $this->type = self::UPDATE; - - return $this->parseQueryPart('from', $update); - } - /** - * set - * sets the SET part of the query - * - * @param string $update Query UPDATE part - * @return Doctrine_Query - */ - public function set($key, $value, $params = null) - { - if (is_array($key)) { - foreach ($key as $k => $v) { - $this->set($k, '?', array($v)); - } - } else { - if ($params !== null) { - if (is_array($params)) { - $this->_params['set'] = array_merge($this->_params['set'], $params); - } else { - $this->_params['set'][] = $params; - } - } - return $this->parseQueryPart('set', $key . ' = ' . $value, true); - } + $this->parseQueryPart('distinct', (bool) $flag); } /** * from @@ -353,15 +257,8 @@ class Doctrine_Resource_Query * @param mixed $params an array of parameters or a simple scalar * @return Doctrine_Query */ - public function where($where, $params = array()) + public function where($where) { - $this->_params['where'] = array(); - if (is_array($params)) { - $this->_params['where'] = $params; - } else { - $this->_params['where'][] = $params; - } - return $this->parseQueryPart('where', $where); } /** @@ -372,15 +269,8 @@ class Doctrine_Resource_Query * @param mixed $params an array of parameters or a simple scalar * @return Doctrine_Query */ - public function having($having, $params = array()) + public function having($having) { - $this->_params['having'] = array(); - if (is_array($params)) { - $this->_params['having'] = $params; - } else { - $this->_params['having'][] = $params; - } - return $this->parseQueryPart('having', $having); } /** diff --git a/lib/Doctrine/Resource/Server.php b/lib/Doctrine/Resource/Server.php index 08a868e0a..b80391a0d 100644 --- a/lib/Doctrine/Resource/Server.php +++ b/lib/Doctrine/Resource/Server.php @@ -59,7 +59,7 @@ class Doctrine_Resource_Server extends Doctrine_Resource $existing = true; $pks = array(); foreach ($identifier as $name) { - if (isset($data[$name])) { + if (isset($data[$name]) && $data[$name]) { $pks[$name] = $data[$name]; } else { $existing = false; diff --git a/playground/index.php b/playground/index.php index cd8e29472..35a3b2af7 100644 --- a/playground/index.php +++ b/playground/index.php @@ -14,14 +14,15 @@ if ($action == 'server') { $server->run($_REQUEST); } else { - $config = array('url' => 'http://localhost/~jwage/doctrine_trunk/playground/index.php?action=server'); + //$config = array('url' => 'http://localhost/~jwage/doctrine_trunk/playground/index.php?action=server'); + $config = array('url' => 'http://dev.centresource.com/jwage/hca/web/frontend_dev.php/main'); $client = Doctrine_Resource_Client::getInstance($config); - $query = new Doctrine_Resource_Query(); - $query->from('User u, u.Email e, u.Phonenumber p'); + $user = $client->newRecord('sfGuardUser'); - $users = $query->execute(); + $user->name = 'jonnnywage'; + $user->save(); - print_r($users->toArray()); + print_r($user->toArray()); } \ No newline at end of file