PHP Class LazyRecord\BaseModel

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

Méthodes publiques

Свойство 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

Свойство Type Description
$_cache
$_cachePrefix
$_data
$_foreignRecordCache
$_preparedCreateStms
$_preparedFindSql
$_preparedFindStm prepared statement for find by primary key method.
$_preparedFindStms
$_schema

Méthodes publiques

Méthode 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.

Méthodes protégées

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

Private Methods

Méthode Description
getCache ( $key )
setCache ( $key, $val, $ttl )

Method Details

__call() public méthode

__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 méthode

public __clone ( )

__construct() public méthode

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 méthode

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

__isset() public méthode

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

__set() public méthode

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

_validateColumn() protected méthode

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 méthode

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

afterDelete() public méthode

public afterDelete ( $args )

afterUpdate() public méthode

public afterUpdate ( $args )

asCollection() public méthode

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

asCreateAction() public méthode

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

asDeleteAction() public méthode

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

asUpdateAction() public méthode

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

beforeCreate() public méthode

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
Résultat array $args Arguments

beforeDelete() public méthode

Trigger method for.
public beforeDelete ( $args )

beforeUpdate() public méthode

public beforeUpdate ( $args )

clear() public méthode

Clear current data stash.
public clear ( )

clearInternalCache() public méthode

public clearInternalCache ( )

columns() public méthode

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

count() public méthode

Countable interface
public count ( )

create() public méthode

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
Résultat Result operation result (success or error)

createOrUpdate() public méthode

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 méthode

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 méthode

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

dataLabel() public méthode

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

dataValue() public méthode

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

dbPrepareAndExecute() public méthode

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

dbQuery() public méthode

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

deflate() public méthode

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

deflateData() public méthode

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
Résultat array current record data.

delete() public méthode

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

display() public méthode

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

fastCreate() public méthode

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

fetchManyToManyRelationCollection() public méthode

public fetchManyToManyRelationCollection ( $relationId )

fetchOneToManyRelationCollection() public méthode

public fetchOneToManyRelationCollection ( $relationId )

find() public méthode

Find record.
public find ( $pkId )

findMixinMethodClass() public méthode

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

flushCache() public méthode

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

free() public méthode

public free ( )

fromArray() public static méthode

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

get() public méthode

Get inflate value.
public get ( $key )

getAlias() public méthode

public getAlias ( )

getCacheInstance() public static méthode

public static getCacheInstance ( )

getCachePrefix() public méthode

public getCachePrefix ( )

getConnection() public méthode

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

getCurrentUser() public méthode

public getCurrentUser ( )

getData() public méthode

DEPRECATED
public getData ( ) : array
Résultat array record data stash

getDataLabelField() public méthode

public getDataLabelField ( )

getDataValueField() public méthode

public getDataValueField ( )

getDeclareSchema() public méthode

public getDeclareSchema ( )

getInternalCache() public méthode

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

getIterator() public méthode

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

getLastResult() public méthode

public getLastResult ( )

getQueryDriver() public méthode

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

getReadConnection() public méthode

Get PDO connection for reading data.
public getReadConnection ( ) : PDO
Résultat PDO

getReadQueryDriver() public méthode

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

getReadSourceId() public méthode

public getReadSourceId ( )

getRecordActionClass() public méthode

public getRecordActionClass ( $type )

getRelationalRecords() public méthode

public getRelationalRecords ( $key, $relation = null )

getSchema() public méthode

public getSchema ( )

getSchemaProxyClass() public méthode

public getSchemaProxyClass ( )

getSelected() public méthode

public getSelected ( )

getStashedData() public méthode

public getStashedData ( )

getTable() public méthode

public getTable ( )

getValue() public méthode

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

getWriteConnection() public méthode

Get PDO connection for writing data.
public getWriteConnection ( ) : PDO
Résultat PDO

getWriteQueryDriver() public méthode

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

getWriteSourceId() public méthode

public getWriteSourceId ( )

hasInternalCache() public méthode

public hasInternalCache ( $key )

hasValue() public méthode

Check if the value exist.
public hasValue ( string $name ) : boolean
$name string
Résultat boolean

inflateColumnValue() public méthode

Inflate column value.
public inflateColumnValue ( string $n ) : mixed
$n string Column name
Résultat mixed

invokeAllMixinMethods() public méthode

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 méthode

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
Résultat mixed execution result

load() public méthode

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

loadFromCache() public méthode

public loadFromCache ( $args, $ttl = 3600 )

loadOrCreate() public méthode

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 méthode

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' );
Résultat Result

lockRead() public méthode

public lockRead ( $alias = null )

lockWrite() public méthode

public lockWrite ( $alias = null )

newAction() public méthode

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

offsetExists() public méthode

public offsetExists ( $name )

offsetGet() public méthode

public offsetGet ( $name )

offsetSet() public méthode

public offsetSet ( $name, $value )

offsetUnset() public méthode

public offsetUnset ( $name )

rawCreate() public méthode

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

rawUpdate() public méthode

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

reload() public méthode

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 méthode

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

reportSuccess() public méthode

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.
Résultat Result

save() public méthode

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

select() public méthode

public select ( $sels )

serialize() public méthode

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

set() public méthode

public set ( $name, $value )

setAlias() public méthode

public setAlias ( $alias )

setCurrentUser() public méthode

public setCurrentUser ( CurrentUserInterface $user )
$user CurrentUserInterface

setData() public méthode

DEPRECATED
public setData ( array $array )
$array array

setInternalCache() public méthode

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

setPreferredTable() public méthode

public setPreferredTable ( $tableName )

setStashedData() public méthode

public setStashedData ( array $array )
$array array

toArray() public méthode

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

toInflatedArray() public méthode

Deflate data and return.
public toInflatedArray ( ) : array
Résultat array

toJson() public méthode

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

toXml() public méthode

Return xml format data.
public toXml ( ) : string
Résultat string XML string

toYaml() public méthode

Return YAML format data.
public toYaml ( ) : string
Résultat string YAML string

unlock() public méthode

public unlock ( )

unserialize() public méthode

public unserialize ( $data )

unsetPrimaryKey() public méthode

public unsetPrimaryKey ( )

update() public méthode

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

using() public méthode

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