diff options
-rw-r--r-- | docs/configuration.rst | 16 | ||||
-rw-r--r-- | idiorm.php | 44 |
2 files changed, 47 insertions, 13 deletions
diff --git a/docs/configuration.rst b/docs/configuration.rst index 698b67d..e6dc972 100644 --- a/docs/configuration.rst +++ b/docs/configuration.rst @@ -118,6 +118,21 @@ class constants defined by PDO. For example: The default setting is ``PDO::ERRMODE_EXCEPTION``. For full details of the error modes available, see `the PDO set attribute documentation`_. +PDO object access +^^^^^^^^^^^^^^^^^ + +Should it ever be necessary, the PDO object used by Idiorm may be +accessed directly through ``ORM::get_db()``, or set directly via +``ORM::set_db()``. This should be an unusual occurance. + +After a statement has been executed by any means, such as ``::save()`` +or ``::raw_execute()``, the ``PDOStatement`` instance used may be +accessed via ``ORM::get_last_statement()``. This may be useful in order +to access ``PDOStatement::errorCode()``, if PDO exceptions are turned +off, or to access the ``PDOStatement::rowCount()`` method, which returns +differing results based on the underlying database. For more +information, see the `PDOStatement documentation`_. + Identifier quote character ^^^^^^^^^^^^^^^^^^^^^^^^^^ @@ -209,4 +224,5 @@ Warnings and gotchas .. _PDO documentation: http://php.net/manual/en/pdo.construct.php .. _the PDO documentation: http://www.php.net/manual/en/pdo.construct.php .. _the PDO set attribute documentation: http://uk2.php.net/manual/en/pdo.setattribute.php +.. _PDOStatement documentation: http://www.php.net/manual/en/class.pdostatement.php .. _Memcached: http://www.memcached.org/
\ No newline at end of file @@ -78,6 +78,9 @@ // Query cache, only used if query caching is enabled protected static $_query_cache = array(); + // Reference to previously used PDOStatement object to enable low-level access, if needed + protected static $_last_statement = null; + // --------------------------- // // --- INSTANCE PROPERTIES --- // // --------------------------- // @@ -256,6 +259,14 @@ } /** + * Returns the PDOSatemen instance last used by the the ORM. + * Useful for access to PDOStatement::rowCount() or error information + */ + public static function get_last_statement() { + return self::$_last_statement; + } + + /** * Executes a raw query as a wrapper for PDOStatement::execute. * Useful for queries that can't be accomplished through Idiorm, * particularly those using engine-specific features. @@ -268,8 +279,21 @@ public static function raw_execute($query, $parameters = array()) { self::_setup_db(); + return self::_execute($query, $parameters); + } + + /** + * Internal helper method for executing statments. Logs queries, and + * stores statement object in ::_last_statment, accessible publicly + * through ::get_last_statement() + * @return bool Response of PDOStatement::execute() + **/ + protected static function _execute($query, $parameters = array()) { self::_log_query($query, $parameters); $statement = self::$_db->prepare($query); + + self::$_last_statement = $statement; + return $statement->execute($parameters); } @@ -1173,9 +1197,8 @@ } } - self::_log_query($query, $this->_values); - $statement = self::$_db->prepare($query); - $statement->execute($this->_values); + self::_execute($query, $this->_values); + $statement = self::$_last_statement; $rows = array(); while ($row = $statement->fetch(PDO::FETCH_ASSOC)) { @@ -1315,9 +1338,7 @@ $query = $this->_build_insert(); } - self::_log_query($query, $values); - $statement = self::$_db->prepare($query); - $success = $statement->execute($values); + $success = self::_execute($query, $values); // If we've just inserted a new record, set the ID of this object if ($this->_is_new) { @@ -1387,10 +1408,8 @@ $this->_quote_identifier($this->_get_id_column_name()), "= ?", )); - $params = array($this->id()); - self::_log_query($query, $params); - $statement = self::$_db->prepare($query); - return $statement->execute($params); + + return self::_execute($query, array($this->id())); } /** @@ -1404,9 +1423,8 @@ $this->_quote_identifier($this->_table_name), $this->_build_where(), )); - self::_log_query($query, $this->_values); - $statement = self::$_db->prepare($query); - return $statement->execute($this->_values); + + return self::_execute($query, $this->_values); } // --------------------- // |