Overview

Namespaces

  • None
  • PHP
  • Wei
    • Validator

Classes

  • Apc
  • App
  • ArrayCache
  • Asset
  • Base
  • BaseCache
  • Bicache
  • Cache
  • Config
  • Cookie
  • Couchbase
  • Counter
  • Db
  • DbCache
  • E
  • Env
  • Error
  • FileCache
  • Gravatar
  • Http
  • Lock
  • Logger
  • Memcache
  • Memcached
  • MongoCache
  • Password
  • PhpError
  • Pinyin
  • Record
  • Redis
  • Request
  • Response
  • Router
  • SafeUrl
  • Session
  • Soap
  • T
  • Ua
  • Upload
  • Url
  • Uuid
  • Validate
  • View
  • WeChatApp
  • Wei
  • Overview
  • Namespace
  • Class
  • Tree

Class Db

A database service inspired by Doctrine DBAL

Wei\Base
Extended by Wei\Db
Namespace: Wei
Author: Twin Huang twinhuang@qq.com
Located at Db.php
Methods summary
public
# __construct( array $options = array() )

Constructor

Constructor

Parameters

$options

Throws

InvalidArgumentException
When option "wei" is not an instance of "Wei\Wei"

Overrides

Wei\Base::__construct()
public Wei\Record
# __invoke( string $table = null )

Create a new instance of a SQL query builder with specified table name

Create a new instance of a SQL query builder with specified table name

Parameters

$table
The name of database table

Returns

Wei\Record
public boolean
# connect( )

Connect to the database server

Connect to the database server

Returns

boolean

Throws

PDOException
When fails to connect to database server
public boolean
# isConnected( )

Check if has connected to the database server

Check if has connected to the database server

Returns

boolean
public
# close( )

Close the database connection

Close the database connection

public
# reconnect( )

Close the current connection and create a new one

Close the current connection and create a new one

public integer
# insert( string $table, array $data )

Executes an INSERT query to insert specified data into table

Executes an INSERT query to insert specified data into table

Parameters

$table
The name of table
$data
An associative array containing column-value pairs

Returns

integer
The number of affected rows
public integer
# insertBatch( string $table, array $data )

Insert batch data into table

Insert batch data into table

Parameters

$table
The name of table
$data
A two-dimensional array

Returns

integer
The number of inserted rows
public integer
# update( string $table, array $data, array $conditions )

Executes a UPDATE query

Executes a UPDATE query

Parameters

$table
The name of table
$data
An associative array containing column-value pairs
$conditions
The conditions to search records

Returns

integer
The number of affected rows
public integer
# delete( string $table, array $conditions )

Executes a DELETE query

Executes a DELETE query

Parameters

$table
The name of table
$conditions
The conditions to search records

Returns

integer
The number of affected rows
public array|false
# select( string $table, string|array $conditions, string $select = '*' )

Executes a SELECT query and return the first result

Executes a SELECT query and return the first result

// Find by the "id" key
$db->select('user', 1);

// Find by specified column
$db->select('user', array('username' => 'twin'));

// Find in list
$db->select('user', array('id' => array(1, 2, 3)));

Parameters

$table
The name of table
$conditions
The "id" key value or an associative array containing column-value pairs
$select
The table columns to select

Returns

array|false
An associative array containing column-value pairs
public array
# selectAll( string $table, boolean $conditions = false, string $select = '*', integer $limit = null )

Executes a SELECT query and return all results

Executes a SELECT query and return all results

// Find by the "id" key
$db->selectAll('user', 1);

// Find by specified column
$db->selectAll('user', array('username' => 'twin'));

// Find in list
$db->selectAll('user', array('id' => array(1, 2, 3)));

Parameters

$table
The name of table
$conditions
The "id" key value or an associative array containing column-value pairs
$select
The table columns to select
$limit
The row number to retrieve

Returns

array
public array|false
# fetch( string $sql, mixed $params = array(), array $types = array() )

Executes a query and returns the first array result

Executes a query and returns the first array result

Parameters

$sql
The SQL query
$params
The query parameters
$types
The parameter types to bind

Returns

array|false
Return an array or false when no result found
public array|false
# fetchAll( string $sql, mixed $params = array(), array $types = array() )

Executes a query and returns all array results

Executes a query and returns all array results

Parameters

$sql
The SQL query
$params
The query parameters
$types
The parameter types to bind

Returns

array|false
Return an array or false when no result found
public string
# fetchColumn( string $sql, mixed $params = array(), integer $column = 0 )

Executes a query and returns a column value of the first row

Executes a query and returns a column value of the first row

Parameters

$sql
The SQL query
$params
The query parameters
$column
The index of column

Returns

string
public integer
# executeUpdate( string $sql, array $params = array(), array $types = array() )

Executes an SQL INSERT/UPDATE/DELETE query with the given parameters and returns the number of affected rows

Executes an SQL INSERT/UPDATE/DELETE query with the given parameters and returns the number of affected rows

Parameters

$sql
The SQL query
$params
The query parameters
$types
The parameter types to bind

Returns

integer
The number of affected rows
public PDOStatement|integer
# query( string $sql, array $params = array(), array $types = array(), boolean $returnRows = false )

Executes an SQL statement, returning a PDOStatement object or the number of affected rows

Executes an SQL statement, returning a PDOStatement object or the number of affected rows

Parameters

$sql
The SQL query
$params
The SQL parameters
$types
The parameter types to bind
$returnRows
Whether returns a PDOStatement object or the number of affected rows

Returns

PDOStatement|integer

Throws

PDOException
public integer
# count( string $table, mixed $conditions = false )

Returns the rows number of table search by specified parameters

Returns the rows number of table search by specified parameters

Parameters

$table
$conditions

Returns

integer
public float
# sum( string $table, string $field, mixed $conditions = false )

Returns the sum of specified table field and conditions

Returns the sum of specified table field and conditions

Parameters

$table
$field
$conditions

Returns

float
public float
# max( string $table, string $field, mixed $conditions = false )

Returns the max value of specified table field and conditions

Returns the max value of specified table field and conditions

Parameters

$table
$field
$conditions

Returns

float
public float
# min( string $table, string $field, mixed $conditions = false )

Returns the min value of specified table field and conditions

Returns the min value of specified table field and conditions

Parameters

$table
$field
$conditions

Returns

float
public float
# avg( string $table, string $field, mixed $conditions = false )

Returns the avg value of specified table field and conditions

Returns the avg value of specified table field and conditions

Parameters

$table
$field
$conditions

Returns

float
public string
# lastInsertId( string $sequence = null )

Returns the ID of the last inserted row or sequence value

Returns the ID of the last inserted row or sequence value

Parameters

$sequence
The name of PostgreSQL sequence

Returns

string
public mixed
# errorCode( )

Fetch the SQLSTATE associated with the last operation on the database handle

Fetch the SQLSTATE associated with the last operation on the database handle

Returns

mixed
public array
# errorInfo( )

Fetch extended error information associated with the last operation on the database handle

Fetch extended error information associated with the last operation on the database handle

Returns

array
public Wei\Record
# init( string $table, array $data = array(), boolean $isNew = true )

Init a record instance

Init a record instance

Parameters

$table
The name of database table
$data
The data for table record
$isNew
Whether it's a new record and have not save to database

Returns

Wei\Record
public Wei\Record|false
# find( string $table, string|array $id )

Find a record from specified table and conditions

Find a record from specified table and conditions

Parameters

$table
The name of table
$id
The primary key value or an associative array containing column-value pairs

Returns

Wei\Record|false
public Wei\Record
# findOne( string $table, string|array $id )

Find a record from specified table and conditions and throws 404 exception when record not found

Find a record from specified table and conditions and throws 404 exception when record not found

Parameters

$table
$id
The primary key value or an associative array containing column-value pairs

Returns

Wei\Record

Throws

Exception
public Wei\Record
# findOrInit( string $table, string $id, array $data = array() )

Find a record, if not found, create a new one from specified data

Find a record, if not found, create a new one from specified data

Parameters

$table
The name of table
$id
The primary key value or an associative array containing column-value pairs
$data
The data to create a new record when record not found

Returns

Wei\Record
public Wei\Record
# findAll( string $table, mixed $where = false )

Find records from specified table and conditions

Find records from specified table and conditions

Parameters

$table
The name of table
$where
The primary key value or an associative array containing column-value pairs

Returns

Wei\Record
public mixed
# addRecordClass( string $table, string $class )

Set the record class for specified table

Set the record class for specified table

Parameters

$table
$class

Returns

mixed
$this
public string
# getRecordClass( string $table )

Returns the record class name of table

Returns the record class name of table

Parameters

$table
The name of table

Returns

string
The record class name for table
public string
# getTable( string $table )

Returns the full table name with prefix

Returns the full table name with prefix

Parameters

$table

Returns

string
public string
# getUser( )

Returns the database username

Returns the database username

Returns

string
public string
# getPassword( )

Returns the database password

Returns the database password

Returns

string
public string
# getHost( )

Returns the hostname on which the database server resides

Returns the hostname on which the database server resides

Returns

string
public string
# getPort( )

Returns the port on which the database server is running

Returns the port on which the database server is running

Returns

string
public string
# getDbname( )

Returns the name of the database

Returns the name of the database

Returns

string
public string
# getDriver( )

Returns the name of PDO driver

Returns the name of PDO driver

Returns

string
public string
# getDsn( )

Returns the PDO DSN

Returns the PDO DSN

Returns

string

Throws

RuntimeException
When database driver is unsupported
public PDO
# getPdo( )

Returns the original PDO object

Returns the original PDO object

Returns

PDO
public string
# getTablePrefix( )

Returns the prefix string of table name

Returns the prefix string of table name

Returns

string
public string
# getLastQuery( )

Returns the last executed SQL query

Returns the last executed SQL query

Returns

string
public array
# getQueries( )

Returns all executed SQL queries

Returns all executed SQL queries

Returns

array
public array
# getTableFields( string $table, boolean $withPrefix = false )

Returns the name of fields of specified table

Returns the name of fields of specified table

Parameters

$table
$withPrefix

Returns

array

Throws

PDOException
protected
# filter( mixed $data, mixed $name )
protected
# buildSqlObject( array & $data, mixed $glue )
protected
# bindParameter( PDOStatement $stmt, array $params, array $types )

Bind parameters to statement object

Bind parameters to statement object

Parameters

$stmt
$params
$types
protected float
# executeAggregate( string $fn, string $table, string $field, mixed $conditions )

Execute a query with aggregate function

Execute a query with aggregate function

Parameters

$fn
$table
$field
$conditions

Returns

float
Methods inherited from Wei\Base
__call(), __get(), getOption(), setOption()
Properties summary
protected string $driver

The name of PDO driver, could be mysql, sqlite or pgsql

The name of PDO driver, could be mysql, sqlite or pgsql

# 'mysql'
protected string $user

The database username

The database username

#
protected string $password

The database password

The database password

#
protected string $host

The hostname on which the database server resides, for mysql and pgsql

The hostname on which the database server resides, for mysql and pgsql

# '127.0.0.1'
protected string $port

The port on which the database server is running, for mysql and pgsql

The port on which the database server is running, for mysql and pgsql

#
protected string $dbname

The name of the database, for mysql and pgsql

The name of the database, for mysql and pgsql

#
protected string $unixSocket

The MySQL Unix socket (shouldn't be used with host or port), for mysql only

The MySQL Unix socket (shouldn't be used with host or port), for mysql only

#
protected string $charset

The character set, for mysql only

The character set, for mysql only

#
protected string $path

The path for sqlite database

The path for sqlite database

#
protected string $dsn

The dsn parameter for PDO constructor

The dsn parameter for PDO constructor

#
protected array $attrs

A key=>value array of driver-specific connection attributes

A key=>value array of driver-specific connection attributes

Link

http://www.php.net/manual/zh/pdo.setattribute.php
# array()
protected PDO $pdo

The PDO object

The PDO object

#
protected callable $beforeConnect

The callback triggers before connect to database

The callback triggers before connect to database

#
protected callable $connectFails

The callback triggers when fails to connect to database

The callback triggers when fails to connect to database

#
protected callable $afterConnect

The callback triggers after connect to database

The callback triggers after connect to database

#
protected callable $beforeQuery

The callback triggers before execute query

The callback triggers before execute query

#
protected callable $afterQuery

The callback triggers after execute query

The callback triggers after execute query

#
protected boolean $isConnected

Whether connected to the database server

Whether connected to the database server

# false
protected array $tables

The database table definitions

The database table definitions

# array()
protected string $recordClass

The base record class when instance a new record object

The base record class when instance a new record object

# 'Wei\Record'
protected array $recordClasses

An associative array that the key is table name and the value is class name

An associative array that the key is table name and the value is class name

# array()
protected string $recordNamespace

The record namespace without ending "\"

The record namespace without ending "\"

#
protected string $tablePrefix

The prefix of table name

The prefix of table name

#
protected boolean $global

Whether use the options from the default db service

Whether use the options from the default db service

# false
protected array $queries

All executed SQL queries

All executed SQL queries

# array()
protected array $tableFields

The field names of table

The field names of table

# array()
protected string $slaveDb

The salve db configuration name

The salve db configuration name

#
Properties inherited from Wei\Base
$providers, $wei
Wei Framework API documentation generated by ApiGen