PHP 클래스 SQL_Model, atk4

상속: extends Model, implements Serializable
파일 보기 프로젝트 열기: atk4/atk4 1 사용 예제들

공개 프로퍼티들

프로퍼티 타입 설명
$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.

보호된 프로퍼티들

프로퍼티 타입 설명
$_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.

공개 메소드들

메소드 설명
__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 ( )

보호된 메소드들

메소드 설명
_load ( $id, $ignore_missing = false ) Internal loading funciton. Do not use. OK to override.

비공개 메소드들

메소드 설명
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.

메소드 상세

__clone() 공개 메소드

Clone DSQL
public __clone ( )

__construct() 공개 메소드

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

_dsql() 공개 메소드

This will initialize $dsql property if it does not exist yet.
public _dsql ( ) : DB_dsql
리턴 DB_dsql

_load() 보호된 메소드

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

_preexec() 공개 메소드

public _preexec ( )

_ref() 공개 메소드

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

_refBind() 공개 메소드

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

addCache() 공개 메소드

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

addCondition() 공개 메소드

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() 공개 메소드

See Field_Expression class.
public addExpression ( string $name, mixed $expression = null ) : DB_dsql
$name string
$expression mixed
리턴 DB_dsql

addField() 공개 메소드

Adds field to model
public addField ( string $name, string $actual_field = null ) : Field
$name string
$actual_field string
리턴 Field

containsMany() 공개 메소드

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

containsOne() 공개 메소드

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

count() 공개 메소드

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

current() 공개 메소드

public current ( )

debug() 공개 메소드

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

delete() 공개 메소드

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

deleteAll() 공개 메소드

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

dsql() 공개 메소드

Use for statements you are going to execute manually.
public dsql ( ) : DB_dsql
리턴 DB_dsql

each() 공개 메소드

public each ( $callable )

exception() 공개 메소드

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

fieldQuery() 공개 메소드

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

get() 공개 메소드

public get ( $name = null )

getActualFields() 공개 메소드

public getActualFields ( $group = UNDEFINED )

getBy() 공개 메소드

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

getField() 공개 메소드

public getField ( $f )

getRef() 공개 메소드

public getRef ( $name, $load = null )

getRows() 공개 메소드

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

hasField() 공개 메소드

public hasField ( $name )

hasMany() 공개 메소드

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

hasOne() 공개 메소드

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

init() 공개 메소드

public init ( )

initQuery() 공개 메소드

Initializes base query for this model.
public initQuery ( )

isDirty() 공개 메소드

public isDirty ( $name )

isInstanceLoaded() 공개 메소드

public isInstanceLoaded ( )

join() 공개 메소드

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 key ( )

leftJoin() 공개 메소드

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() 공개 메소드

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

loadAny() 공개 메소드

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

loadBy() 공개 메소드

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

loadData() 공개 메소드

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

loadRandom() 공개 메소드

public loadRandom ( )

newField() 공개 메소드

public newField ( $name )

next() 공개 메소드

public next ( )

offsetExists() 공개 메소드

public offsetExists ( $name )

offsetGet() 공개 메소드

public offsetGet ( $name )

offsetSet() 공개 메소드

public offsetSet ( $name, $val )

offsetUnset() 공개 메소드

public offsetUnset ( $name )

ref() 공개 메소드

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

refSQL() 공개 메소드

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

reset() 공개 메소드

public reset ( )

rewind() 공개 메소드

{{{ Iterator support
public rewind ( )

save() 공개 메소드

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

saveAndUnload() 공개 메소드

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

saveAs() 공개 메소드

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

selectQuery() 공개 메소드

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

serialize() 공개 메소드

public serialize ( )

set() 공개 메소드

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

setActualFields() 공개 메소드

public setActualFields ( array $fields )
$fields array

setDirty() 공개 메소드

public setDirty ( $name )

setLimit() 공개 메소드

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

setMasterField() 공개 메소드

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

setOrder() 공개 메소드

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

setSource() 공개 메소드

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

sum() 공개 메소드

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

titleQuery() 공개 메소드

Returns query which selects title field
public titleQuery ( )

tryDelete() 공개 메소드

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

tryLoad() 공개 메소드

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

tryLoadAny() 공개 메소드

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

tryLoadBy() 공개 메소드

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() 공개 메소드

Loads random entry into model
public tryLoadRandom ( )

unload() 공개 메소드

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

unserialize() 공개 메소드

public unserialize ( $data )

update() 공개 메소드

사용 중단: 4.3.1 Use set() then save().
public update ( $data = [] )

valid() 공개 메소드

public valid ( )

프로퍼티 상세

$_iterating 보호되어 있는 프로퍼티

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

$db 공개적으로 프로퍼티

Set to use different database connection
public DB $db
리턴 DB

$debug 공개적으로 프로퍼티

Call $model->debug(true|false) to turn on|off debug mode
public bool $debug
리턴 boolean

$dsql 보호되어 있는 프로퍼티

For low level use only. Use $this->dsql() when in doubt.
protected DB_dsql $dsql
리턴 DB_dsql

$entity_code 공개적으로 프로퍼티

사용 중단: 4.3.0 Use $table instead
public $entity_code

$fast 공개적으로 프로퍼티

Set this to true to speed up model, but sacrifice some of the consistency
public bool $fast
리턴 boolean

$field_class 공개적으로 프로퍼티

Default Field class name
public string $field_class
리턴 string

$relations 공개적으로 프로퍼티

Joins
public array $relations
리턴 array

$table_alias 공개적으로 프로퍼티

This will help to keep SQL syntax shorter, but will not impact functionality.
public string $table_alias
리턴 string