diff --git a/application/config/database.php b/application/config/database.php index 55ca839b..e4852ba3 100644 --- a/application/config/database.php +++ b/application/config/database.php @@ -16,6 +16,20 @@ return array( 'profile' => true, + /* + |-------------------------------------------------------------------------- + | PDO Fetch Style + |-------------------------------------------------------------------------- + | + | By default, database results will be returned as instances of the PHP + | stdClass object; however, you may wish to retrieve records as arrays + | instead of objects. Here you can control the PDO fetch style of the + | database queries run by your application. + | + */ + + 'fetch' => PDO::FETCH_CLASS, + /* |-------------------------------------------------------------------------- | Default Database Connection @@ -43,8 +57,6 @@ return array( | so make sure you have the PDO drivers for your particlar database of | choice installed on your machine. | - | Drivers: 'mysql', 'pgsql', 'sqlsrv', 'sqlite'. - | */ 'connections' => array( diff --git a/laravel/config.php b/laravel/config.php index fc5e0895..54cc87d8 100644 --- a/laravel/config.php +++ b/laravel/config.php @@ -56,13 +56,14 @@ class Config { * * * @param string $key + * @param mixed $default * @return array */ - public static function get($key) + public static function get($key, $default = null) { list($bundle, $file, $item) = static::parse($key); - if ( ! static::load($bundle, $file)) return; + if ( ! static::load($bundle, $file)) return value($default); $items = static::$items[$bundle][$file]; @@ -75,7 +76,7 @@ class Config { } else { - return array_get($items, $item); + return array_get($items, $item, $default); } } diff --git a/laravel/database/connection.php b/laravel/database/connection.php index 4d84b949..9369f2ba 100644 --- a/laravel/database/connection.php +++ b/laravel/database/connection.php @@ -145,7 +145,7 @@ class Connection { // and deletes we will return the affected row count. if (stripos($sql, 'select') === 0) { - return $statement->fetchAll(PDO::FETCH_CLASS, 'stdClass'); + return $this->fetch($statement, Config::get('database.fetch')); } elseif (stripos($sql, 'update') === 0 or stripos($sql, 'delete') === 0) { @@ -212,6 +212,28 @@ class Connection { return array($statement, $result); } + /** + * Fetch all of the rows for a given statement. + * + * @param PDOStatement $statement + * @param int $style + * @return array + */ + protected function fetch($statement, $style) + { + // If the fetch style is "class", we'll hydrate an array of PHP + // stdClass objects as generic containers for the query rows, + // otherwise we'll just use the fetch styel value. + if ($style === PDO::FETCH_CLASS) + { + return $statement->fetchAll(PDO::FETCH_CLASS, 'stdClass'); + } + else + { + return $statement->fetchAll($style); + } + } + /** * Log the query and fire the core query event. *