PHP Class LazyRecord\BaseModel

Inheritance: implements Serializabl\Serializable, implements ArrayAcces\ArrayAccess, implements IteratorAggregat\IteratorAggregate, implements Countabl\Countable
Datei anzeigen Open project: corneltek/lazyrecord Class Usage Examples

Public Properties

Property Type Description
$_cacheInstance
$_currentUser Current user object
$alias
$autoReload Auto reload record after creating new record Turn off this if you want performance.
$currentUser Model-Scope current user object Book::$currentUser = new YourCurrentUser;
$dataLabelField
$dataValueField
$lastResult The last result object.
$mixin_classes Mixin classes are emtpy. (MixinDeclareSchema)
$selected
$usingDataSource static $schemaCache;
$yamlEncoding
$yamlExtension

Protected Properties

Property Type Description
$_cache
$_cachePrefix
$_data
$_foreignRecordCache
$_preparedCreateStms
$_preparedFindSql
$_preparedFindStm prepared statement for find by primary key method.
$_preparedFindStms
$_schema

Public Methods

Method Description
__call ( $m, $a ) To support static operation methods like ::create, ::update, we can not define methods with the same name, so that we dispatch these methods from the magic method __call.
__clone ( )
__construct ( mixed $args = null, array $options = [] ) The constructor.
__get ( string $key ) Get record data, relational records, schema object or connection object.
__isset ( string $name ) Do we have this column ?
__set ( string $name, mixed $value ) Set column value.
afterCreate ( array $args ) Trigger for after creating new record.
afterDelete ( $args )
afterUpdate ( $args )
asCollection ( ) : LazyRecord\BaseCollection Return the collection object of current model object.
asCreateAction ( array $args = [], array $options = [] )
asDeleteAction ( array $args = [], array $options = [] )
asUpdateAction ( array $args = [], array $options = [] )
beforeCreate ( array $args ) : array Trigger method for "before creating new record".
beforeDelete ( $args ) Trigger method for.
beforeUpdate ( $args )
clear ( ) Clear current data stash.
clearInternalCache ( )
columns ( ) Get the RuntimeColumn objects from RuntimeSchema object.
count ( ) Countable interface
create ( array $args, array $options = [] ) : Result Method for creating new record, which is called from static::create and $record->create.
createOrUpdate ( array $args, array $byKeys = null ) Create or update an record by checking the existence from the $byKeys array that you defined.
currentUserCan ( CurrentUserInterface $user, string $right, array $args = [] ) Provide a basic access controll for model.
dataKeyValue ( ) This is for select widget, returns data key from specific column.
dataLabel ( ) This is for select widget, returns label value from specific column.
dataValue ( ) Alias method of $this->dataValue().
dbPrepareAndExecute ( PDO $conn, $sql, array $args = [] ) : PDOStatement We should move this method into connection manager.
dbQuery ( $dsId, string $sql ) : PDOStatement get pdo connetion and make a query.
deflate ( ) deflate current record data, usually deflate data from database turns data into objects, int, string (type casting).
deflateData ( array &$args ) : array deflate data from database.
delete ( ) : Result Delete current record, the record should be loaded already.
display ( string $name ) Render readable column value.
fastCreate ( array $args ) The fast create method does not reload record from created the primary key.
fetchManyToManyRelationCollection ( $relationId )
fetchOneToManyRelationCollection ( $relationId )
find ( $pkId ) Find record.
findMixinMethodClass ( string $m ) : string Find methods in mixin schema classes, methods will be called statically.
flushCache ( ) flush internal cache, in php memory.
free ( )
fromArray ( array $array ) Create from array.
get ( $key ) Get inflate value.
getAlias ( )
getCacheInstance ( )
getCachePrefix ( )
getConnection ( string $dsId = 'default' ) : PDO get default connection object (PDO) from connection manager.
getCurrentUser ( )
getData ( ) : array get current record data stash.
getDataLabelField ( )
getDataValueField ( )
getDeclareSchema ( )
getInternalCache ( string $key ) : mixed get internal cache from php memory.
getIterator ( ) =====================================
getLastResult ( )
getQueryDriver ( string $dsId ) Get SQL Query Driver by data source id.
getReadConnection ( ) : PDO Get PDO connection for reading data.
getReadQueryDriver ( ) : SQLBuilder\QueryDriver Get SQL Query driver object for reading data.
getReadSourceId ( )
getRecordActionClass ( $type )
getRelationalRecords ( $key, $relation = null )
getSchema ( )
getSchemaProxyClass ( )
getSelected ( )
getStashedData ( )
getTable ( )
getValue ( string $name ) : mixed Get the raw value from record (without deflator).
getWriteConnection ( ) : PDO Get PDO connection for writing data.
getWriteQueryDriver ( ) Get SQL Query driver object for writing data.
getWriteSourceId ( )
hasInternalCache ( $key )
hasValue ( string $name ) : boolean Check if the value exist.
inflateColumnValue ( string $n ) : mixed Inflate column value.
invokeAllMixinMethods ( string $m, array $a ) Invoke method on all mixin classes statically. this method does not return anything.
invokeMixinClassMethod ( string $mixinClass, string $m, array $a ) : mixed Invoke single mixin class method statically,.
load ( $args, array $options = null )
loadFromCache ( $args, $ttl = 3600 )
loadOrCreate ( array $args, array $byKeys = null ) Create a record if the record does not exists Otherwise the record should be updated with the arguments.
loadQuery ( string $sql, array $args = [], string $dsId = null ) : Result Load record from an sql query.
lockRead ( $alias = null )
lockWrite ( $alias = null )
newAction ( string $type, array $args = [], $options = [] ) Create an action from existing record object.
offsetExists ( $name )
offsetGet ( $name )
offsetSet ( $name, $value )
offsetUnset ( $name )
rawCreate ( array $args ) Simply create record without validation and triggers.
rawUpdate ( array $args ) Simply update record without validation and triggers.
reload ( string $pkId = null ) Relaod record data by primary key, parameter is optional if you've already defined the primary key column in this model.
reportError ( string $message, array $extra = [] ) : LazyRecord\Result\OperationError Report error.
reportSuccess ( string $message, array $extra = [] ) : Result Report success.
save ( ) : Result Save current data (create or update) if primary key is defined, do update if primary key is not defined, do create.
select ( $sels )
serialize ( ) ===============================
set ( $name, $value )
setAlias ( $alias )
setCurrentUser ( CurrentUserInterface $user )
setData ( array $array ) Set raw data.
setInternalCache ( string $key, mixed $val ) : mixed set internal cache, in php memory.
setPreferredTable ( $tableName )
setStashedData ( array $array )
toArray ( array $fields = null ) : array return data stash array,.
toInflatedArray ( ) : array Deflate data and return.
toJson ( ) : string return json format data.
toXml ( ) : string Return xml format data.
toYaml ( ) : string Return YAML format data.
unlock ( )
unserialize ( $data )
unsetPrimaryKey ( )
update ( array $args, $options = [] ) : Result Update current record.
using ( string $dsId ) Use specific data source for data operations.

Protected Methods

Method Description
_validateColumn ( RuntimeColumn $column, $val, array $args ) Run validator to validate column.

Private Methods

Method Description
getCache ( $key )
setCache ( $key, $val, $ttl )

Method Details

__call() public method

__call method is slower than normal method, because there are one more method table to look up. you should call create method if you need a better performance.
public __call ( $m, $a )

__clone() public method

public __clone ( )

__construct() public method

This constructor simply does nothing if no argument is passed. If the first argument is an integer, the record object will try to load the record by primary key with the given integer. If the first argument is an array, the record object will try to look up the record by treating the array as conditions, just like where([ ... ]) To avoid record object load the data, you can specify ['load' => false] as the option
public __construct ( mixed $args = null, array $options = [] )
$args mixed arguments for finding
$options array constructor options

__get() public method

Get record data, relational records, schema object or connection object.
public __get ( string $key )
$key string

__isset() public method

Do we have this column ?
public __isset ( string $name )
$name string

__set() public method

Set column value.
public __set ( string $name, mixed $value )
$name string
$value mixed

_validateColumn() protected method

A validator could be: 1. a ValidationKit validator, 2. a closure 3. a function name The validation result must be returned as in following format: boolean (valid or invalid, true or false) array( boolean valid , string message ) ValidationKit\ValidationMessage object. This method returns (object) { valid: boolean valid or invalid field: string field name message: }
protected _validateColumn ( RuntimeColumn $column, $val, array $args )
$column LazyRecord\Schema\RuntimeColumn
$args array

afterCreate() public method

Trigger for after creating new record.
public afterCreate ( array $args )
$args array

afterDelete() public method

public afterDelete ( $args )

afterUpdate() public method

public afterUpdate ( $args )

asCollection() public method

Return the collection object of current model object.
public asCollection ( ) : LazyRecord\BaseCollection
return LazyRecord\BaseCollection

asCreateAction() public method

public asCreateAction ( array $args = [], array $options = [] )
$args array
$options array

asDeleteAction() public method

public asDeleteAction ( array $args = [], array $options = [] )
$args array
$options array

asUpdateAction() public method

public asUpdateAction ( array $args = [], array $options = [] )
$args array
$options array

beforeCreate() public method

By overriding this method, you can modify the arguments that is passed to the query builder. Remember to return the arguments back.
public beforeCreate ( array $args ) : array
$args array Arguments
return array $args Arguments

beforeDelete() public method

Trigger method for.
public beforeDelete ( $args )

beforeUpdate() public method

public beforeUpdate ( $args )

clear() public method

Clear current data stash.
public clear ( )

clearInternalCache() public method

public clearInternalCache ( )

columns() public method

Get the RuntimeColumn objects from RuntimeSchema object.
public columns ( )

count() public method

Countable interface
public count ( )

create() public method

1. create method calls beforeCreate to trigger events or filter arguments. 2. it runs filterArrayWithColumns method to filter arguments with column definitions. 3. use currentUserCan method to check permission. 4. get column definitions and run filters, default value builders, canonicalizer, type constraint checkers to build a new arguments. 5. use these new arguments to build a SQL query with SQLBuilder\QueryBuilder. 6. insert SQL into data source (write) 7. reutrn the operation result.
public create ( array $args, array $options = [] ) : Result
$args array data
$options array
return Result operation result (success or error)

createOrUpdate() public method

If the record exists, then the record should be updated. If the record does not exist, then the record should be created.
public createOrUpdate ( array $args, array $byKeys = null )
$args array
$byKeys array

currentUserCan() public method

Provide a basic access controll for model.
public currentUserCan ( CurrentUserInterface $user, string $right, array $args = [] )
$user CurrentUserInterface Current user object, but be sure to implement CurrentUserInterface
$right string Can be 'create', 'update', 'load', 'delete'
$args array Arguments for operations (update, create, delete.. etc)

dataKeyValue() public method

This is for select widget, returns data key from specific column.
public dataKeyValue ( )

dataLabel() public method

This is for select widget, returns label value from specific column.
public dataLabel ( )

dataValue() public method

Alias method of $this->dataValue().
public dataValue ( )

dbPrepareAndExecute() public method

We should move this method into connection manager.
public dbPrepareAndExecute ( PDO $conn, $sql, array $args = [] ) : PDOStatement
$conn PDO
$args array
return PDOStatement

dbQuery() public method

get pdo connetion and make a query.
public dbQuery ( $dsId, string $sql ) : PDOStatement
$sql string SQL statement
return PDOStatement pdo statement object. $stm = $this->dbQuery($sql); foreach( $stm as $row ) { $row['name']; }

deflate() public method

deflate current record data, usually deflate data from database turns data into objects, int, string (type casting).
public deflate ( )

deflateData() public method

for datetime object, deflate it into DateTime object. for integer object, deflate it into int type. for boolean object, deflate it into bool type.
public deflateData ( array &$args ) : array
$args array
return array current record data.

delete() public method

Delete current record, the record should be loaded already.
public delete ( ) : Result
return Result operation result (success or error)

display() public method

Render readable column value.
public display ( string $name )
$name string column name

fastCreate() public method

TODO: refactor create code to call fastCreate. TODO: provide rawCreate to create data without validation.
public fastCreate ( array $args )
$args array

fetchManyToManyRelationCollection() public method

public fetchManyToManyRelationCollection ( $relationId )

fetchOneToManyRelationCollection() public method

public fetchOneToManyRelationCollection ( $relationId )

find() public method

Find record.
public find ( $pkId )

findMixinMethodClass() public method

Find methods in mixin schema classes, methods will be called statically.
public findMixinMethodClass ( string $m ) : string
$m string method name
return string the mixin class name.

flushCache() public method

flush internal cache, in php memory.
public flushCache ( )

free() public method

public free ( )

fromArray() public static method

Create from array.
public static fromArray ( array $array )
$array array

get() public method

Get inflate value.
public get ( $key )

getAlias() public method

public getAlias ( )

getCacheInstance() public static method

public static getCacheInstance ( )

getCachePrefix() public method

public getCachePrefix ( )

getConnection() public method

get default connection object (PDO) from connection manager.
public getConnection ( string $dsId = 'default' ) : PDO
$dsId string data source id
return PDO

getCurrentUser() public method

public getCurrentUser ( )

getData() public method

DEPRECATED
public getData ( ) : array
return array record data stash

getDataLabelField() public method

public getDataLabelField ( )

getDataValueField() public method

public getDataValueField ( )

getDeclareSchema() public method

public getDeclareSchema ( )

getInternalCache() public method

get internal cache from php memory.
public getInternalCache ( string $key ) : mixed
$key string cache key
return mixed cached value

getIterator() public method

=====================================
public getIterator ( )

getLastResult() public method

public getLastResult ( )

getQueryDriver() public method

Get SQL Query Driver by data source id.
public getQueryDriver ( string $dsId )
$dsId string Data source id.

getReadConnection() public method

Get PDO connection for reading data.
public getReadConnection ( ) : PDO
return PDO

getReadQueryDriver() public method

Get SQL Query driver object for reading data.
public getReadQueryDriver ( ) : SQLBuilder\QueryDriver
return SQLBuilder\QueryDriver

getReadSourceId() public method

public getReadSourceId ( )

getRecordActionClass() public method

public getRecordActionClass ( $type )

getRelationalRecords() public method

public getRelationalRecords ( $key, $relation = null )

getSchema() public method

public getSchema ( )

getSchemaProxyClass() public method

public getSchemaProxyClass ( )

getSelected() public method

public getSelected ( )

getStashedData() public method

public getStashedData ( )

getTable() public method

public getTable ( )

getValue() public method

Get the raw value from record (without deflator).
public getValue ( string $name ) : mixed
$name string
return mixed

getWriteConnection() public method

Get PDO connection for writing data.
public getWriteConnection ( ) : PDO
return PDO

getWriteQueryDriver() public method

Get SQL Query driver object for writing data.
public getWriteQueryDriver ( )

getWriteSourceId() public method

public getWriteSourceId ( )

hasInternalCache() public method

public hasInternalCache ( $key )

hasValue() public method

Check if the value exist.
public hasValue ( string $name ) : boolean
$name string
return boolean

inflateColumnValue() public method

Inflate column value.
public inflateColumnValue ( string $n ) : mixed
$n string Column name
return mixed

invokeAllMixinMethods() public method

Invoke method on all mixin classes statically. this method does not return anything.
public invokeAllMixinMethods ( string $m, array $a )
$m string method name.
$a array method arguments.

invokeMixinClassMethod() public method

Invoke single mixin class method statically,.
public invokeMixinClassMethod ( string $mixinClass, string $m, array $a ) : mixed
$mixinClass string mixin class name.
$m string method name.
$a array
return mixed execution result

load() public method

public load ( $args, array $options = null )
$options array

loadFromCache() public method

public loadFromCache ( $args, $ttl = 3600 )

loadOrCreate() public method

Create a record if the record does not exists Otherwise the record should be updated with the arguments.
public loadOrCreate ( array $args, array $byKeys = null )
$args array
$byKeys array it's optional if you defined primary key

loadQuery() public method

Load record from an sql query.
public loadQuery ( string $sql, array $args = [], string $dsId = null ) : Result
$sql string sql statement
$args array
$dsId string data source id $result = $record->loadQuery( 'select * from ....', array( ... ) , 'master' );
return Result

lockRead() public method

public lockRead ( $alias = null )

lockWrite() public method

public lockWrite ( $alias = null )

newAction() public method

Create an action from existing record object.
public newAction ( string $type, array $args = [], $options = [] )
$type string 'create','update','delete'
$args array

offsetExists() public method

public offsetExists ( $name )

offsetGet() public method

public offsetGet ( $name )

offsetSet() public method

public offsetSet ( $name, $value )

offsetUnset() public method

public offsetUnset ( $name )

rawCreate() public method

Simply create record without validation and triggers.
public rawCreate ( array $args )
$args array

rawUpdate() public method

Simply update record without validation and triggers.
public rawUpdate ( array $args )
$args array

reload() public method

Relaod record data by primary key, parameter is optional if you've already defined the primary key column in this model.
public reload ( string $pkId = null )
$pkId string primary key name

reportError() public method

Report error.
public reportError ( string $message, array $extra = [] ) : LazyRecord\Result\OperationError
$message string Error message.
$extra array Extra data.
return LazyRecord\Result\OperationError

reportSuccess() public method

In this method, which pushs result object into ->results array. you can use flushResult() method to clean up these result objects.
public reportSuccess ( string $message, array $extra = [] ) : Result
$message string Success message.
$extra array Extra data.
return Result

save() public method

Save current data (create or update) if primary key is defined, do update if primary key is not defined, do create.
public save ( ) : Result
return Result operation result (success or error)

select() public method

public select ( $sels )

serialize() public method

===============================
public serialize ( )

set() public method

public set ( $name, $value )

setAlias() public method

public setAlias ( $alias )

setCurrentUser() public method

public setCurrentUser ( CurrentUserInterface $user )
$user CurrentUserInterface

setData() public method

DEPRECATED
public setData ( array $array )
$array array

setInternalCache() public method

set internal cache, in php memory.
public setInternalCache ( string $key, mixed $val ) : mixed
$key string cache key
$val mixed cache value
return mixed cached value

setPreferredTable() public method

public setPreferredTable ( $tableName )

setStashedData() public method

public setStashedData ( array $array )
$array array

toArray() public method

return data stash array,.
public toArray ( array $fields = null ) : array
$fields array
return array

toInflatedArray() public method

Deflate data and return.
public toInflatedArray ( ) : array
return array

toJson() public method

return json format data.
public toJson ( ) : string
return string JSON string

toXml() public method

Return xml format data.
public toXml ( ) : string
return string XML string

toYaml() public method

Return YAML format data.
public toYaml ( ) : string
return string YAML string

unlock() public method

public unlock ( )

unserialize() public method

public unserialize ( $data )

unsetPrimaryKey() public method

public unsetPrimaryKey ( )

update() public method

Update current record.
public update ( array $args, $options = [] ) : Result
$args array
return Result operation result (success or error)

using() public method

Use specific data source for data operations.
public using ( string $dsId )
$dsId string data source id.

Property Details

$_cache protected_oe property

protected $_cache

$_cacheInstance public_oe static_oe property

public static $_cacheInstance

$_cachePrefix protected_oe property

protected $_cachePrefix

$_currentUser public_oe property

Current user object
public $_currentUser

$_data protected_oe property

protected $_data

$_foreignRecordCache protected_oe property

protected $_foreignRecordCache

$_preparedCreateStms protected_oe property

protected $_preparedCreateStms

$_preparedFindSql protected_oe property

protected $_preparedFindSql

$_preparedFindStm protected_oe property

prepared statement for find by primary key method.
protected $_preparedFindStm

$_preparedFindStms protected_oe property

protected $_preparedFindStms

$_schema protected_oe property

protected $_schema

$alias public_oe property

public $alias

$autoReload public_oe property

Auto reload record after creating new record Turn off this if you want performance.
public $autoReload

$currentUser public_oe static_oe property

Model-Scope current user object Book::$currentUser = new YourCurrentUser;
public static $currentUser

$dataLabelField public_oe property

public $dataLabelField

$dataValueField public_oe property

public $dataValueField

$lastResult public_oe property

The last result object.
public $lastResult

$mixin_classes public_oe static_oe property

Mixin classes are emtpy. (MixinDeclareSchema)
public static $mixin_classes

$selected public_oe property

public $selected

$usingDataSource public_oe property

static $schemaCache;
public $usingDataSource

$yamlEncoding public_oe static_oe property

public static $yamlEncoding

$yamlExtension public_oe static_oe property

public static $yamlExtension