refactoring.

This commit is contained in:
Taylor Otwell
2011-09-26 21:48:06 -05:00
parent ed3e3e73cc
commit 54c69d8c9f
7 changed files with 89 additions and 36 deletions

View File

@@ -1,7 +1,4 @@
<?php namespace Laravel\Database;
use PDO;
use PDOStatement;
<?php namespace Laravel\Database; use PDO, PDOStatement;
class Connection {
@@ -49,6 +46,14 @@ class Connection {
/**
* Execute a SQL query against the connection and return a scalar result.
*
* <code>
* // Get the total number of rows on a table
* $count = DB::connection()->scalar('select count(*) from users');
*
* // Get the sum of payment amounts from a table
* $sum = DB::connection()->scalar('select sum(amount) from payments')
* </code>
*
* @param string $sql
* @param array $bindings
* @return int|float
@@ -63,13 +68,21 @@ class Connection {
/**
* Execute a SQL query against the connection and return the first result.
*
* <code>
* // Execute a query against the database connection
* $user = DB::connection()->first('select * from users');
*
* // Execute a query with bound parameters
* $user = DB::connection()->first('select * from users where id = ?', array($id));
* </code>
*
* @param string $sql
* @param array $bindings
* @return object
*/
public function first($sql, $bindings = array())
{
return (count($results = $this->query($sql, $bindings)) > 0) ? $results[0] : null;
if (count($results = $this->query($sql, $bindings)) > 0) return $results[0];
}
/**
@@ -82,6 +95,14 @@ class Connection {
* DELETE -> Number of Rows affected.
* ELSE -> Boolean true / false depending on success.
*
* <code>
* // Execute a query against the database connection
* $users = DB::connection()->query('select * from users');
*
* // Execute a query with bound parameters
* $user = DB::connection()->query('select * from users where id = ?', array($id));
* </code>
*
* @param string $sql
* @param array $bindings
* @return mixed

View File

@@ -18,6 +18,14 @@ class Manager {
*
* Note: Database connections are managed as singletons.
*
* <code>
* // Get the default database connection for the application
* $connection = DB::connection();
*
* // Get a specific connection by passing the connection name
* $connection = DB::connection('mysql');
* </code>
*
* @param string $connection
* @return Connection
*/