diff options
author | Simon Holywell <[email protected]> | 2014-01-23 13:08:36 +0000 |
---|---|---|
committer | Simon Holywell <[email protected]> | 2014-01-23 13:08:36 +0000 |
commit | 84e4d5e0d707c06cbce50a9f0947fdc20054aa0f (patch) | |
tree | d546f0526108168fb710efbf173bf4a672cb36b6 /idiorm.php | |
parent | 3fbafbeb80039facd6c3eea33d5060cf82270463 (diff) | |
parent | 472123d14eb8785bc54240a8d7a88addb1424d55 (diff) |
Merge branch 'develop' of github.com:j4mie/idiorm into develop
Diffstat (limited to 'idiorm.php')
-rw-r--r-- | idiorm.php | 47 |
1 files changed, 31 insertions, 16 deletions
@@ -233,7 +233,6 @@ * @return ORM */ public static function for_table($table_name, $connection_name = self::DEFAULT_CONNECTION) { - self::_setup_db($connection_name); return new self($table_name, array(), $connection_name); } @@ -259,7 +258,7 @@ } /** - * Ensures configuration (mulitple connections) is at least set to default. + * Ensures configuration (multiple connections) is at least set to default. * @param string $connection_name Which connection to use */ protected static function _setup_db_config($connection_name) { @@ -279,8 +278,10 @@ public static function set_db($db, $connection_name = self::DEFAULT_CONNECTION) { self::_setup_db_config($connection_name); self::$_db[$connection_name] = $db; - self::_setup_identifier_quote_character($connection_name); - self::_setup_limit_clause_style($connection_name); + if(!is_null(self::$_db[$connection_name])) { + self::_setup_identifier_quote_character($connection_name); + self::_setup_limit_clause_style($connection_name); + } } /** @@ -324,7 +325,7 @@ * @return string */ protected static function _detect_identifier_quote_character($connection_name) { - switch(self::$_db[$connection_name]->getAttribute(PDO::ATTR_DRIVER_NAME)) { + switch(self::get_db($connection_name)->getAttribute(PDO::ATTR_DRIVER_NAME)) { case 'pgsql': case 'sqlsrv': case 'dblib': @@ -347,7 +348,7 @@ * @return string Limit clause style keyword/constant */ protected static function _detect_limit_clause_style($connection_name) { - switch(self::$_db[$connection_name]->getAttribute(PDO::ATTR_DRIVER_NAME)) { + switch(self::get_db($connection_name)->getAttribute(PDO::ATTR_DRIVER_NAME)) { case 'sqlsrv': case 'dblib': case 'mssql': @@ -406,7 +407,7 @@ */ protected static function _execute($query, $parameters = array(), $connection_name = self::DEFAULT_CONNECTION) { self::_log_query($query, $parameters, $connection_name); - $statement = self::$_db[$connection_name]->prepare($query); + $statement = self::get_db($connection_name)->prepare($query); self::$_last_statement = $statement; @@ -438,7 +439,7 @@ if (count($parameters) > 0) { // Escape the parameters - $parameters = array_map(array(self::$_db[$connection_name], 'quote'), $parameters); + $parameters = array_map(array(self::get_db($connection_name), 'quote'), $parameters); // Avoid %format collision for vsprintf $query = str_replace("%", "%%", $query); @@ -695,7 +696,10 @@ $return_value = 0; if($result !== false && isset($result->$alias)) { - if((int) $result->$alias == (float) $result->$alias) { + if (!is_numeric($result->$alias)) { + $return_value = $result->$alias; + } + elseif((int) $result->$alias == (float) $result->$alias) { $return_value = (int) $result->$alias; } else { $return_value = (float) $result->$alias; @@ -1461,7 +1465,7 @@ $fragment = ''; if (!is_null($this->_limit) && self::$_config[$this->_connection_name]['limit_clause_style'] == ORM::LIMIT_STYLE_LIMIT) { - if (self::$_db[$this->_connection_name]->getAttribute(PDO::ATTR_DRIVER_NAME) == 'firebird') { + if (self::get_db($this->_connection_name)->getAttribute(PDO::ATTR_DRIVER_NAME) == 'firebird') { $fragment = 'ROWS'; } else { $fragment = 'LIMIT'; @@ -1477,7 +1481,7 @@ protected function _build_offset() { if (!is_null($this->_offset)) { $clause = 'OFFSET'; - if (self::$_db[$this->_connection_name]->getAttribute(PDO::ATTR_DRIVER_NAME) == 'firebird') { + if (self::get_db($this->_connection_name)->getAttribute(PDO::ATTR_DRIVER_NAME) == 'firebird') { $clause = 'TO'; } return "$clause " . $this->_offset; @@ -1738,10 +1742,11 @@ if ($this->_is_new) { $this->_is_new = false; if (is_null($this->id())) { - if(self::$_db[$this->_connection_name]->getAttribute(PDO::ATTR_DRIVER_NAME) == 'pgsql') { + $db = self::get_db($this->_connection_name); + if($db->getAttribute(PDO::ATTR_DRIVER_NAME) == 'pgsql') { $this->_data[$this->_get_id_column_name()] = self::get_last_statement()->fetchColumn(); } else { - $this->_data[$this->_get_id_column_name()] = self::$_db[$this->_connection_name]->lastInsertId(); + $this->_data[$this->_get_id_column_name()] = $db->lastInsertId(); } } } @@ -1784,7 +1789,7 @@ $placeholders = $this->_create_placeholders($this->_dirty_fields); $query[] = "({$placeholders})"; - if (self::$_db[$this->_connection_name]->getAttribute(PDO::ATTR_DRIVER_NAME) == 'pgsql') { + if (self::get_db($this->_connection_name)->getAttribute(PDO::ATTR_DRIVER_NAME) == 'pgsql') { $query[] = 'RETURNING ' . $this->_quote_identifier($this->_get_id_column_name()); } @@ -1881,7 +1886,11 @@ { $method = strtolower(preg_replace('/([a-z])([A-Z])/', '$1_$2', $name)); - return call_user_func_array(array($this, $method), $arguments); + if (method_exists($this, $method)) { + return call_user_func_array(array($this, $method), $arguments); + } else { + throw new IdiormMethodMissingException("Method $name() does not exist in class " . get_class($this)); + } } /** @@ -2131,7 +2140,11 @@ */ public function __call($method, $params = array()) { foreach($this->_results as $model) { - call_user_func_array(array($model, $method), $params); + if (method_exists($model, $method)) { + call_user_func_array(array($model, $method), $params); + } else { + throw new IdiormMethodMissingException("Method $method() does not exist in class " . get_class($this)); + } } return $this; } @@ -2141,3 +2154,5 @@ * A placeholder for exceptions eminating from the IdiormString class */ class IdiormStringException extends Exception {} + + class IdiormMethodMissingException extends Exception {}
\ No newline at end of file |