Methods summary
public
|
#
__construct( array $options = array() )
Constructor
Parameters
Throws
Overrides
|
public
string
|
#
getTable( )
Return the record table name
Return the record table name
Returns
string
|
public
mixed
|
#
setTable( string $table )
Set the record table name
Set the record table name
Parameters
Returns
mixed $this
|
public
array
|
#
toArray( array $returnFields = array() )
Returns the record data as array
Returns the record data as array
Parameters
- $returnFields
- A indexed array specified the fields to return
Returns
array
|
public
array
|
#
toJson( array $returnFields = array() )
Returns the record and relative records data as JSON string
Returns the record and relative records data as JSON string
Parameters
- $returnFields
- A indexed array specified the fields to return
Returns
array
|
public
mixed
|
#
fromArray( array|ArrayAccess $data )
Import a PHP array in this record
Import a PHP array in this record
Parameters
Returns
mixed $this
|
protected
mixed
|
#
setData( array $data )
Import a PHP array in this record
Import a PHP array in this record
Parameters
Returns
mixed $this
|
public
mixed
|
#
save( array $data = array() )
Save the record or data to database
Save the record or data to database
Parameters
Returns
mixed $this
|
public
mixed
|
#
destroy( mixed $conditions = false )
Delete the current record and trigger the beforeDestroy and afterDestroy callback
Delete the current record and trigger the beforeDestroy and afterDestroy callback
Parameters
Returns
mixed $this
|
public
mixed
|
#
reload( )
Reload the record data from database
Reload the record data from database
Returns
mixed $this
|
public
mixed
|
#
saveColl( array $data, array $extraData = array(), boolean $sort = false )
Merges data into collection and save to database, including insert, update and delete
Merges data into collection and save to database, including insert, update and delete
Parameters
- $data
- A two-dimensional array
- $extraData
- The extra data for new rows
- $sort
-
Returns
mixed $this
|
public
string
|
#
get( string $name )
Receives the record field value
Receives the record field value
Parameters
Returns
string
Throws
|
public
mixed
|
#
set( string $name, mixed $value = null )
Set the record field value
Set the record field value
Parameters
Returns
mixed $this
Throws
|
public
mixed
|
#
setAll( string $name, mixed $value )
Set field value for every record in collection
Set field value for every record in collection
Parameters
Returns
mixed $this
|
public
array
|
#
getAll( string $name )
Return the value of field from every record in collection
Return the value of field from every record in collection
Parameters
Returns
array
|
public
mixed
|
#
remove( string $name )
Remove field value
Parameters
Returns
mixed $this
|
public
mixed
|
#
incr( string $name, integer $offset )
Increment a field
Parameters
Returns
mixed $this
|
public
mixed
|
#
decr( string $name, integer $offset )
Decrement a field
Parameters
Returns
mixed $this
|
public
mixed
|
#
detach( boolean $bool = true )
Set the detach status for current record
Set the detach status for current record
Parameters
Returns
mixed $this
|
public
boolean
|
#
isNew( )
Check if it's a new record and has not save to database
Check if it's a new record and has not save to database
Returns
boolean
|
public
boolean
|
#
isChanged( string $field = null )
Check if the record's data or specified field is changed
Check if the record's data or specified field is changed
Parameters
Returns
boolean
|
public
boolean
|
#
isDestroyed( )
Check if the record has been removed from the database
Check if the record has been removed from the database
Returns
boolean
|
public
boolean
|
#
isDetached( )
Check if the record is waiting to remove from database
Check if the record is waiting to remove from database
Returns
boolean
|
public
|
|
public
boolean
|
#
isLoaded( )
Returns whether the data is loaded
Returns whether the data is loaded
Returns
boolean
|
public
mixed
|
#
setPrimaryKey( string $primaryKey )
Sets the primary key field
Sets the primary key field
Parameters
Returns
mixed $this
|
public
string
|
#
getPrimaryKey( )
Returns the primary key field
Returns the primary key field
Returns
string
|
public
array
|
#
getFields( )
Returns the name of fields of current table
Returns the name of fields of current table
Returns
array
|
public
string|array
|
#
getChangedData( string $field = null )
Return the field data before changed
Return the field data before changed
Parameters
Returns
string|array
|
public
integer
|
#
getState( )
Get the state of this query builder instance
Get the state of this query builder instance
Returns
integer
|
public
mixed
|
#
execute( )
Execute this query using the bound parameters and their types
Execute this query using the bound parameters and their types
Returns
mixed
|
public
false|Wei\Record
|
#
find( mixed $conditions = false )
Executes the generated SQL and returns the found record object or false
Executes the generated SQL and returns the found record object or false
Parameters
Returns
|
public
mixed
|
#
findOrInit( mixed $conditions, array $data = array() )
Find a record by specified conditions and init with the specified data if record not found
Find a record by specified conditions and init with the specified data if record not found
Parameters
Returns
mixed $this
|
public
mixed
|
#
findOne( mixed $conditions = false )
Find a record by specified conditions and throws 404 exception if record not found
Find a record by specified conditions and throws 404 exception if record not found
Parameters
Returns
mixed $this
Throws
|
public
mixed
|
#
findAll( mixed $conditions = false )
Executes the generated SQL and returns the found record collection object or false
Executes the generated SQL and returns the found record collection object or false
Parameters
Returns
mixed $this[]
|
public
mixed
|
#
findById( mixed $value )
Find a record by primary key value
Find a record by primary key value
Parameters
Returns
mixed $this|false
|
public
mixed
|
#
findOneById( mixed $value )
Find a record by primary key value and throws 404 exception if record not found
Find a record by primary key value and throws 404 exception if record not found
Parameters
Returns
mixed $this
|
public
mixed
|
#
findOrInitById( mixed $value, array $data = array() )
Find a record by primary key value and init with the specified data if record not found
Find a record by primary key value and init with the specified data if record not found
Parameters
Returns
mixed $this
|
public
array|false
|
#
fetch( mixed $conditions = false )
Executes the generated query and returns the first array result
Executes the generated query and returns the first array result
Parameters
Returns
array|false
|
public
array|false
|
#
fetchAll( mixed $conditions = false )
Executes the generated query and returns all array results
Executes the generated query and returns all array results
Parameters
Returns
array|false
|
public
integer
|
#
count( mixed $conditions = false, string $count = '1' )
Execute a COUNT query to receive the rows number
Execute a COUNT query to receive the rows number
Parameters
- $conditions
-
- $count
- The argument for SQL COUNT method
Returns
integer
Implementation of
|
public
integer
|
#
length( )
Returns the record number in collection
Returns the record number in collection
Returns
integer
|
public
integer
|
#
size( )
Returns the record number in collection
Returns the record number in collection
Returns
integer
|
public
integer
|
#
update( array $set = array() )
Execute a update query with specified data
Execute a update query with specified data
Parameters
Returns
integer
|
public
mixed
|
#
delete( mixed $conditions = false )
Execute a delete query with specified conditions
Execute a delete query with specified conditions
Parameters
Returns
mixed
|
public
mixed
|
#
offset( integer $offset )
Sets the position of the first result to retrieve (the "offset")
Sets the position of the first result to retrieve (the "offset")
Parameters
- $offset
- The first result to return
Returns
mixed $this
|
public
mixed
|
#
limit( integer $limit )
Sets the maximum number of results to retrieve (the "limit")
Sets the maximum number of results to retrieve (the "limit")
Parameters
- $limit
- The maximum number of results to retrieve
Returns
mixed $this
|
public
mixed
|
#
page( integer $page )
Sets the page number, the "OFFSET" value is equals "($page - 1) * LIMIT"
Sets the page number, the "OFFSET" value is equals "($page - 1) * LIMIT"
Parameters
Returns
mixed $this
|
public
mixed
|
#
select( mixed $select = null )
Specifies an item that is to be returned in the query result.
Replaces any previously specified selections, if any.
Specifies an item that is to be returned in the query result.
Replaces any previously specified selections, if any.
Parameters
- $select
- The selection expressions.
Returns
mixed $this
|
public
mixed
|
#
addSelect( mixed $select = null )
Adds an item that is to be returned in the query result.
Adds an item that is to be returned in the query result.
Parameters
- $select
- The selection expression.
Returns
mixed $this
|
public
mixed
|
#
from( string $from )
Sets table for FROM query
Sets table for FROM query
Parameters
Returns
mixed $this
|
public
mixed
|
#
join( string $table, string $on = null )
Adds a inner join to the query
Adds a inner join to the query
Parameters
- $table
- The table name to join
- $on
- The condition for the join
Returns
mixed $this
|
public
mixed
|
#
innerJoin( string $table, string $on = null )
Adds a inner join to the query
Adds a inner join to the query
Parameters
- $table
- The table name to join
- $on
- The condition for the join
Returns
mixed $this
|
public
mixed
|
#
leftJoin( string $table, string $on = null )
Adds a left join to the query
Adds a left join to the query
Parameters
- $table
- The table name to join
- $on
- The condition for the join
Returns
mixed $this
|
public
mixed
|
#
rightJoin( string $table, string $on = null )
Adds a right join to the query
Adds a right join to the query
Parameters
- $table
- The table name to join
- $on
- The condition for the join
Returns
mixed $this
|
public
mixed
|
#
where( mixed $conditions, array $params = array(), array $types = array() )
Specifies one or more restrictions to the query result.
Replaces any previously specified restrictions, if any.
Specifies one or more restrictions to the query result.
Replaces any previously specified restrictions, if any.
$user = wei()->db('user')->where('id = 1');
$user = wei()->db('user')->where('id = ?', 1);
$users = wei()->db('user')->where(array('id' => '1', 'username' => 'twin'));
$users = wei()->where(array('id' => array('1', '2', '3')));
Parameters
- $conditions
- The WHERE conditions
- $params
- The condition parameters
- $types
- The parameter types
Returns
mixed $this
|
public
mixed
|
#
andWhere( string $conditions, array $params = array(), array $types = array() )
Adds one or more restrictions to the query results, forming a logical
conjunction with any previously specified restrictions
Adds one or more restrictions to the query results, forming a logical
conjunction with any previously specified restrictions
Parameters
- $conditions
- The WHERE conditions
- $params
- The condition parameters
- $types
- The parameter types
Returns
mixed $this
|
public
mixed
|
#
orWhere( string $conditions, array $params = array(), array $types = array() )
Adds one or more restrictions to the query results, forming a logical
disjunction with any previously specified restrictions.
Adds one or more restrictions to the query results, forming a logical
disjunction with any previously specified restrictions.
Parameters
- $conditions
- The WHERE conditions
- $params
- The condition parameters
- $types
- The parameter types
Returns
mixed $this
|
public
mixed
|
#
groupBy( mixed $groupBy )
Specifies a grouping over the results of the query.
Replaces any previously specified groupings, if any.
Specifies a grouping over the results of the query.
Replaces any previously specified groupings, if any.
Parameters
- $groupBy
- The grouping expression.
Returns
mixed $this
|
public
mixed
|
#
addGroupBy( mixed $groupBy )
Adds a grouping expression to the query.
Adds a grouping expression to the query.
Parameters
- $groupBy
- The grouping expression.
Returns
mixed $this
|
public
mixed
|
#
having( string $conditions, array $params = array(), array $types = array() )
Specifies a restriction over the groups of the query.
Replaces any previous having restrictions, if any.
Specifies a restriction over the groups of the query.
Replaces any previous having restrictions, if any.
Parameters
- $conditions
- The having conditions
- $params
- The condition parameters
- $types
- The parameter types
Returns
mixed $this
|
public
mixed
|
#
andHaving( string $conditions, array $params = array(), array $types = array() )
Adds a restriction over the groups of the query, forming a logical
conjunction with any existing having restrictions.
Adds a restriction over the groups of the query, forming a logical
conjunction with any existing having restrictions.
Parameters
- $conditions
- The HAVING conditions to append
- $params
- The condition parameters
- $types
- The parameter types
Returns
mixed $this
|
public
mixed
|
#
orHaving( string $conditions, array $params = array(), array $types = array() )
Adds a restriction over the groups of the query, forming a logical
disjunction with any existing having restrictions.
Adds a restriction over the groups of the query, forming a logical
disjunction with any existing having restrictions.
Parameters
- $conditions
- The HAVING conditions to add
- $params
- The condition parameters
- $types
- The parameter types
Returns
mixed $this
|
public
mixed
|
#
orderBy( string $sort, string $order = 'ASC' )
Specifies an ordering for the query results.
Replaces any previously specified orderings, if any.
Specifies an ordering for the query results.
Replaces any previously specified orderings, if any.
Parameters
- $sort
- The ordering expression.
- $order
- The ordering direction.
Returns
mixed $this
|
public
mixed
|
#
addOrderBy( string $sort, string $order = 'ASC' )
Adds an ordering to the query results.
Adds an ordering to the query results.
Parameters
- $sort
- The ordering expression.
- $order
- The ordering direction.
Returns
mixed $this
|
public
mixed
|
#
desc( string $field )
Adds a DESC ordering to the query
Adds a DESC ordering to the query
Parameters
Returns
mixed $this
|
public
mixed
|
#
asc( string $field )
Add an ASC ordering to the query
Add an ASC ordering to the query
Parameters
Returns
mixed $this
|
public
mixed
|
#
indexBy( string $field )
Specifies a field to be the key of the fetched array
Specifies a field to be the key of the fetched array
Parameters
Returns
mixed $this
|
protected
array
|
#
executeIndexBy( array $data, string $field )
Parameters
Returns
array
Throws
|
public
mixed
|
#
getSqlPart( string $name )
Returns a SQL query part by its name
Returns a SQL query part by its name
Parameters
- $name
- The name of SQL part
Returns
mixed
|
public
array
|
|
public
mixed
|
#
resetSqlParts( array $name = null )
Reset all SQL parts
Parameters
Returns
mixed $this
|
public
mixed
|
#
resetSqlPart( string $name )
Reset single SQL part
Parameters
Returns
mixed $this
|
public
mixed
|
#
setParameter( string|integer $key, mixed $value, string|null $type = null )
Sets a query parameter for the query being constructed
Sets a query parameter for the query being constructed
Parameters
- $key
- The parameter position or name
- $value
- The parameter value
- $type
- PDO::PARAM_*
Returns
mixed $this
|
public
mixed
|
#
getParameter( mixed $key )
Gets a (previously set) query parameter of the query being constructed
Gets a (previously set) query parameter of the query being constructed
Parameters
- $key
- The key (index or name) of the bound parameter
Returns
mixed The value of the bound parameter
|
public
mixed
|
#
setParameters( array $params, array $types = array() )
Sets a collection of query parameters for the query being constructed
Sets a collection of query parameters for the query being constructed
Parameters
- $params
- The query parameters to set
- $types
- The query parameters types to set
Returns
mixed $this
|
public
array
|
#
getParameters( )
Gets all defined query parameters for the query being constructed.
Gets all defined query parameters for the query being constructed.
Returns
array The currently defined query parameters.
|
public
string
|
#
getSql( )
Get the complete SQL string formed by the current specifications of this QueryBuilder
Get the complete SQL string formed by the current specifications of this QueryBuilder
Returns
string The sql query string
|
protected
string
|
#
getSqlForSelect( boolean $count = false )
Converts this instance into an SELECT string in SQL
Converts this instance into an SELECT string in SQL
Parameters
Returns
string
|
protected
|
#
getSqlForCount( )
Converts this instance into an SELECT COUNT string in SQL
Converts this instance into an SELECT COUNT string in SQL
|
protected
string
|
#
getSqlForUpdate( )
Converts this instance into an UPDATE string in SQL.
Converts this instance into an UPDATE string in SQL.
Returns
string
|
protected
string
|
#
getSqlForDelete( )
Converts this instance into a DELETE string in SQL.
Converts this instance into a DELETE string in SQL.
Returns
string
|
public
boolean
|
#
offsetExists( string $offset )
Check if the offset exists
Check if the offset exists
Parameters
Returns
boolean
Implementation of
|
public
mixed
|
#
offsetGet( string $offset )
Get the offset value
Parameters
Returns
mixed
Implementation of
|
public
|
#
offsetSet( string $offset, mixed $value )
Set the offset value
Parameters
Implementation of
|
public
|
#
offsetUnset( string $offset )
Unset the offset
Parameters
Implementation of
|
public
ArrayIterator
|
#
getIterator( )
Retrieve an array iterator
Retrieve an array iterator
Returns
Implementation of
|
protected
mixed
|
#
add( string $sqlPartName, string $sqlPart, boolean $append = false, string $type = null )
Either appends to or replaces a single, generic query part.
Either appends to or replaces a single, generic query part.
The available parts are: 'select', 'from', 'set', 'where',
'groupBy', 'having', 'orderBy', 'limit' and 'offset'.
Parameters
- $sqlPartName
-
- $sqlPart
-
- $append
-
- $type
-
Returns
mixed $this
|
protected
string
|
#
processCondition( mixed $conditions, array $params, array $types )
Generate condition string for WHERE or Having statement
Generate condition string for WHERE or Having statement
Parameters
- $conditions
-
- $params
-
- $types
-
Returns
string
|
protected
|
#
loadData( integer|string $offset )
Load record by array offset
Load record by array offset
Parameters
|
public
mixed
|
#
filter( Closure $fn )
Filters elements of the collection using a callback function
Filters elements of the collection using a callback function
Parameters
Returns
mixed $this
|
protected
|
|
protected
|
#
afterLoad( )
The method called after load a record
The method called after load a record
|
protected
|
#
afterFind( )
The method called after find a record
The method called after find a record
|
public
|
#
beforeSave( )
The method called before save a record
The method called before save a record
|
public
|
#
afterSave( )
The method called after save a record
The method called after save a record
|
public
|
#
beforeCreate( )
The method called before insert a record
The method called before insert a record
|
public
|
#
afterCreate( )
The method called after insert a record
The method called after insert a record
|
public
|
#
beforeUpdate( )
The method called before update a record
The method called before update a record
|
public
|
#
afterUpdate( )
The method called after update a record
The method called after update a record
|
public
|
#
beforeDestroy( )
The method called before delete a record
The method called before delete a record
|
public
|
#
afterDestroy( )
The method called after delete a record
The method called after delete a record
|
Properties summary
protected
string
|
$table
|
|
protected
string
|
$fullTable
The complete record table name with table prefix
The complete record table name with table prefix
|
|
protected
array
|
$fields
The table fields
If leave it blank, it will automatic generate form the database table,
or fill it to speed up the record
The table fields
If leave it blank, it will automatic generate form the database table,
or fill it to speed up the record
|
|
protected
string
|
$primaryKey
|
|
protected
boolean
|
$isNew
Whether it's a new record and has not save to database
Whether it's a new record and has not save to database
|
|
protected
array|Wei\Record[]
|
$data
|
|
protected
boolean
|
$isChanged
Whether the record's data is changed
Whether the record's data is changed
|
|
protected
array
|
$changedData
The record data before changed
The record data before changed
|
|
protected
boolean
|
$isDestroyed
Whether the record has been removed from database
Whether the record has been removed from database
|
|
protected
boolean
|
$detached
Whether the record is waiting to remove from database
Whether the record is waiting to remove from database
|
|
protected
boolean
|
$loaded
Whether the data is loaded
Whether the data is loaded
|
|
protected
boolean
|
$isColl
Whether it contains multiple or single row data
Whether it contains multiple or single row data
|
|
protected
array
|
$sqlParts
|
#
array(
'select' => array(),
'from' => null,
'join' => array(),
'set' => array(),
'where' => null,
'groupBy' => array(),
'having' => null,
'orderBy' => array(),
'limit' => null,
'offset' => null,
)
|
protected
string
|
$indexBy
A field to be the key of the fetched array, if not provided, return
default number index array
A field to be the key of the fetched array, if not provided, return
default number index array
|
|
protected
string
|
$sql
The complete SQL string for this query.
The complete SQL string for this query.
|
|
protected
array
|
$params
|
|
protected
array
|
$paramTypes
The parameter type map of this query
The parameter type map of this query
|
|
protected
integer
|
$type
The type of query this is. Can be select, update or delete
The type of query this is. Can be select, update or delete
|
|
protected
integer
|
$state
The state of the query object. Can be dirty or clean
The state of the query object. Can be dirty or clean
|
|
protected
callable
|
$afterLoad
The callback triggered after load a record
The callback triggered after load a record
|
|
protected
callable
|
$afterFind
The callback triggered after fetch a record from database
The callback triggered after fetch a record from database
|
|
protected
callable
|
$beforeSave
The callback triggered before save a record
The callback triggered before save a record
|
|
protected
callable
|
$afterSave
The callback triggered after save a record
The callback triggered after save a record
|
|
protected
callable
|
$beforeCreate
The callback triggered before insert a record
The callback triggered before insert a record
|
|
protected
callable
|
$afterCreate
The callback triggered after insert a record
The callback triggered after insert a record
|
|
protected
callable
|
$beforeUpdate
The callback triggered after update a record
The callback triggered after update a record
|
|
protected
callable
|
$afterUpdate
The callback triggered after update a record
The callback triggered after update a record
|
|
protected
callable
|
$beforeDestroy
The callback triggered before delete a record
The callback triggered before delete a record
|
|
protected
callable
|
$afterDestroy
The callback triggered after delete a record
The callback triggered after delete a record
|
|