Methods

Disconnect from the database when the object is destroyed.

__destruct() : void

// Destroy the database instance unset(static::instances[(string) $db], $db);

[!!] Calling unset($db) is not enough to destroy the database, as it will still be stored in static::$instances.

Returns the database instance name.

__toString() : string

echo (string) $db;

Returns

string

Returns a database cache object

cache(array $result, string $sql, mixed $as_object) : \Fuel\Core\Database_Cached

$db->cache($result, $sql);

Parameters

$result

array

$sql

string

$as_object

mixed

Returns

\Fuel\Core\Database_Cached

Per connection cache controller setter/getter

caching(bool $bool) : mixed

Parameters

$bool

bool

whether to enable it [optional]

Returns

mixedcache boolean when getting, current instance when setting.

Commits nested transaction

commit_transaction() : bool

$db->commit_transaction();

Returns

bool

Connect to the database.

connect() : void

This is called automatically when the first query is executed.

$db->connect();

Exceptions

\Fuel\Core\Database_Exception

Returns the raw connection object for custom method access

connection() : resource

$db->connection()->lastInsertId('id');

Returns

resource

Count the number of records in the last query, without LIMIT or OFFSET applied.

count_last_query() : integer

// Get the total number of records that match the last query $count = $db->count_last_query();

Returns

integer

Count the number of records in a table.

count_records(mixed $table) : integer

// Get the total number of records in the "users" table $count = $db->count_records('users');

Parameters

$table

mixed

table name string or array(query, alias)

Returns

integer

Returns a normalized array describing the SQL data type

datatype(string $type) : array

$db->datatype('char');

Parameters

$type

string

SQL data type

Returns

array

Create a new [Database_Query_Builder_Delete].

delete(string $table) : \Fuel\Core\Database_Query_Builder_Delete

// DELETE FROM users $query = $db->delete('users');

Parameters

$table

string

table to delete from

Returns

Disconnect from the database.

disconnect() : boolean

This is called automatically by [static::__destruct].

$db->disconnect();

Returns

boolean

Sanitize a string by escaping characters that could cause an SQL injection attack.

escape(string $value) : string

$value = $db->escape('any string');

Parameters

$value

string

value to quote

Returns

string

Returns whether or not we have a valid database connection object

has_connection() : bool

$db->has_connection()

Returns

bool

Whether or not the connection is in transaction mode

in_transaction() : bool

$db->in_transaction();

Returns

bool

Create a new [Database_Query_Builder_Insert].

insert(string $table, array $columns) : \Fuel\Core\Database_Query_Builder_Insert

// INSERT INTO users (id, username) $query = $db->insert('users', array('id', 'username'));

Parameters

$table

string

table to insert into

$columns

array

list of column names or array($column, $alias) or object

Returns

Get a singleton Database instance.

instance(string $name, array $config, bool $writable) : \Fuel\Core\Database_Connection

If configuration is not specified, it will be loaded from the database configuration file using the same group as the name.

// Load the default database
$db = static::instance();

// Create a custom configured instance
$db = static::instance('custom', $config);

Parameters

$name

string

instance name

$config

array

configuration parameters

$writable

bool

when replication is enabled, whether to return the master connection

Exceptions

\FuelException

Returns

Lists all of the columns in a table.

list_columns(string $table, string $like) : array

Optionally, a LIKE string can be used to search for specific fields.

// Get all columns from the "users" table
$columns = $db->list_columns('users');

// Get all name-related columns
$columns = $db->list_columns('users', '%name%');

Parameters

$table

string

table to get columns from

$like

string

column to search for

Returns

array

Lists all of the indexes in a table.

list_indexes(string $table, string $like) : array

Optionally, a LIKE string can be used to search for specific indexes by name.

// Get all indexes from the "users" table
$indexes = $db->list_indexes('users');

// Get all name-related columns
$indexes = $db->list_indexes('users', '%name%');

Parameters

$table

string

table to get indexes from

$like

string

index names to search for

Returns

array

List all of the tables in the database.

list_tables(string $like) : array

Optionally, a LIKE string can be used to search for specific tables.

// Get all tables in the current database
$tables = $db->list_tables();

// Get all user-related tables
$tables = $db->list_tables('user%');

Parameters

$like

string

table to search for

Returns

array

Perform an SQL query of the given type.

query(integer $type, string $sql, mixed $as_object) : object: array: integer

// Make a SELECT query and use objects for results $db->query(static::SELECT, 'SELECT * FROM groups', true);

// Make a SELECT query and use "Model_User" for the results
$db->query(static::SELECT, 'SELECT * FROM users LIMIT 1', 'Model_User');

Parameters

$type

integer

static::SELECT, static::INSERT, etc

$sql

string

SQL query

$as_object

mixed

result object class, true for stdClass, false for assoc array

Returns

objectDatabase_Result for SELECT queries
arraylist (insert id, row count) for INSERT queries
integernumber of affected rows for all other queries

Quote a value for an SQL query.

quote(mixed $value) : string

$db->quote(null); // 'null' $db->quote(10); // 10 $db->quote('fred'); // 'fred'

Objects passed to this function will be converted to strings. [Database_Expression] objects will use the value of the expression. [Database_Query] objects will be compiled and converted to a sub-query. All other objects will be converted using the __toString method.

uses \Fuel\Core\static::escape

Parameters

$value

mixed

any value to quote

Returns

string

Quote a database identifier, such as a column name.

quote_identifier(mixed $value) : string

Adds the table prefix to the identifier if a table name is present.

$column = $db->quote_identifier($column);

You can also use SQL methods within identifiers.

// The value of "column" will be quoted
$column = $db->quote_identifier('COUNT("column")');

Objects passed to this function will be converted to strings. [Database_Expression] objects will use the value of the expression. [Database_Query] objects will be compiled and converted to a sub-query. All other objects will be converted using the __toString method.

uses \Fuel\Core\static::table_prefix

Parameters

$value

mixed

any identifier

Returns

string

Quote a database table name and adds the table prefix if needed.

quote_table(mixed $value) : string

$table = $db->quote_table($table);

uses \Fuel\Core\static::quote_identifier
uses \Fuel\Core\static::table_prefix

Parameters

$value

mixed

table name or array(table, alias)

Returns

string

Rollsback nested pending transaction queries.

rollback_transaction($rollback_all) : bool

Rollback to the current level uses SAVEPOINT, it does not work if current RDBMS does not support them. In this case system rollbacks all queries and closes the transaction

$db->rollback_transaction();

Parameters

$rollback_all

Returns

bool

Database schema operations

schema(string $operation, array $params) : \Fuel\Core\Database_Query_Builder_Delete

// CREATE DATABASE database CHARACTER SET utf-8 DEFAULT utf-8 $query = $db->schema('create_database', array('database', 'utf-8'));

Parameters

$operation

string

table to delete from

$params

Returns

Create a new [Database_Query_Builder_Select].

select(array $args) : \Fuel\Core\Database_Query_Builder_Select

Each argument will be treated as a column. To generate a foo AS bar alias, use an array.

// SELECT id, username
$query = $db->select('id', 'username');

// SELECT id AS user_id
$query = $db->select(array('id', 'user_id'));

Parameters

$args

mixed

column name or array($column, $alias) or object

Returns

Set the connection character set.

set_charset(string $charset) : void

This is called automatically by [static::connect].

$db->set_charset('utf8');

Parameters

$charset

string

character set name

Exceptions

\Fuel\Core\Database_Exception

Begins a nested transaction on instance

start_transaction() : bool

$db->start_transaction();

Returns

bool

Return the table prefix defined in the current configuration.

table_prefix(string $table) : string

$prefix = $db->table_prefix();

Parameters

$table

string

Returns

string

Create a new [Database_Query_Builder_Update].

update(string $table) : \Fuel\Core\Database_Query_Builder_Update

// UPDATE users $query = $db->update('users');

Parameters

$table

string

table to update

Returns

Stores the database configuration locally and name the instance.

__construct(string $name, array $config) 

[!!] This method cannot be accessed directly, you must use [static::instance].

Parameters

$name

string

$config

array

Extracts the text between parentheses, if any.

_parse_type(string $type) : array

// Returns: array('CHAR', '6') list($type, $length) = $db->_parse_type('CHAR(6)');

Parameters

$type

string

Returns

arraylist containing the type and length, if any

Commits all pending transactional queries on the driver level

driver_commit() : bool

Returns

bool

Rollback all pending transactional queries on the driver level

driver_rollback() : bool

Returns

bool

Begins a transaction on the driver level

driver_start_transaction() : bool

Returns

bool

Release savepoint of the transaction

release_savepoint(string $name) : boolean

Parameters

$name

string

name of the savepoint

Returns

booleantrue - savepoint was set successfully; false - failed to set savepoint; null - RDBMS does not support savepoints

Rollback savepoint of the transaction

rollback_savepoint(string $name) : boolean

Parameters

$name

string

name of the savepoint

Returns

booleantrue - savepoint was set successfully; false - failed to set savepoint; null - RDBMS does not support savepoints

Sets savepoint of the transaction

set_savepoint(string $name) : boolean

Parameters

$name

string

name of the savepoint

Returns

booleantrue - savepoint was set successfully; false - failed to set savepoint; null - RDBMS does not support savepoints

 Properties

 

<p>Database instances</p>

$instances : array

 

<p>the last query executed</p>

$last_query : string

 

<p>Configuration array</p>

$_config : array

 

<p>Raw server connection</p>

$_connection : resource

 

<p>Character that is used to quote identifiers</p>

$_identifier : string

 

<p>allows transactions</p>

$_in_transaction : bool

 

<p>Instance name</p>

$_instance : string

 

<p>Cache of the name of the readonly connection</p>

$_readonly : string

 

<p>Instance of the database schema class</p>

$_schema : \Fuel\Core\Database_Schema

 

<p>Transaction nesting depth counter. Should be modified AFTER a driver has changed the level successfully</p>

$_transaction_depth : int