diff --git a/laravel/database/connection.php b/laravel/database/connection.php index b64fa020..e6ec8792 100644 --- a/laravel/database/connection.php +++ b/laravel/database/connection.php @@ -1,5 +1,8 @@ name = $name; $this->query = $query; @@ -79,9 +82,17 @@ public function connected() /** * Execute a SQL query against the connection and return a scalar result. * - * @param string $sql - * @param array $bindings - * @return mixed + * + * // Get the number of rows in the "users" table + * $count = DB::connection()->scalar('select count(*) from users'); + * + * // Get the sum of payments from the "bank" table + * $sum = DB::connection()->scalar('select sum(payment) from banks where bank_id = ?', array(1)); + * + * + * @param string $sql + * @param array $bindings + * @return int|float */ public function scalar($sql, $bindings = array()) { @@ -93,6 +104,14 @@ public function scalar($sql, $bindings = array()) /** * Execute a SQL query against the connection and return the first result. * + * + * // Get the first result from the "users" table + * $user = DB::connection()->first('select * from users limit 1'); + * + * // Get the first result from a specified group of users + * $user = DB::connection()->first('select * from users where group_id = ?', array(1)); + * + * * @param string $sql * @param array $bindings * @return object @@ -112,6 +131,14 @@ public function first($sql, $bindings = array()) * DELETE -> Number of Rows affected. * ELSE -> Boolean true / false depending on success. * + * + * // Execute a query against the connection + * $users = DB::connection()->query('select * from users'); + * + * // Execute a query against the connection using bindings + * $users = DB::connection()->query('select * from users where group_id = ?', array(1)); + * + * * @param string $sql * @param array $bindings * @return mixed @@ -132,13 +159,13 @@ public function query($sql, $bindings = array()) * @param array $results * @return mixed */ - private function execute(\PDOStatement $statement, $bindings) + protected function execute(PDOStatement $statement, $bindings) { $result = $statement->execute($bindings); if (strpos(strtoupper($statement->queryString), 'SELECT') === 0) { - return $statement->fetchAll(\PDO::FETCH_CLASS, 'stdClass'); + return $statement->fetchAll(PDO::FETCH_CLASS, 'stdClass'); } elseif (strpos(strtoupper($statement->queryString), 'INSERT') === 0) { @@ -151,6 +178,11 @@ private function execute(\PDOStatement $statement, $bindings) /** * Begin a fluent query against a table. * + * + * // Begin a fluent query against the "users" table + * $query = DB::connection()->table('users'); + * + * * @param string $table * @return Query */ @@ -168,11 +200,16 @@ public function driver() { if ( ! $this->connected()) $this->connect(); - return $this->pdo->getAttribute(\PDO::ATTR_DRIVER_NAME); + return $this->pdo->getAttribute(PDO::ATTR_DRIVER_NAME); } /** * Magic Method for dynamically beginning queries on database tables. + * + * + * // Begin a query against the "users" table + * $query = DB::connection()->users(); + * */ public function __call($method, $parameters) { diff --git a/laravel/database/manager.php b/laravel/database/manager.php index 29d73c5f..54d723d2 100644 --- a/laravel/database/manager.php +++ b/laravel/database/manager.php @@ -7,7 +7,7 @@ class Manager { * * @var array */ - public $connections = array(); + protected $connections = array(); /** * The connector factory instance. @@ -51,6 +51,14 @@ public function __construct(Connector\Factory $connector, $config, $default) * * Note: Database connections are managed as singletons. * + * + * // Get the default database connection + * $connection = DB::connection(); + * + * // Get a database connection by name + * $connection = DB::connection('slave'); + * + * * @param string $connection * @return Database\Connection */ @@ -76,7 +84,13 @@ public function connection($connection = null) /** * Begin a fluent query against a table. * - * This method primarily serves as a short-cut to the $connection->table() method. + * + * // Begin a fluent query against the "users" table using the default connection + * $query = DB::table('users'); + * + * // Begin a fluent query against the "users" table using a specified connection + * $query = DB::table('users', 'slave'); + * * * @param string $table * @param string $connection @@ -91,6 +105,14 @@ public function table($table, $connection = null) * Magic Method for calling methods on the default database connection. * * This provides a convenient API for querying or examining the default database connection. + * + * + * // Perform a query against the default connection + * $results = DB::query('select * from users'); + * + * // Get the name of the PDO driver being used by the default connection + * $driver = DB::driver(); + * */ public function __call($method, $parameters) {