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 Record

A base database record class

Wei\Base
Extended by Wei\Record implements ArrayAccess, IteratorAggregate, Countable
Namespace: Wei
Author: Twin Huang twinhuang@qq.com
Located at Record.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 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

$table

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

$data

Returns

mixed
$this
protected mixed
# setData( array $data )

Import a PHP array in this record

Import a PHP array in this record

Parameters

$data

Returns

mixed
$this
public mixed
# save( array $data = array() )

Save the record or data to database

Save the record or data to database

Parameters

$data

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

$conditions

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

$name

Returns

string

Throws

InvalidArgumentException
When field not found
public mixed
# set( string $name, mixed $value = null )

Set the record field value

Set the record field value

Parameters

$name
$value

Returns

mixed
$this

Throws

InvalidArgumentException
public mixed
# setAll( string $name, mixed $value )

Set field value for every record in collection

Set field value for every record in collection

Parameters

$name
$value

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

$name

Returns

array
public mixed
# remove( string $name )

Remove field value

Remove field value

Parameters

$name
The name of field

Returns

mixed
$this
public mixed
# incr( string $name, integer $offset )

Increment a field

Increment a field

Parameters

$name
$offset

Returns

mixed
$this
public mixed
# decr( string $name, integer $offset )

Decrement a field

Decrement a field

Parameters

$name
$offset

Returns

mixed
$this
public mixed
# detach( boolean $bool = true )

Set the detach status for current record

Set the detach status for current record

Parameters

$bool

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

$field

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
# isColl( )
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

$primaryKey

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

$field

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

$conditions

Returns

false|Wei\Record
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

$conditions
$data

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

$conditions

Returns

mixed
$this

Throws

Exception
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

$conditions

Returns

mixed
$this[]
public mixed
# findById( mixed $value )

Find a record by primary key value

Find a record by primary key value

Parameters

$value

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

$value

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

$value
$data

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

$conditions

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

$conditions

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

Countable::count()
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

$set

Returns

integer
public mixed
# delete( mixed $conditions = false )

Execute a delete query with specified conditions

Execute a delete query with specified conditions

Parameters

$conditions

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

$page
The page number

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

$from
The table

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

$field
The name of field

Returns

mixed
$this
public mixed
# asc( string $field )

Add an ASC ordering to the query

Add an ASC ordering to the query

Parameters

$field
The name of field

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

$field

Returns

mixed
$this
protected array
# executeIndexBy( array $data, string $field )

Parameters

$data
$field

Returns

array

Throws

RuntimeException
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
# getSqlParts( )

Get all SQL parts

Get all SQL parts

Returns

array
$sqlParts
public mixed
# resetSqlParts( array $name = null )

Reset all SQL parts

Reset all SQL parts

Parameters

$name

Returns

mixed
$this
public mixed
# resetSqlPart( string $name )

Reset single SQL part

Reset single SQL part

Parameters

$name

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

$count

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

$offset

Returns

boolean

Implementation of

ArrayAccess::offsetExists()
public mixed
# offsetGet( string $offset )

Get the offset value

Get the offset value

Parameters

$offset

Returns

mixed

Implementation of

ArrayAccess::offsetGet()
public
# offsetSet( string $offset, mixed $value )

Set the offset value

Set the offset value

Parameters

$offset
$value

Implementation of

ArrayAccess::offsetSet()
public
# offsetUnset( string $offset )

Unset the offset

Unset the offset

Parameters

$offset

Implementation of

ArrayAccess::offsetUnset()
public ArrayIterator
# getIterator( )

Retrieve an array iterator

Retrieve an array iterator

Returns

ArrayIterator

Implementation of

IteratorAggregate::getIterator()
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

$offset
public mixed
# filter( Closure $fn )

Filters elements of the collection using a callback function

Filters elements of the collection using a callback function

Parameters

$fn

Returns

mixed
$this
protected
# triggerCallback( string $name )

Trigger a callback

Trigger a callback

Parameters

$name
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

Methods inherited from Wei\Base
__call(), __get(), getOption(), setOption()
Magic methods summary
public Wei\Record
# db( mixed $table = null )

Create a new record object

Create a new record object

Parameters

$table

Returns

Wei\Record
Constants summary
integer SELECT
# 0
integer DELETE
# 1
integer UPDATE
# 2
integer STATE_DIRTY

The builder states.

The builder states.

# 0
integer STATE_CLEAN
# 1
Properties summary
protected string $table

The record table name

The record table name

#
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

# array()
protected string $primaryKey

The primary key field

The primary key field

# 'id'
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

# true
protected array|Wei\Record[] $data

The record data

The record data

# array()
protected boolean $isChanged

Whether the record's data is changed

Whether the record's data is changed

# false
protected array $changedData

The record data before changed

The record data before changed

# array()
protected boolean $isDestroyed

Whether the record has been removed from database

Whether the record has been removed from database

# false
protected boolean $detached

Whether the record is waiting to remove from database

Whether the record is waiting to remove from database

# false
protected boolean $loaded

Whether the data is loaded

Whether the data is loaded

# false
protected boolean $isColl

Whether it contains multiple or single row data

Whether it contains multiple or single row data

#
protected array $sqlParts

The parts of SQL

The parts of SQL

# 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

The query parameters

The query parameters

# array()
protected array $paramTypes

The parameter type map of this query

The parameter type map of this query

# array()
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

# Wei\Record::SELECT
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

# Wei\Record::STATE_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

#
Properties inherited from Wei\Base
$providers, $wei
Magic properties summary
public Wei\Db $db

A database service inspired by Doctrine DBAL

Wei Framework API documentation generated by ApiGen