From 3392971ce5b390a6488a783547a03d6e37e3b003 Mon Sep 17 00:00:00 2001 From: Taylor Otwell Date: Wed, 22 Jun 2011 21:11:35 -0500 Subject: [PATCH] added dynamic finders to fluent query builder and eloquent. --- system/db/query.php | 14 +++++++++ system/db/query/dynamic.php | 60 +++++++++++++++++++++++++++++++++++++ 2 files changed, 74 insertions(+) create mode 100644 system/db/query/dynamic.php diff --git a/system/db/query.php b/system/db/query.php index 3e28f20d..0d053554 100644 --- a/system/db/query.php +++ b/system/db/query.php @@ -500,6 +500,20 @@ class Query { */ public function __call($method, $parameters) { + // --------------------------------------------------------- + // Dynamic methods allows the building of very expressive + // queries. All dynamic methods start with "where_". + // + // Ex: DB::table('users')->where_email($email)->first(); + // --------------------------------------------------------- + if (strpos($method, 'where_') === 0) + { + return Query\Dynamic::build($method, $parameters, $this); + } + + // --------------------------------------------------------- + // Handle any of the aggregate functions. + // --------------------------------------------------------- if (in_array($method, array('count', 'min', 'max', 'avg', 'sum'))) { return ($method == 'count') ? $this->aggregate(Str::upper($method), '*') : $this->aggregate(Str::upper($method), $parameters[0]); diff --git a/system/db/query/dynamic.php b/system/db/query/dynamic.php new file mode 100644 index 00000000..ea90e2ea --- /dev/null +++ b/system/db/query/dynamic.php @@ -0,0 +1,60 @@ +where($segment, '=', $parameters[$index], $connector); + + $index++; + } + else + { + $connector = trim(Str::upper($segment), '_'); + } + } + + return $query; + } + +} \ No newline at end of file