From 78b0dcedfc85e40930d79e96937c3c3cbdd1916c Mon Sep 17 00:00:00 2001 From: Rudi Grinberg Date: Fri, 7 Sep 2012 17:58:16 -0400 Subject: [PATCH] Added smartPrepareAndExecute. Warning: Test before using --- airtime_mvc/application/common/Database.php | 25 +++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/airtime_mvc/application/common/Database.php b/airtime_mvc/application/common/Database.php index 8793af03d..a90227ff6 100644 --- a/airtime_mvc/application/common/Database.php +++ b/airtime_mvc/application/common/Database.php @@ -29,4 +29,29 @@ class Application_Common_Database } return $rows; } + /* + Wrapper around prepareAndExecute that allows you to use multipe :xx's + in one query. Transforms $sql to :xx1, :xx2, .... + */ + public static function smartPrepareAndExecute($sql, array $params, + $type='all', $fetchType=PDO::FETCH_ASSOC) + { + $new_params = array(); + $new_sql = $sql; + foreach ($params as $k => $v) { + $matches_count = substr_count($sql, $k); + if ($matches_count == 0) { + throw new Exception("Argument $k is not inside $sql"); + } elseif ($matches_count == 1) { + $new_params[$k] = $new_params[$v]; + } else { + foreach ( range(1,$matches_count) as $i ) { + preg_replace( "/$k(\D)/", "$k.$i${1}", $sql, 1); + $new_params[ $k.$i ] = $v; + } + } + } + return Application_Common_Database::prepareAndExecute( $new_sql, + $new_params, $type, $fetchType); + } }