DB

__construct()

Creates an instance of DB class.

$db = new DB();

autoCommit(int $value)

Disables or enables the default autocommit mode for the current connection.

$db->autoCommit(1);

begin()

Begin a new transaction.

$db->begin();

commit()

Commits the current transaction, making its changes permanent.

$db->commit();

config()

Defines the path to the configuration file.

DB::config('config/database.php');

count()

Returns a count of the number of non-NULL values in the rows retrieved by a SELECT statement.

$db->count();

debug(bool $value)

Dumps the information contained by a prepared statement directly on the output.

$db->debug(true);

delete($modifiers=null)

Deletes rows from table, and returns the number of records deleted. Available modifiers are: LOW_PRIORITY, QUICK, and IGNORE.

$db->delete();
$db->delete('IGNORE');

find($value)

Execute the query as a SELECT statement and retrieve a single row by its id column value.

$db->find(1);

first()

Execute the query as a SELECT statement and retrieve a single row from the result set.

$db->first();

get()

Execute the query as a SELECT statement.

$db->get();

groupBy(string $value=null)

Add a generic GROUP BY clause.

$db->groupBy('id');

having(string $value=null)

Add a generic HAVING clause.

$db->having('cnt > 0');

insert(array $data, $modifiers=null)

Insert record in the database. Available modifiers are: LOW_PRIORITY, DELAYED, HIGH_PRIORITY, IGNORE

$db->insert(['name'=>'John']);
$db->insert(['name'=>'John'], 'IGNORE');

join(string $table, string $conditions)

Add a generic INNER JOIN clause.

$db->join('roles', 'roles.id=users.role_id');

leftJoin(string $table, string $conditions)

Add a generic LEFT JOIN clause.

$db->leftJoin('roles', 'roles.id=users.role_id');

rightJoin(string $table, string $conditions)

Add a generic RIGHT JOIN clause.

$db->rightJoin('roles', 'roles.id=users.role_id');

limit(int $rowCount=null, int $offset=null)

Add a generic LIMIT clause.

$db->limit(10, 20);

orderBy(string $value=null)

Add a generic ORDER BY clause.

$db->orderBy('name ASC');

param(string $key, $value)

Binds a value to a corresponding named placeholder in the SQL statement that was used to prepare the statement.

$db->param(':q', 'John');

raw(string $value)

Returns new Expression()

DB::raw('NOW()');

releaseSavepoint(string $identifier)

Removes the named save point from the set of save points of the current transaction. No commit or rollback occurs.

$db->releaseSavepoint('phase-1');

replace(array $data, $modifiers = null)

Replace a record in the database. Available modifiers are: LOW_PRIORITY, DELAYED.

$db->replace(['email' => 'john@domain.com']);
$db->replace(['email' => 'john@domain.com'], 'DELAYED');

reset()

Reset all query builder's clauses.

$db->reset();

rollback()

Rolls back the current transaction, canceling its changes.

$db->rollback();

rollbackToSavepoint(string $identifier)

Rolls back a transaction to the named save point.

$db->rollbackToSavepoint('phase-1');

savepoint(string $identifier)

Sets a named transaction save point with a name of identifier.

$db->savepoint('phase-1');

select(string $value=null)

Add a generic SELECT clause.

$db->select('users.id, users.name');

table(string $value)

Change table name.

$db->table('users');

truncate()

Empties a table completely.

$db->truncate();

update(array $data, $modifiers=null)

Update records in the database. Available modifiers are: LOW_PRIORITY, IGNORE

$db->update(['name'=>'Peter']);
$db->update(['name'=>'Peter'], 'IGNORE');

upsert(array $data, $modifiers=null)

Insert new records or update the existing ones. Available modifiers are: LOW_PRIORITY, DELAYED, HIGH_PRIORITY, IGNORE

$db->upsert(['email'=>'peter@gmail.com','name'=>'Peter Schmidt']);
$db->upsert(['email'=>'peter@gmail.com','name'=>'Peter Schmidt'], 'LOW_PRIORITY');

value(string $column)

Get a single column's value from the first result of a query.

$db->value('name');

where()

Add a basic where clause to the query.

$db->where('(name LIKE :q OR email LIKE :q)');
$db->where('id', 1);
$db->where('id', '=', 1);
$db->where('id', '>', 1);

Expression

__construct(string $value)

Create an instance of Expression class.

$expr = new Expression('NOW()');

getValue()

Returns the value.

$expr->getValue();

__toString()

Returns the value as string.

echo $expr;

Configuration

__construct(string $username, string $password, string $database, string $host=null, int $port=null, string $driver=null, string $charset=null, string $collation=null)

Create an instance of Configuration class.

$config = new Configuration('root', 'secret', 'test');

getCharset()

Return the charset.

echo $config->getCharset(); // 'utf8mb4'

getCollation()

Return the collation.

echo $config->getCollation(); // 'utf8mb4_general_ci'

getDatabase()

Return the database name.

echo $config->getDatabase(); // 'test'

getDriver()

Return the PDO driver.

echo $config->getDriver(); // 'mysql'

getHost()

Return the host.

echo $config->getHost(); // 'localhost'

getPassword()

Return the password.

echo $config->getPassword(); // 'secret'

getPort()

Return the port.

echo $config->getPort(); // 3306

getUsername()

Return the username.

echo $config->getUsername(); // 'root'

Connection

__construct(Configuration $configuration)

Create an instance of Connection class.

$connection = new Connection($config);

getDbh()

Return an instance of Connection.

var_dump($connection->getDbh() instanceof \PDO); // true

getDsn()

Return the Data Source Name (DSN).

echo $connection->getDsn(); // 'mysql:host=localhost;dbname=test'

connect()

Creates a PDO instance to represent a connection to the requested database.

$connection->connect();

disconnect()

Close a previously established connection to the database.

$connection->disconnect();

reconnect()

Close the current connection and create a new connection.

$connection->reconnect();