PHP Class SQL_Model, atk4

Inheritance: extends Model, implements Serializable
Afficher le fichier Open project: atk4/atk4 Class Usage Examples

Méthodes publiques

Свойство Type Description
$db DB Set to use different database connection
$debug boolean Call $model->debug(true|false) to turn on|off debug mode
$entity_code
$fast boolean Set this to true to speed up model, but sacrifice some of the consistency
$field_class string Default Field class name
$relations array Joins
$table_alias string This will help to keep SQL syntax shorter, but will not impact functionality.

Protected Properties

Свойство Type Description
$_iterating boolean | DB_dsql False: finished iterating. True, reset not yet fetched. Object=DSQL
$dsql DB_dsql For low level use only. Use $this->dsql() when in doubt.

Méthodes publiques

Méthode Description
__clone ( ) Clone DSQL
__construct ( $options = [] ) Initialization of ID field, which must always be defined
_dsql ( ) : DB_dsql Use this instead of accessing dsql directly.
_preexec ( )
_ref ( $ref, $class, $field, $val )
_refBind ( $field_in, $expression, $field_out = null ) Strange method. Uses undefined $field variable, undefined refBind() method etc.
addCache ( $controller, $table = null, $priority = 5 )
addCondition ( mixed $field, mixed $cond = UNDEFINED, mixed $value = UNDEFINED, DB_dsql $dsql = null ) Adds "WHERE" condition / conditions in underlying DSQL.
addExpression ( string $name, mixed $expression = null ) : DB_dsql Adds and returns SQL-calculated expression as a read-only field.
addField ( string $name, string $actual_field = null ) : Field Adds field to model
containsMany ( $field, $model ) Defines multiple contained models for field
containsOne ( $field, $model ) Defines contained model for field
count ( string $alias = null ) : DB_dsql Returns dynamic query selecting number of entries in the database.
current ( )
debug ( boolean $enabled = true ) Turns debugging mode on|off for this model. All database operations will be outputed.
delete ( $id = null ) Deletes record matching the ID
deleteAll ( ) Deletes all records matching this model. Use with caution.
dsql ( ) : DB_dsql Produces a clone of Dynamic SQL object configured with table, conditions and joins of this model.
each ( $callable )
exception ( ) exception() will automatically add information about current model and will allow to turn on "debug" mode
fieldQuery ( $field ) Return query for a specific field. All other fields are ommitted.
get ( $name = null )
getActualFields ( $group = UNDEFINED )
getBy ( $field, $cond = UNDEFINED, $value = UNDEFINED ) Loads data record and return array of that data. Will not affect currently loaded record.
getField ( $f )
getRef ( $name, $load = null )
getRows ( $fields = null ) Loads all matching data into array of hashes
hasField ( $name )
hasMany ( $model, $their_field = null, $our_field = null, $as_field = null ) Defines many to one association
hasOne ( $model, $our_field = null, $display_field = null, $as_field = null ) Defines one to many association
init ( )
initQuery ( ) Initializes base query for this model.
isDirty ( $name )
isInstanceLoaded ( )
join ( $foreign_table, $master_field = null, $join_kind = null, $_foreign_alias = null, $relation = null ) Constructs model from multiple tables.
key ( )
leftJoin ( $foreign_table, $master_field = null, $join_kind = null, $_foreign_alias = null, $relation = null ) Creates weak join between tables.
load ( $id ) Loads record specified by ID.
loadAny ( ) Loads the first matching record from the model
loadBy ( $field, $cond = UNDEFINED, $value = UNDEFINED ) Similar to loadAny() but will apply condition before loading.
loadData ( $id = null )
loadRandom ( )
newField ( $name )
next ( )
offsetExists ( $name )
offsetGet ( $name )
offsetSet ( $name, $val )
offsetUnset ( $name )
ref ( $name, $load = null ) Traverses references. Use field name for hasOne() relations. Use model name for hasMany()
refSQL ( $name, $load = null ) Returns Model with SQL join usable for subqueries.
reset ( )
rewind ( ) {{{ Iterator support
save ( ) Save model into database and load it back.
saveAndUnload ( ) Save model into database and don't try to load it back
saveAs ( $model ) Save model into database and try to load it back as a new model of specified class.
selectQuery ( array $fields = null ) : DB_dsql Completes initialization of dsql() by adding fields and expressions.
serialize ( )
set ( $name, $value = UNDEFINED ) Override all methods to keep back-compatible
setActualFields ( array $fields )
setDirty ( $name )
setLimit ( $count, $offset = null ) Sets limit on query
setMasterField ( $field, $value )
setOrder ( $field, $desc = null ) Sets an order on the field. Field must be properly defined
setSource ( $controller, $table = null, $id = null )
sum ( string | array | Field $field ) : DB_dsql Returns dynamic query selecting sum of particular field or fields.
titleQuery ( ) Returns query which selects title field
tryDelete ( $id = null ) Tries to delete record, but does nothing if not found
tryLoad ( $id ) Try to load a record by specified ID. Will not raise exception if record is not found
tryLoadAny ( ) Try to load a matching record for the model. Will not raise exception if no records are found
tryLoadBy ( $field, $cond = UNDEFINED, $value = UNDEFINED ) Attempt to load using a specified condition, but will not fail if such record is not found
tryLoadRandom ( ) Loads random entry into model
unload ( ) forget currently loaded record and it's ID. Will not affect database
unserialize ( $data )
update ( $data = [] )
valid ( )

Méthodes protégées

Méthode Description
_load ( $id, $ignore_missing = false ) Internal loading funciton. Do not use. OK to override.

Private Methods

Méthode Description
insert ( ) Internal function which performs insert of data. Use save() instead. OK to override.
modify ( ) Internal function which performs modification of existing data. Use save() instead. OK to override.

Method Details

__clone() public méthode

Clone DSQL
public __clone ( )

__construct() public méthode

Initialization of ID field, which must always be defined
public __construct ( $options = [] )

_dsql() public méthode

This will initialize $dsql property if it does not exist yet.
public _dsql ( ) : DB_dsql
Résultat DB_dsql

_load() protected méthode

Internal loading funciton. Do not use. OK to override.
protected _load ( $id, $ignore_missing = false )

_preexec() public méthode

public _preexec ( )

_ref() public méthode

public _ref ( $ref, $class, $field, $val )

_refBind() public méthode

https://github.com/atk4/atk4/issues/711
public _refBind ( $field_in, $expression, $field_out = null )

addCache() public méthode

public addCache ( $controller, $table = null, $priority = 5 )

addCondition() public méthode

It tries to be smart about where and how the field is defined. $field can be passed as: - string (field name in this model) - Field object - DSQL expression - array (see note below) $cond can be passed as: - string ('=', '>', '<=', etc.) - value can be passed here, then it's used as $value with condition '=' $value can be passed as: - string, integer, boolean or any other simple data type - Field object - DSQL expreession NOTE: $field can be passed as array of conditions. Then all conditions will be joined with OR using DSQLs orExpr method. For example, $model->addCondition(array( array('profit', '=', null), array('profit', '<', 1000), )); will generate "WHERE profit is null OR profit < 1000" EXAMPLES: you can pass [dsql, dsql, dsql ...] and this will be treated as (dsql OR dsql OR dsql) ... you can pass [[field,cond,value], [field,cond,value], ...] and this will be treated as (field=value OR field=value OR ...) BTW, you can mix these too :) [[field,cond,value], dsql, [field,cond,value], ...] will become (field=value OR dsql OR field=value) Value also can be DSQL expression, so following will work nicely: [dsql,'>',dsql] will become (dsql > dsql) [dsql, dsql] will become (dsql = dsql) [field, cond, dsql] will become (field = dsql)
public addCondition ( mixed $field, mixed $cond = UNDEFINED, mixed $value = UNDEFINED, DB_dsql $dsql = null )
$field mixed Field for comparing or array of conditions
$cond mixed Condition
$value mixed Value for comparing
$dsql DB_dsql DSQL object to which conditions will be added

addExpression() public méthode

See Field_Expression class.
public addExpression ( string $name, mixed $expression = null ) : DB_dsql
$name string
$expression mixed
Résultat DB_dsql

addField() public méthode

Adds field to model
public addField ( string $name, string $actual_field = null ) : Field
$name string
$actual_field string
Résultat Field

containsMany() public méthode

Defines multiple contained models for field
public containsMany ( $field, $model )

containsOne() public méthode

Defines contained model for field
public containsOne ( $field, $model )

count() public méthode

Returns dynamic query selecting number of entries in the database.
public count ( string $alias = null ) : DB_dsql
$alias string Optional alias of count expression
Résultat DB_dsql

current() public méthode

public current ( )

debug() public méthode

Turns debugging mode on|off for this model. All database operations will be outputed.
public debug ( boolean $enabled = true )
$enabled boolean

delete() public méthode

Deletes record matching the ID
public delete ( $id = null )

deleteAll() public méthode

Deletes all records matching this model. Use with caution.
public deleteAll ( )

dsql() public méthode

Use for statements you are going to execute manually.
public dsql ( ) : DB_dsql
Résultat DB_dsql

each() public méthode

public each ( $callable )

exception() public méthode

exception() will automatically add information about current model and will allow to turn on "debug" mode
public exception ( )

fieldQuery() public méthode

Return query for a specific field. All other fields are ommitted.
public fieldQuery ( $field )

get() public méthode

public get ( $name = null )

getActualFields() public méthode

public getActualFields ( $group = UNDEFINED )

getBy() public méthode

Loads data record and return array of that data. Will not affect currently loaded record.
public getBy ( $field, $cond = UNDEFINED, $value = UNDEFINED )

getField() public méthode

public getField ( $f )

getRef() public méthode

public getRef ( $name, $load = null )

getRows() public méthode

Loads all matching data into array of hashes
public getRows ( $fields = null )

hasField() public méthode

public hasField ( $name )

hasMany() public méthode

Defines many to one association
public hasMany ( $model, $their_field = null, $our_field = null, $as_field = null )

hasOne() public méthode

Defines one to many association
public hasOne ( $model, $our_field = null, $display_field = null, $as_field = null )

init() public méthode

public init ( )

initQuery() public méthode

Initializes base query for this model.
public initQuery ( )

isDirty() public méthode

public isDirty ( $name )

isInstanceLoaded() public méthode

public isInstanceLoaded ( )

join() public méthode

Queries will join tables, inserts, updates and deletes will be applied on both tables
public join ( $foreign_table, $master_field = null, $join_kind = null, $_foreign_alias = null, $relation = null )

key() public méthode

public key ( )

leftJoin() public méthode

The foreign table may be absent and will not be automatically deleted.
public leftJoin ( $foreign_table, $master_field = null, $join_kind = null, $_foreign_alias = null, $relation = null )

load() public méthode

Loads record specified by ID.
public load ( $id )

loadAny() public méthode

Loads the first matching record from the model
public loadAny ( )

loadBy() public méthode

Condition is temporary. Fails if record is not loaded.
public loadBy ( $field, $cond = UNDEFINED, $value = UNDEFINED )

loadData() public méthode

Deprecation: 4.3.3 Backward-compatible. Will attempt to load but will not fail
public loadData ( $id = null )

loadRandom() public méthode

public loadRandom ( )

newField() public méthode

public newField ( $name )

next() public méthode

public next ( )

offsetExists() public méthode

public offsetExists ( $name )

offsetGet() public méthode

public offsetGet ( $name )

offsetSet() public méthode

public offsetSet ( $name, $val )

offsetUnset() public méthode

public offsetUnset ( $name )

ref() public méthode

Traverses references. Use field name for hasOne() relations. Use model name for hasMany()
public ref ( $name, $load = null )

refSQL() public méthode

Returns Model with SQL join usable for subqueries.
public refSQL ( $name, $load = null )

reset() public méthode

public reset ( )

rewind() public méthode

{{{ Iterator support
public rewind ( )

save() public méthode

If for some reason it won't load, whole operation is undone.
public save ( )

saveAndUnload() public méthode

Save model into database and don't try to load it back
public saveAndUnload ( )

saveAs() public méthode

Instance of new class is returned.
public saveAs ( $model )

selectQuery() public méthode

Completes initialization of dsql() by adding fields and expressions.
public selectQuery ( array $fields = null ) : DB_dsql
$fields array
Résultat DB_dsql

serialize() public méthode

public serialize ( )

set() public méthode

Override all methods to keep back-compatible
public set ( $name, $value = UNDEFINED )

setActualFields() public méthode

public setActualFields ( array $fields )
$fields array

setDirty() public méthode

public setDirty ( $name )

setLimit() public méthode

Sets limit on query
public setLimit ( $count, $offset = null )

setMasterField() public méthode

Deprecation: use two-argument addCondition. Always keep $field equals to $value for queries and new data
public setMasterField ( $field, $value )

setOrder() public méthode

Sets an order on the field. Field must be properly defined
public setOrder ( $field, $desc = null )

setSource() public méthode

public setSource ( $controller, $table = null, $id = null )

sum() public méthode

Returns dynamic query selecting sum of particular field or fields.
public sum ( string | array | Field $field ) : DB_dsql
$field string | array | Field
Résultat DB_dsql

titleQuery() public méthode

Returns query which selects title field
public titleQuery ( )

tryDelete() public méthode

Tries to delete record, but does nothing if not found
public tryDelete ( $id = null )

tryLoad() public méthode

Try to load a record by specified ID. Will not raise exception if record is not found
public tryLoad ( $id )

tryLoadAny() public méthode

Try to load a matching record for the model. Will not raise exception if no records are found
public tryLoadAny ( )

tryLoadBy() public méthode

Attempt to load using a specified condition, but will not fail if such record is not found
public tryLoadBy ( $field, $cond = UNDEFINED, $value = UNDEFINED )

tryLoadRandom() public méthode

Loads random entry into model
public tryLoadRandom ( )

unload() public méthode

forget currently loaded record and it's ID. Will not affect database
public unload ( )

unserialize() public méthode

public unserialize ( $data )

update() public méthode

Deprecation: 4.3.1 Use set() then save().
public update ( $data = [] )

valid() public méthode

public valid ( )

Property Details

$_iterating protected_oe property

False: finished iterating. True, reset not yet fetched. Object=DSQL
protected bool|DB_dsql $_iterating
Résultat boolean | DB_dsql

$db public_oe property

Set to use different database connection
public DB $db
Résultat DB

$debug public_oe property

Call $model->debug(true|false) to turn on|off debug mode
public bool $debug
Résultat boolean

$dsql protected_oe property

For low level use only. Use $this->dsql() when in doubt.
protected DB_dsql $dsql
Résultat DB_dsql

$entity_code public_oe property

Deprecation: 4.3.0 Use $table instead
public $entity_code

$fast public_oe property

Set this to true to speed up model, but sacrifice some of the consistency
public bool $fast
Résultat boolean

$field_class public_oe property

Default Field class name
public string $field_class
Résultat string

$relations public_oe property

Joins
public array $relations
Résultat array

$table_alias public_oe property

This will help to keep SQL syntax shorter, but will not impact functionality.
public string $table_alias
Résultat string