summaryrefslogtreecommitdiff
path: root/idiorm.php
diff options
context:
space:
mode:
authorJamie Matthews <[email protected]>2010-09-20 11:37:26 +0100
committerJamie Matthews <[email protected]>2010-09-20 11:37:26 +0100
commitf37277495a6cfec9d28dc09af19f791664a93000 (patch)
treef20312b1085ff96066f04aa2803e6d70b26a76db /idiorm.php
parent35d1a323e02b4d29ca4c0f042091555735d2fba6 (diff)
Simplify _run method, move logic for the different types of query into the separate find_one, find_many, count methods
Diffstat (limited to 'idiorm.php')
-rw-r--r--idiorm.php37
1 files changed, 14 insertions, 23 deletions
diff --git a/idiorm.php b/idiorm.php
index 8cc8c89..375ba17 100644
--- a/idiorm.php
+++ b/idiorm.php
@@ -242,7 +242,9 @@
$this->where($this->_get_id_column_name(), $id);
}
$this->_find_type = self::FIND_ONE;
- return $this->_run();
+ $statement = $this->_run();
+ $result = $statement->fetch(PDO::FETCH_ASSOC);
+ return $result ? self::for_table($this->_table_name)->hydrate($result) : $result;
}
/**
@@ -253,7 +255,12 @@
*/
public function find_many() {
$this->_find_type = self::FIND_MANY;
- return $this->_run();
+ $statement = $this->_run();
+ $instances = array();
+ while ($row = $statement->fetch(PDO::FETCH_ASSOC)) {
+ $instances[] = self::for_table($this->_table_name)->hydrate($row);
+ }
+ return $instances;
}
/**
@@ -263,7 +270,9 @@
*/
public function count() {
$this->_find_type = self::COUNT;
- return $this->_run();
+ $statement = $this->_run();
+ $result = $statement->fetch(PDO::FETCH_ASSOC);
+ return isset($result['count']) ? (int) $result['count'] : 0;
}
/**
@@ -543,31 +552,13 @@
/**
* Execute the SELECT query that has been built up by chaining methods
- * on this class. This method is called by find_one(), find_many() and
- * count(). If find_one() has been called, this will return a single
- * instance of the class or false. If find_many() has been called, this
- * will return an array of instances of the class. If count() has been
- * called, this will return an integer count of the rows.
+ * on this class. Return the executed PDOStatement object.
*/
protected function _run() {
self::_setup_db();
$statement = self::$_db->prepare($this->_build_select());
$statement->execute($this->_values);
-
- switch ($this->_find_type) {
- case self::FIND_ONE:
- $result = $statement->fetch(PDO::FETCH_ASSOC);
- return $result ? self::for_table($this->_table_name)->hydrate($result) : $result;
- case self::FIND_MANY:
- $instances = array();
- while ($row = $statement->fetch(PDO::FETCH_ASSOC)) {
- $instances[] = self::for_table($this->_table_name)->hydrate($row);
- }
- return $instances;
- case self::COUNT:
- $result = $statement->fetch(PDO::FETCH_ASSOC);
- return isset($result['count']) ? (int) $result['count'] : 0;
- }
+ return $statement;
}
/**