\ActiveRecord

Simple implement of active record in PHP.<br /> Using magic function to implement more smarty functions.<br /> Can using chain method calls, to build concise and compactness program.<br />

Summary

Methods
Properties
Constants
__construct()
__set()
__get()
reset()
dirty()
setDb()
find()
findAll()
delete()
update()
insert()
execute()
_query()
__call()
wrap()
addCondition()
join()
__unset()
$data
$db
$operators
$sqlParts
$defaultSqlExpressions
$table
$primaryKey
$dirty
$params
$relations
$count
BELONGS_TO
HAS_MANY
HAS_ONE
PREFIX
getRelation()
_buildSql()
_filterParam()
_addExpression()
_addCondition()
$sqlExpressions
N/A
_buildSqlCallback()
No private properties found
N/A

Constants

BELONGS_TO

BELONGS_TO

HAS_MANY

HAS_MANY

HAS_ONE

HAS_ONE

PREFIX

PREFIX

Properties

$data

$data : array

Type

array — Stored the attributes of the current object

$db

$db : \PDO

Type

\PDO — static property to connect database.

$operators

$operators : array

Type

array — maping the function name and the operator, to build Expressions in WHERE condition. <pre>user can call it like this: $user->isnotnull()->eq('id', 1); will create Expressions can explain to SQL: WHERE user.id IS NOT NULL AND user.id = :ph1</pre>

$sqlParts

$sqlParts : array

Type

array — Part of SQL, maping the function name and the operator to build SQL Part. <pre>call function like this: $user->order('id desc', 'name asc')->limit(2,1); can explain to SQL: ORDER BY id desc, name asc limit 2,1</pre>

$defaultSqlExpressions

$defaultSqlExpressions : array

Type

array — Static property to stored the default Sql Expressions values.

$table

$table : string

Type

string — The table name in database.

$primaryKey

$primaryKey : string

Type

string — The primary key of this ActiveRecord, just suport single primary key.

$dirty

$dirty : array

Type

array — Stored the drity data of this object, when call "insert" or "update" function, will write this data into database.

$params

$params : array

Type

array — Stored the params will bind to SQL when call PDOStatement::execute(),

$relations

$relations : array

Type

array — Stored the configure of the relation, or target of the relation.

$count

$count : integer

Type

integer — The count of bind params, using this count and const "PREFIX" (:ph) to generate place holder in SQL.

$sqlExpressions

$sqlExpressions : array

Type

array — Stored the Expressions of the SQL.

Methods

__construct()

__construct(  $config = array()) 

Parameters

$config

__set()

__set(  $var,   $val) 

magic function to SET values of the current object.

Parameters

$var
$val

__get()

__get(  $var) 

magic function to GET the values of current object.

Parameters

$var

reset()

reset() : \ActiveRecord

function to reset the $params and $sqlExpressions.

Returns

\ActiveRecord

return $this, can using chain method calls.

dirty()

dirty(array  $dirty = array()) : \ActiveRecord

function to SET or RESET the dirty data.

Parameters

array $dirty

The dirty data will be set, or empty array to reset the dirty data.

Returns

\ActiveRecord

return $this, can using chain method calls.

setDb()

setDb(\PDO  $db) 

set the DB connection.

Parameters

\PDO $db

find()

find(integer  $id = null) : boolean|\ActiveRecord

function to find one record and assign in to current object.

Parameters

integer $id

If call this function using this param, will find record by using this id. If not set, just find the first record in database.

Returns

boolean|\ActiveRecord

if find record, assign in to current object and return it, other wise return "false".

findAll()

findAll() : array

function to find all records in database.

Returns

array —

return array of ActiveRecord

delete()

delete() : boolean

function to delete current record in database.

Returns

boolean

update()

update() : boolean|\ActiveRecord

function to build update SQL, and update current record in database, just write the dirty data into database.

Returns

boolean|\ActiveRecord

if update success return current object, other wise return false.

insert()

insert() : boolean|\ActiveRecord

function to build insert SQL, and insert current record into database.

Returns

boolean|\ActiveRecord

if insert success return current object, other wise return false.

execute()

execute(string  $sql, array  $param = array()) : boolean

helper function to exec sql.

Parameters

string $sql

The SQL need to be execute.

array $param

The param will be bind to PDOStatement.

Returns

boolean

_query()

_query(string  $sql, array  $param = array(), \ActiveRecord  $obj = null, boolean  $single = false) : boolean|\ActiveRecord|array

helper function to query one record by sql and params.

Parameters

string $sql

The SQL to find record.

array $param

The param will be bind to PDOStatement.

\ActiveRecord $obj

The object, if find record in database, will assign the attributes in to this object.

boolean $single

if set to true, will find record and fetch in current object, otherwise will find all records.

Returns

boolean|\ActiveRecord|array

__call()

__call(string  $name, array  $args) : mixed

magic function to make calls witch in function mapping stored in $operators and $sqlPart.

also can call function of PDO object.

Parameters

string $name

function name

array $args

The arguments of the function.

Returns

mixed —

Return the result of callback or the current object to make chain method calls.

wrap()

wrap(string  $op = null) : \ActiveRecord

make wrap when build the SQL expressions of WHWRE.

Parameters

string $op

If give this param will build one WrapExpressions include the stored expressions add into WHWRE. otherwise wil stored the expressions into array.

Returns

\ActiveRecord

return $this, can using chain method calls.

addCondition()

addCondition(string  $field, string  $operator, mixed  $value, string  $op = 'AND', string  $name = 'where') 

helper function to add condition into WHERE.

create the SQL Expressions.

Parameters

string $field

The field name, the source of Expressions

string $operator
mixed $value

the target of the Expressions

string $op

the operator to concat this Expressions into WHERE or SET statment.

string $name

The Expression will contact to.

join()

join(string  $table, string  $on, string  $type = 'LEFT') 

helper function to add condition into JOIN.

create the SQL Expressions.

Parameters

string $table

The join table name

string $on

The condition of ON

string $type

The join type, like "LEFT", "INNER", "OUTER"

__unset()

__unset(  $var) 

magic function to UNSET values of the current object.

Parameters

$var

getRelation()

getRelation(string  $name) : mixed

helper function to get relation of this object.

There was three types of relations: {BELONGS_TO, HAS_ONE, HAS_MANY}

Parameters

string $name

The name of the relation, the array key when defind the relation.

Returns

mixed

_buildSql()

_buildSql(array  $sqls = array()) : string

helper function to build SQL with sql parts.

Parameters

array $sqls

The SQL part will be build.

Returns

string

_filterParam()

_filterParam(mixed  $value) : mixed

helper function to build place holder when make SQL expressions.

Parameters

mixed $value

the value will bind to SQL, just store it in $this->params.

Returns

mixed —

$value

_addExpression()

_addExpression(\Expressions  $exp, string  $operator) 

helper function to make wrapper. Stored the expression in to array.

Parameters

\Expressions $exp

The expression will be stored.

string $operator

The operator to concat this Expressions into WHERE statment.

_addCondition()

_addCondition(\Expressions  $exp, string  $operator, string  $name = 'where') 

helper function to add condition into WHERE.

Parameters

\Expressions $exp

The expression will be concat into WHERE or SET statment.

string $operator

the operator to concat this Expressions into WHERE or SET statment.

string $name

The Expression will contact to.

_buildSqlCallback()

_buildSqlCallback(string  $n, integer  $i, \ActiveRecord  $o) : string

helper function to build SQL with sql parts.

Parameters

string $n

The SQL part will be build.

integer $i

The index of $n in $sqls array.

\ActiveRecord $o

The refrence to $this

Returns

string