Ticket Details

Model::find('count',..) patch for RDMS databases

RFC Ticket (closed)

###What happened:
- something
$model::find('count',array('1'));

throws a fatal error, which can not be trapped in try/catch

###What was expected:
numeric result

###Patch

diff --git a/libraries/lithium/data/source/Database.php b/libraries/lithium/data/source/Database.php
index dcb499f..b3069cd 100644
@@ -617,6 +675,29 @@ abstract class Database extends \lithium\data\Source {
 		}
 		return (boolean) $value;
 	}
+
+        /**
+	 * Executes calculation-related queries, such as those required for `count`.
+	 *
+	 * @param string $type Only accepts `count`.
+	 * @param mixed $query The query to be executed.
+	 * @param array $options Optional arguments for the `read()` query that will be executed
+	 *        to obtain the calculation result.
+	 * @return integer Result of the calculation.
+	 */
+      	public function calculation($type, $query, array $options = array()) {
+		$query->calculate($type);
+                $query->fields(array("count(*) as C"));
+
+                $sql = $this->renderCommand($query);
+                $res = $this->invokeMethod('_execute', array($sql));
+                if ($res) {
+                    $data = $this->result('next', $res, null);
+                    return intval(implode('',$data));
+                }
+                return -1;
+	}
+
 }
 
-?>
\ No newline at end of file
+?>
on 08.11.10 reported by: thelonecabbage owned by: nate

Updates

on 08.11.10 by thelonecabbage
{{{

diff --git a/libraries/lithium/data/source/Database.php b/libraries/lithium/data/source/Database.php
index dcb499f..b3069cd 100644
@@ -617,6 +675,29 @@ abstract class Database extends \lithium\data\Source {
 		}
 		return (boolean) $value;
 	}
+
+        /**
+	 * Executes calculation-related queries, such as those required for `count`.
+	 *
+	 * @param string $type Only accepts `count`.
+	 * @param mixed $query The query to be executed.
+	 * @param array $options Optional arguments for the `read()` query that will be executed
+	 *        to obtain the calculation result.
+	 * @return integer Result of the calculation.
+	 */
+      	public function calculation($type, $query, array $options = array()) {
+		$query->calculate($type);
+                $query->fields(array("count(*) as C"));
+
+                $sql = $this->renderCommand($query);
+                $res = $this->invokeMethod('_execute', array($sql));
+                if ($res) {
+                    $data = $this->result('next', $res, null);
+                    return intval(implode('',$data));
+                }
+                return -1;
+	}
+
 }
 
-?>
\ No newline at end of file
+?>


}}}
(fixed) on 03.03.11 by nate
  • owner was changed to nate
  • status was changed to closed
  • resolution was changed to fixed
Implemented. Thanks for the patch.