idx
int64
0
60.3k
question
stringlengths
92
4.62k
target
stringlengths
7
635
40,700
public function getUrlTrackGoal ( $ idGoal , $ revenue = 0.0 ) { $ url = $ this -> getRequest ( $ this -> idSite ) ; $ url .= '&idgoal=' . $ idGoal ; if ( ! empty ( $ revenue ) ) { $ revenue = $ this -> forceDotAsSeparatorForDecimalPoint ( $ revenue ) ; $ url .= '&revenue=' . $ revenue ; } return $ url ; }
Builds URL to track a goal with idGoal and revenue .
40,701
public function getUrlTrackAction ( $ actionUrl , $ actionType ) { $ url = $ this -> getRequest ( $ this -> idSite ) ; $ url .= '&' . $ actionType . '=' . urlencode ( $ actionUrl ) ; return $ url ; }
Builds URL to track a new action .
40,702
public function setUserId ( $ userId ) { if ( $ userId === false ) { $ this -> setNewVisitorId ( ) ; return $ this ; } if ( $ userId === '' ) { throw new Exception ( "User ID cannot be empty." ) ; } $ this -> userId = $ userId ; return $ this ; }
Force the action to be recorded for a specific User . The User ID is a string representing a given user in your system .
40,703
protected function loadVisitorIdCookie ( ) { $ idCookie = $ this -> getCookieMatchingName ( 'id' ) ; if ( $ idCookie === false ) { return false ; } $ parts = explode ( '.' , $ idCookie ) ; if ( strlen ( $ parts [ 0 ] ) != self :: LENGTH_VISITOR_ID ) { return false ; } $ this -> cookieVisitorId = $ parts [ 0 ] ; $ this ...
Loads values from the VisitorId Cookie
40,704
public function setLocalTime ( $ time ) { list ( $ hour , $ minute , $ second ) = explode ( ':' , $ time ) ; $ this -> localHour = ( int ) $ hour ; $ this -> localMinute = ( int ) $ minute ; $ this -> localSecond = ( int ) $ second ; return $ this ; }
Sets local visitor time
40,705
public function setResolution ( $ width , $ height ) { $ this -> width = $ width ; $ this -> height = $ height ; return $ this ; }
Sets user resolution width and height .
40,706
public function setPlugins ( $ flash = false , $ java = false , $ director = false , $ quickTime = false , $ realPlayer = false , $ pdf = false , $ windowsMedia = false , $ gears = false , $ silverlight = false ) { $ this -> plugins = '&fla=' . ( int ) $ flash . '&java=' . ( int ) $ java . '&dir=' . ( int ) $ director ...
Sets visitor browser supported plugins
40,707
public function setRequestTimeout ( $ timeout ) { if ( ! is_int ( $ timeout ) || $ timeout < 0 ) { throw new Exception ( "Invalid value supplied for request timeout: $timeout" ) ; } $ this -> requestTimeout = $ timeout ; return $ this ; }
Sets the maximum number of seconds that the tracker will spend waiting for a response from Piwik .
40,708
protected function getBaseUrl ( ) { if ( empty ( self :: $ URL ) ) { throw new Exception ( 'You must first set the Piwik Tracker URL by calling PiwikTracker::$URL = \'http://your-website.org/piwik/\';' ) ; } if ( strpos ( self :: $ URL , '/piwik.php' ) === false && strpos ( self :: $ URL , '/proxy-piwik...
Returns the base URL for the piwik server .
40,709
protected function setCookie ( $ cookieName , $ cookieValue , $ cookieTTL ) { $ cookieExpire = $ this -> currentTs + $ cookieTTL ; if ( ! headers_sent ( ) ) { setcookie ( $ this -> getCookieName ( $ cookieName ) , $ cookieValue , $ cookieExpire , $ this -> configCookiePath , $ this -> configCookieDomain ) ; } return $ ...
Sets a first party cookie to the client to improve dual JS - PHP tracking .
40,710
public function setOutgoingTrackerCookie ( $ name , $ value ) { if ( $ value === null ) { unset ( $ this -> outgoingTrackerCookies [ $ name ] ) ; } else { $ this -> outgoingTrackerCookies [ $ name ] = $ value ; } }
Sets a cookie to be sent to the tracking server .
40,711
public function getIncomingTrackerCookie ( $ name ) { if ( isset ( $ this -> incomingTrackerCookies [ $ name ] ) ) { return $ this -> incomingTrackerCookies [ $ name ] ; } return false ; }
Gets a cookie which was set by the tracking server .
40,712
protected function parseIncomingCookies ( $ headers ) { $ this -> incomingTrackerCookies = array ( ) ; if ( ! empty ( $ headers ) ) { $ headerName = 'set-cookie:' ; $ headerNameLength = strlen ( $ headerName ) ; foreach ( $ headers as $ header ) { if ( strpos ( strtolower ( $ header ) , $ headerName ) !== 0 ) { continu...
Reads incoming tracking server cookies .
40,713
public function hasPermissionTo ( $ permission ) : bool { if ( $ this -> hasPermissionFlags ( ) ) { return $ this -> hasPermissionThroughFlag ( ) ; } if ( $ this -> hasPermissionThroughRole ( $ permission ) ) { return true ; } if ( $ this -> hasPermission ( $ permission ) ) { return true ; } return false ; }
The mothergoose check . Runs through each scenario provided by Shinobi - checking for special flags role permissions and individual user permissions ; in that order .
40,714
protected function hasPermission ( $ permission ) : bool { return ( bool ) $ this -> permissions -> where ( 'slug' , $ permission -> slug ) -> count ( ) ; }
Checks if the user has the given permission assigned .
40,715
protected function registerGates ( ) { Gate :: before ( function ( $ user , $ permission ) { try { if ( method_exists ( $ user , 'hasPermissionTo' ) ) { $ permission = Shinobi :: permission ( ) -> where ( 'slug' , $ permission ) -> firstOrFail ( ) ; return $ user -> hasPermissionTo ( $ permission ) ? : null ; } } catch...
Register the permission gates .
40,716
protected function hasPermissionThroughRole ( $ permission ) : bool { foreach ( $ permission -> roles as $ role ) { if ( $ this -> roles -> contains ( $ role ) ) { return true ; } } return false ; }
Run through the roles assigned to the permission and checks if the user has any of them assigned .
40,717
public function hasRole ( $ role ) : bool { $ slug = str_slug ( $ role ) ; return ( bool ) $ this -> roles -> where ( 'slug' , $ slug ) -> count ( ) ; }
Checks if the model has the given role assigned .
40,718
public function assignRoles ( ... $ roles ) : self { $ roles = array_flatten ( $ roles ) ; $ roles = $ this -> getRoles ( $ roles ) ; if ( ! $ roles ) { return $ this ; } $ this -> roles ( ) -> syncWithoutDetaching ( $ roles ) ; return $ this ; }
Assign the specified roles to the model .
40,719
public function removeRoles ( ... $ roles ) : self { $ roles = array_flatten ( $ roles ) ; $ roles = $ this -> getRoles ( $ roles ) ; $ this -> roles ( ) -> detach ( $ roles ) ; return $ this ; }
Remove the specified roles from the model .
40,720
public function syncRoles ( ... $ roles ) : self { $ roles = array_flatten ( $ roles ) ; $ roles = $ this -> getRoles ( $ roles ) ; $ this -> roles ( ) -> sync ( $ roles ) ; return $ this ; }
Sync the specified roles to the model .
40,721
public function add ( $ object , $ defaults = [ ] ) { if ( ! is_array ( $ defaults ) ) { $ defaults = [ 'name' => $ defaults ] ; } $ defaults [ 'join' ] = $ this ; return $ this -> owner -> add ( $ object , $ defaults ) ; }
Adds any object to owner model .
40,722
public function join ( $ foreign_table , $ defaults = [ ] ) { if ( ! is_array ( $ defaults ) ) { $ defaults = [ 'master_field' => $ defaults ] ; } $ defaults [ 'join' ] = $ this ; return $ this -> owner -> join ( $ foreign_table , $ defaults ) ; }
Another join will be attached to a current join .
40,723
public function leftJoin ( $ foreign_table , $ defaults = [ ] ) { if ( ! is_array ( $ defaults ) ) { $ defaults = [ 'master_field' => $ defaults ] ; } $ defaults [ 'join' ] = $ this ; return $ this -> owner -> leftJoin ( $ foreign_table , $ defaults ) ; }
Another leftJoin will be attached to a current join .
40,724
public function hasOne ( $ link , $ defaults = [ ] ) { if ( ! is_array ( $ defaults ) ) { $ defaults = [ 'model' => $ defaults ? : 'Model_' . $ link ] ; } $ defaults [ 'join' ] = $ this ; return $ this -> owner -> hasOne ( $ link , $ defaults ) ; }
Creates reference based on a field from the join .
40,725
public function hasMany ( $ link , $ defaults = [ ] ) { if ( ! is_array ( $ defaults ) ) { $ defaults = [ 'model' => $ defaults ? : 'Model_' . $ link ] ; } $ defaults = array_merge ( [ 'our_field' => $ this -> id_field , 'their_field' => $ this -> owner -> table . '_' . $ this -> id_field , ] , $ defaults ) ; return $ ...
Creates reference based on the field from the join .
40,726
public function dsql ( ) { $ dsql = $ this -> owner -> persistence -> initQuery ( $ this -> owner ) ; $ dsql -> reset ( 'table' ) ; $ dsql -> table ( $ this -> foreign_table , $ this -> foreign_alias ) ; return $ dsql ; }
Returns DSQL query .
40,727
public function initSelectQuery ( $ model , $ query ) { if ( $ this -> on ) { $ query -> join ( $ this -> foreign_table . ' ' . $ this -> foreign_alias , $ this -> on instanceof \ atk4 \ dsql \ Expression ? $ this -> on : $ model -> expr ( $ this -> on ) , $ this -> kind ) ; return ; } $ query -> join ( $ this -> forei...
Before query is executed this method will be called .
40,728
protected function getOurValue ( ) { if ( $ this -> owner -> loaded ( ) ) { return $ this -> our_field ? $ this -> owner [ $ this -> our_field ] : $ this -> owner -> id ; } else { return $ this -> owner -> action ( 'field' , [ $ this -> our_field ? : ( $ this -> owner -> id_field ? : 'id' ) , ] ) ; } }
Returns our field value or id .
40,729
public function ref ( $ defaults = [ ] ) : Model { return $ this -> getModel ( $ defaults ) -> addCondition ( $ this -> their_field ? : ( $ this -> owner -> table . '_' . ( $ this -> owner -> id_field ? : 'id' ) ) , $ this -> getOurValue ( ) ) ; }
Returns referenced model with condition set .
40,730
public function init ( ) { parent :: init ( ) ; if ( ! $ this -> our_field ) { $ this -> our_field = $ this -> link ; } if ( ! $ this -> owner -> hasField ( $ this -> our_field ) ) { $ this -> owner -> addField ( $ this -> our_field , [ 'type' => $ this -> type , 'reference' => $ this , 'system' => $ this -> system , '...
Reference_One will also add a field corresponding to our_field unless it exists of course .
40,731
protected function referenceOurValue ( ) : Field { $ this -> owner -> persistence_data [ 'use_table_prefixes' ] = true ; return $ this -> owner -> getField ( $ this -> our_field ) ; }
Returns our field or id field .
40,732
public function ref ( $ defaults = [ ] ) : Model { $ m = $ this -> getModel ( $ defaults ) ; $ m -> addHook ( 'afterDelete' , function ( $ m ) { $ this -> owner [ $ this -> our_field ] = null ; } ) ; if ( $ this -> their_field ) { if ( $ this -> owner [ $ this -> our_field ] ) { $ m -> tryLoadBy ( $ this -> their_field...
If owner model is loaded then return referenced model with respective record loaded .
40,733
public function getModel ( $ defaults = [ ] ) : Model { if ( ! isset ( $ defaults [ 'table_alias' ] ) ) { if ( ! $ this -> table_alias ) { $ this -> table_alias = $ this -> link ; $ this -> table_alias = preg_replace ( '/_' . ( $ this -> owner -> id_field ? : 'id' ) . '/' , '' , $ this -> table_alias ) ; $ this -> tabl...
Returns destination model that is linked through this reference . Will apply necessary conditions .
40,734
protected function addToPersistence ( $ model , $ defaults = [ ] ) : Model { if ( ! $ model -> persistence && $ p = $ this -> getDefaultPersistence ( $ model ) ) { $ p -> add ( $ model , $ defaults ) ; } return $ model ; }
Adds model to persistence .
40,735
public function to ( Model $ destination ) { $ this -> destination = $ destination ; if ( ! $ this -> destination -> persistence ) { $ this -> source -> persistence -> add ( $ this -> destination ) ; } return $ this ; }
Set model in which to copy records into .
40,736
protected function extractKeys ( $ array ) : array { $ result = [ ] ; foreach ( $ array as $ key => $ val ) { if ( is_int ( $ key ) ) { $ result [ $ val ] = [ ] ; } else { $ result [ $ key ] = $ val ; } } return $ result ; }
Will extract non - numeric keys from the array .
40,737
public function copy ( ) { return $ this -> _copy ( $ this -> source , $ this -> destination , $ this -> references , $ this -> exclusions ) -> reload ( ) ; }
Copy records .
40,738
public function expr ( Model $ m , $ expr , $ args = [ ] ) { if ( ! is_string ( $ expr ) ) { return $ this -> connection -> expr ( $ expr , $ args ) ; } preg_replace_callback ( '/\[[a-z0-9_]*\]|{[a-z0-9_]*}/i' , function ( $ matches ) use ( & $ args , $ m ) { $ identifier = substr ( $ matches [ 0 ] , 1 , - 1 ) ; if ( $...
Creates new Expression object from expression string .
40,739
public function initField ( $ q , $ field ) { $ is_sql_field = ( $ field instanceof \ atk4 \ data \ Field_SQL ) || ( $ field instanceof \ atk4 \ data \ Field \ Boolean ) ; if ( $ is_sql_field && $ field -> useAlias ( ) ) { $ q -> field ( $ field , $ field -> short_name ) ; } elseif ( $ is_sql_field ) { $ q -> field ( $...
Adds Field in Query .
40,740
public function initQueryFields ( $ m , $ q , $ fields = null ) { if ( $ fields === false ) { return ; } if ( is_array ( $ fields ) ) { foreach ( $ fields as $ field ) { $ this -> initField ( $ q , $ m -> getField ( $ field ) ) ; } } elseif ( $ m -> only_fields ) { $ added_fields = [ ] ; foreach ( $ m -> only_fields as...
Adds model fields in Query .
40,741
public function tryLoad ( Model $ m , $ id ) { if ( ! $ m -> id_field ) { throw new Exception ( [ 'Unable to load field by "id" when Model->id_field is not defined.' , 'id' => $ id ] ) ; } $ load = $ m -> action ( 'select' ) ; $ load -> where ( $ m -> getField ( $ m -> id_field ) , $ id ) ; $ load -> limit ( 1 ) ; try ...
Tries to load data record but will not fail if record can t be loaded .
40,742
public function load ( Model $ m , $ id ) { $ data = $ this -> tryLoad ( $ m , $ id ) ; if ( ! $ data ) { throw new Exception ( [ 'Record was not found' , 'model' => $ m , 'id' => $ id , 'conditions' => $ m -> conditions , ] , 404 ) ; } return $ data ; }
Loads a record from model and returns a associative array .
40,743
public function tryLoadAny ( Model $ m ) { $ load = $ m -> action ( 'select' ) ; $ load -> limit ( 1 ) ; try { $ data = $ this -> typecastLoadRow ( $ m , $ load -> getRow ( ) ) ; } catch ( \ PDOException $ e ) { throw new Exception ( [ 'Unable to load due to query error' , 'query' => $ load -> getDebugQuery ( false ) ,...
Tries to load any one record .
40,744
public function loadAny ( Model $ m ) { $ data = $ this -> tryLoadAny ( $ m ) ; if ( ! $ data ) { throw new Exception ( [ 'No matching records were found' , 'model' => $ m , 'conditions' => $ m -> conditions , ] , 404 ) ; } return $ data ; }
Loads any one record .
40,745
public function insert ( Model $ m , $ data ) { $ insert = $ m -> action ( 'insert' ) ; if ( $ m -> id_field && array_key_exists ( $ m -> id_field , $ data ) && $ data [ $ m -> id_field ] === null ) { unset ( $ data [ $ m -> id_field ] ) ; } $ insert -> set ( $ this -> typecastSaveRow ( $ m , $ data ) ) ; $ st = null ;...
Inserts record in database and returns new record ID .
40,746
public function update ( Model $ m , $ id , $ data ) { if ( ! $ m -> id_field ) { throw new Exception ( [ 'id_field of a model is not set. Unable to update record.' ] ) ; } $ update = $ this -> initQuery ( $ m ) ; $ update -> mode ( 'update' ) ; $ data = $ this -> typecastSaveRow ( $ m , $ data ) ; $ update -> set ( $ ...
Updates record in database .
40,747
public function delete ( Model $ m , $ id ) { if ( ! $ m -> id_field ) { throw new Exception ( [ 'id_field of a model is not set. Unable to delete record.' ] ) ; } $ delete = $ this -> initQuery ( $ m ) ; $ delete -> mode ( 'delete' ) ; $ delete -> where ( $ m -> id_field , $ id ) ; $ m -> hook ( 'beforeDeleteQuery' , ...
Deletes record from database .
40,748
public function isEditable ( ) { return $ this -> read_only || $ this -> never_persist ? false : ( isset ( $ this -> ui [ 'editable' ] ) ? $ this -> ui [ 'editable' ] : ! $ this -> system ) ; }
Returns if field should be editable in UI .
40,749
public function getCaption ( ) { return $ this -> caption ? : ( isset ( $ this -> ui [ 'caption' ] ) ? $ this -> ui [ 'caption' ] : ucwords ( str_replace ( '_' , ' ' , $ this -> short_name ) ) ) ; }
Returns field caption for use in UI .
40,750
public function getDSQLExpression ( $ expression ) { if ( ! $ this -> owner -> persistence || ! $ this -> owner -> persistence instanceof Persistence \ SQL ) { throw new Exception ( [ 'Field must have SQL persistence if it is used as part of expression' , 'persistence' => $ this -> owner -> persistence ?? null , ] ) ; ...
When field is used as expression this method will be called . Universal way to convert ourselves to expression . Off - load implementation into persistence .
40,751
public function openFile ( $ mode = 'r' ) { if ( ! $ this -> handle ) { $ this -> handle = fopen ( $ this -> file , $ mode ) ; if ( $ this -> handle === false ) { throw new Exception ( [ 'Can not open CSV file.' , 'file' => $ this -> file , 'mode' => $ mode ] ) ; } } }
Open CSV file .
40,752
public function getLine ( ) { $ data = fgetcsv ( $ this -> handle , 0 , $ this -> delimiter , $ this -> enclosure , $ this -> escape_char ) ; if ( $ data ) { $ this -> line ++ ; } return $ data ; }
Returns one line of CSV file as array .
40,753
public function putLine ( $ data ) { $ ok = fputcsv ( $ this -> handle , $ data , $ this -> delimiter , $ this -> enclosure , $ this -> escape_char ) ; if ( $ ok === false ) { throw new Exception ( [ 'Can not write to CSV file.' ] ) ; } }
Writes array as one record to CSV file .
40,754
public function typecastLoadRow ( Model $ m , $ row ) { $ id = null ; if ( isset ( $ row [ $ m -> id_field ] ) ) { $ id = $ row [ $ m -> id_field ] ; unset ( $ row [ $ m -> id_field ] ) ; } else { $ id = null ; } $ row = array_combine ( $ this -> header , $ row ) ; if ( isset ( $ id ) ) { $ row [ $ m -> id_field ] = $ ...
Typecasting when load data row .
40,755
public function generateNewID ( $ m , $ table = null ) { if ( ! isset ( $ table ) ) { $ table = $ m -> table ; } $ ids = array_keys ( $ this -> data [ $ table ] ) ; $ type = $ m -> getField ( $ m -> id_field ) -> type ; switch ( $ type ) { case 'integer' : return count ( $ ids ) === 0 ? 1 : ( max ( $ ids ) + 1 ) ; case...
Generates new record ID .
40,756
public function typecastLoadRow ( Model $ m , $ row ) { if ( ! $ row ) { return $ row ; } $ result = [ ] ; foreach ( $ row as $ key => & $ value ) { $ f = $ m -> hasField ( $ key ) ; if ( ! $ f ) { $ result [ $ key ] = $ value ; continue ; } if ( $ value === null ) { $ result [ $ key ] = $ value ; continue ; } if ( $ f...
Will convert one row of data from Persistence - specific types to PHP native types .
40,757
public function typecastSaveField ( Field $ f , $ value ) { try { if ( is_array ( $ f -> typecast ) && isset ( $ f -> typecast [ 0 ] ) && is_callable ( $ t = $ f -> typecast [ 0 ] ) ) { return $ t ( $ value , $ f , $ this ) ; } if ( $ value === null ) { return ; } return $ this -> _typecastSaveField ( $ f , $ value ) ;...
Prepare value of a specific field by converting it to persistence - friendly format .
40,758
public function typecastLoadField ( Field $ f , $ value ) { try { if ( is_array ( $ f -> typecast ) && isset ( $ f -> typecast [ 1 ] ) && is_callable ( $ t = $ f -> typecast [ 1 ] ) ) { return $ t ( $ value , $ f , $ this ) ; } if ( $ f -> type && $ f -> type != 'string' && $ value === '' ) { return ; } if ( $ value ==...
Cast specific field value from the way how it s stored inside persistence to a PHP format .
40,759
public function serializeSaveField ( Field $ f , $ value ) { try { if ( is_array ( $ f -> serialize ) && isset ( $ f -> serialize [ 0 ] ) && is_callable ( $ t = $ f -> serialize [ 0 ] ) ) { return $ t ( $ f , $ value , $ this ) ; } return $ this -> _serializeSaveField ( $ f , $ value ) ; } catch ( \ Exception $ e ) { t...
Provided with a value will perform field serialization . Can be used for the purposes of encryption or storing unsupported formats .
40,760
public function serializeLoadField ( Field $ f , $ value ) { try { if ( is_array ( $ f -> serialize ) && isset ( $ f -> serialize [ 1 ] ) && is_callable ( $ t = $ f -> serialize [ 1 ] ) ) { return $ t ( $ f , $ value , $ this ) ; } return $ this -> _serializeLoadField ( $ f , $ value ) ; } catch ( \ Exception $ e ) { t...
Provided with a value will perform field un - serialization . Can be used for the purposes of encryption or storing unsupported formats .
40,761
public function _serializeSaveField ( Field $ f , $ value ) { switch ( $ f -> serialize === true ? 'serialize' : $ f -> serialize ) { case 'serialize' : return serialize ( $ value ) ; case 'json' : return $ this -> jsonEncode ( $ f , $ value ) ; case 'base64' : if ( ! is_string ( $ value ) ) { throw new Exception ( [ '...
Override this to fine - tune serialization for your persistence .
40,762
public function _serializeLoadField ( Field $ f , $ value ) { switch ( $ f -> serialize === true ? 'serialize' : $ f -> serialize ) { case 'serialize' : return unserialize ( $ value ) ; case 'json' : return $ this -> jsonDecode ( $ f , $ value , $ f -> type == 'array' ) ; case 'base64' : return base64_decode ( $ value ...
Override this to fine - tune un - serialization for your persistence .
40,763
protected function jsonDecode ( Field $ f , $ value , $ assoc = true ) { if ( ! defined ( 'JSON_THROW_ON_ERROR' ) ) { define ( 'JSON_THROW_ON_ERROR' , 0 ) ; } $ res = json_decode ( $ value , $ assoc , 512 , JSON_THROW_ON_ERROR ) ; if ( JSON_THROW_ON_ERROR == 0 && json_last_error ( ) !== JSON_ERROR_NONE ) { throw new Ex...
JSON decoding with proper error treatment .
40,764
protected function jsonEncode ( Field $ f , $ value ) { if ( ! defined ( 'JSON_THROW_ON_ERROR' ) ) { define ( 'JSON_THROW_ON_ERROR' , 0 ) ; } $ res = json_encode ( $ value , JSON_THROW_ON_ERROR , 512 ) ; if ( JSON_THROW_ON_ERROR == 0 && json_last_error ( ) !== JSON_ERROR_NONE ) { throw new Exception ( [ 'There was erro...
JSON encoding with proper error treatment .
40,765
public function order ( $ fields ) { $ data = $ this -> get ( ) ; $ args = [ ] ; foreach ( $ fields as list ( $ field , $ desc ) ) { $ args [ ] = array_column ( $ data , $ field ) ; $ args [ ] = $ desc ? SORT_DESC : SORT_ASC ; } $ args [ ] = & $ data ; call_user_func_array ( 'array_multisort' , $ args ) ; $ this -> gen...
Applies sorting on Iterator .
40,766
public function limit ( $ cnt , $ shift = 0 ) { $ data = array_slice ( $ this -> get ( ) , $ shift , $ cnt , true ) ; $ this -> generator = new \ ArrayIterator ( $ data ) ; return $ this ; }
Limit Iterator .
40,767
public function init ( ) { $ this -> _init ( ) ; if ( $ this -> id_field ) { $ this -> addField ( $ this -> id_field , [ 'system' => true , ] ) ; } }
Extend this method to define fields of your choice .
40,768
public function addField ( $ field , $ defaults = [ ] ) { $ class = $ this -> _default_seed_addField ; if ( is_array ( $ defaults ) && isset ( $ defaults [ 'type' ] ) ) { switch ( strtolower ( $ defaults [ 'type' ] ) ) { case 'boolean' : $ class = 'Boolean' ; break ; } } $ field_object = $ this -> factory ( $ this -> m...
Adds new field into model .
40,769
public function addFields ( $ fields = [ ] , $ defaults = [ ] ) { foreach ( $ fields as $ field ) { if ( is_string ( $ field ) ) { $ this -> addField ( $ field , $ defaults ) ; continue ; } if ( is_array ( $ field ) && isset ( $ field [ 0 ] ) ) { $ name = $ field [ 0 ] ; unset ( $ field [ 0 ] ) ; $ this -> addField ( $...
Adds multiple fields into model .
40,770
private function normalizeFieldName ( $ field ) { if ( is_object ( $ field ) && isset ( $ field -> _trackableTrait ) && $ field -> owner === $ this ) { $ field = $ field -> short_name ; } if ( ! is_string ( $ field ) || $ field === '' || is_numeric ( $ field [ 0 ] ) ) { throw new Exception ( [ 'Incorrect specification ...
Normalize field name .
40,771
public function isDirty ( $ fields = [ ] ) { if ( ! is_array ( $ fields ) ) { $ fields = [ $ fields ] ; } foreach ( $ fields as $ field ) { $ field = $ this -> normalizeFieldName ( $ field ) ; if ( array_key_exists ( $ field , $ this -> dirty ) ) { return true ; } } return false ; }
Will return true if any of the specified fields are dirty .
40,772
public function get ( $ field = null ) { if ( $ field === null ) { $ data = [ ] ; if ( $ this -> only_fields ) { foreach ( $ this -> only_fields as $ field ) { $ data [ $ field ] = $ this -> get ( $ field ) ; } } else { foreach ( $ this -> elements as $ field => $ f ) { if ( $ f instanceof Field ) { $ data [ $ field ] ...
Returns field value . If no field is passed then returns array of all field values .
40,773
public function _unset ( $ name ) { $ name = $ this -> normalizeFieldName ( $ name ) ; if ( array_key_exists ( $ name , $ this -> dirty ) ) { $ this -> data [ $ name ] = $ this -> dirty [ $ name ] ; unset ( $ this -> dirty [ $ name ] ) ; } return $ this ; }
Remove current field value and use default .
40,774
public function addCondition ( $ field , $ operator = null , $ value = null ) { if ( is_array ( $ field ) ) { $ this -> conditions [ ] = [ $ field ] ; return $ this ; } $ f = ( is_string ( $ field ) || $ field instanceof Field ) ? $ this -> getField ( $ field ) : false ; if ( $ f ) { if ( $ operator === '=' || func_num...
Narrow down data - set of the current model by applying additional condition . There is no way to remove condition once added so if you need - clone model .
40,775
public function setOrder ( $ field , $ desc = null ) { if ( is_string ( $ field ) && strpos ( $ field , ',' ) !== false ) { $ field = explode ( ',' , $ field ) ; } if ( is_array ( $ field ) ) { if ( null !== $ desc ) { throw new Exception ( [ 'If first argument is array, second argument must not be used' , 'arg1' => $ ...
Set order for model records . Multiple calls .
40,776
public function unload ( ) { $ this -> hook ( 'beforeUnload' ) ; $ this -> id = null ; $ this -> data = [ ] ; $ this -> dirty = [ ] ; $ this -> hook ( 'afterUnload' ) ; return $ this ; }
Unload model .
40,777
public function duplicate ( $ new_id = null ) { $ this -> id = null ; if ( $ this -> id_field ) { $ this [ $ this -> id_field ] = $ new_id ; } return $ this ; }
Keeps the model data but wipes out the ID so when you save it next time it ends up as a new record in the database .
40,778
public function asModel ( $ class , $ options = [ ] ) { $ m = $ this -> newInstance ( $ class , $ options ) ; foreach ( $ this -> data as $ field => $ value ) { if ( $ value !== null && $ value !== $ this -> getField ( $ field ) -> default ) { $ m [ $ field ] = $ value ; } } return $ m ; }
This will cast Model into another class without loosing state of your active record .
40,779
public function tryLoad ( $ id ) { if ( ! $ this -> persistence ) { throw new Exception ( [ 'Model is not associated with any database' ] ) ; } if ( ! $ this -> persistence -> hasMethod ( 'tryLoad' ) ) { throw new Exception ( 'Persistence does not support tryLoad()' ) ; } if ( $ this -> loaded ( ) ) { $ this -> unload ...
Try to load record . Will not throw exception if record doesn t exist .
40,780
public function loadBy ( $ field , $ value ) { $ field = $ this -> getField ( $ field ) ; $ system = $ field -> system ; $ default = $ field -> default ; $ this -> addCondition ( $ field , $ value ) ; try { $ this -> loadAny ( ) ; } catch ( \ Exception $ e ) { array_pop ( $ this -> conditions ) ; $ field -> system = $ ...
Load record by condition .
40,781
public function tryLoadBy ( $ field , $ value ) { $ field = $ this -> getField ( $ field ) ; $ system = $ field -> system ; $ default = $ field -> default ; $ this -> addCondition ( $ field , $ value ) ; try { $ this -> tryLoadAny ( ) ; } catch ( \ Exception $ e ) { array_pop ( $ this -> conditions ) ; $ field -> syste...
Try to load record by condition . Will not throw exception if record doesn t exist .
40,782
public function insert ( $ row ) { $ m = clone $ this ; $ this -> _rawInsert ( $ m , $ row ) ; return $ m -> id ; }
Faster method to add data that does not modify active record .
40,783
public function import ( $ rows ) { $ m = clone $ this ; foreach ( $ rows as $ row ) { $ this -> _rawInsert ( $ m , $ row ) ; } return $ this ; }
Even more faster method to add data does not modify your current record and will not return anything .
40,784
public function export ( $ fields = null , $ key_field = null ) { if ( ! $ this -> persistence -> hasMethod ( 'export' ) ) { throw new Exception ( 'Persistence does not support export()' ) ; } if ( $ key_field === null ) { return $ this -> persistence -> export ( $ this , $ fields ) ; } $ key_field_added = false ; if (...
Export DataSet as array of hashes .
40,785
public function each ( $ method ) { foreach ( $ this as $ rec ) { if ( is_string ( $ method ) ) { $ rec -> $ method ( ) ; } elseif ( is_callable ( $ method ) ) { call_user_func ( $ method , $ rec ) ; } } return $ this ; }
Executes specified method or callback for each record in DataSet .
40,786
public function delete ( $ id = null ) { if ( $ this -> read_only ) { throw new Exception ( [ 'Model is read-only and cannot be deleted' ] ) ; } if ( $ id == $ this -> id ) { $ id = null ; } return $ this -> atomic ( function ( ) use ( $ id ) { if ( $ id ) { $ c = clone $ this ; $ c -> load ( $ id ) -> delete ( ) ; ret...
Delete record with a specified id . If no ID is specified then current record is deleted .
40,787
public function action ( $ mode , $ args = [ ] ) { if ( ! $ this -> persistence ) { throw new Exception ( [ 'action() requires model to be associated with db' ] ) ; } if ( ! $ this -> persistence -> hasMethod ( 'action' ) ) { throw new Exception ( 'Persistence does not support action()' ) ; } return $ this -> persisten...
Execute action .
40,788
public function leftJoin ( $ foreign_table , $ defaults = [ ] ) { if ( ! is_array ( $ defaults ) ) { $ defaults = [ 'master_field' => $ defaults ] ; } $ defaults [ 'weak' ] = true ; return $ this -> join ( $ foreign_table , $ defaults ) ; }
Left Join support .
40,789
protected function _hasReference ( $ c , $ link , $ defaults = [ ] ) : Reference { if ( ! is_array ( $ defaults ) ) { $ defaults = [ 'model' => $ defaults ? : 'Model_' . $ link ] ; } elseif ( isset ( $ defaults [ 0 ] ) ) { $ defaults [ 'model' ] = $ defaults [ 0 ] ; unset ( $ defaults [ 0 ] ) ; } $ defaults [ 0 ] = $ l...
Private method .
40,790
public function addRef ( $ link , $ callback ) : Reference { return $ this -> _hasReference ( $ this -> _default_seed_addRef , $ link , $ callback ) ; }
Add generic relation . Provide your own call - back that will return the model .
40,791
public function hasOne ( $ link , $ defaults = [ ] ) : Reference { return $ this -> _hasReference ( $ this -> _default_seed_hasOne , $ link , $ defaults ) ; }
Add hasOne field .
40,792
public function hasMany ( $ link , $ defaults = [ ] ) : Reference { return $ this -> _hasReference ( $ this -> _default_seed_hasMany , $ link , $ defaults ) ; }
Add hasMany field .
40,793
public function getRefs ( ) : array { $ refs = [ ] ; foreach ( $ this -> elements as $ key => $ val ) { if ( substr ( $ key , 0 , 5 ) == '#ref_' ) { $ refs [ substr ( $ key , 5 ) ] = $ val ; } } return $ refs ; }
Returns all reference fields .
40,794
public function addExpression ( $ name , $ expression ) { if ( ! is_array ( $ expression ) ) { $ expression = [ 'expr' => $ expression ] ; } elseif ( isset ( $ expression [ 0 ] ) ) { $ expression [ 'expr' ] = $ expression [ 0 ] ; unset ( $ expression [ 0 ] ) ; } $ c = $ this -> _default_seed_addExpression ; return $ th...
Add expression field .
40,795
public function addCalculatedField ( $ name , $ expression ) { if ( ! is_array ( $ expression ) ) { $ expression = [ 'expr' => $ expression ] ; } elseif ( isset ( $ expression [ 0 ] ) ) { $ expression [ 'expr' ] = $ expression [ 0 ] ; unset ( $ expression [ 0 ] ) ; } return $ this -> addField ( $ name , new Field \ Cal...
Add expression field which will calculate its value by using callback .
40,796
public function lastInsertID ( ) { if ( isset ( $ this -> persistence ) && $ this -> persistence instanceof Persistence \ SQL ) { return $ this -> persistence -> connection -> lastInsertId ( $ this ) ; } throw new Exception ( 'Model is not associated with SQL-enabled persistence' ) ; }
Last ID inserted .
40,797
public function load ( Model $ m , $ id , $ table = null ) { if ( isset ( $ m -> table ) && ! isset ( $ this -> data [ $ m -> table ] ) ) { throw new Exception ( [ 'Table was not found in the array data source' , 'table' => $ m -> table , ] ) ; } if ( ! isset ( $ this -> data [ $ table ? : $ m -> table ] [ $ id ] ) ) {...
Loads model and returns data record .
40,798
public function tryLoadAny ( Model $ m , $ table = null ) { if ( ! isset ( $ table ) ) { $ table = $ m -> table ; } if ( ! $ this -> data [ $ table ] ) { return false ; } reset ( $ this -> data [ $ table ] ) ; $ key = key ( $ this -> data [ $ table ] ) ; $ row = $ this -> load ( $ m , $ key , $ table ) ; $ m -> id = $ ...
Tries to load first available record and return data record . Doesn t throw exception if model can t be loaded or there are no data records .
40,799
public function update ( Model $ m , $ id , $ data , $ table = null ) { if ( ! isset ( $ table ) ) { $ table = $ m -> table ; } $ data = $ this -> typecastSaveRow ( $ m , $ data ) ; $ this -> data [ $ table ] [ $ id ] = array_merge ( isset ( $ this -> data [ $ table ] [ $ id ] ) ? $ this -> data [ $ table ] [ $ id ] : ...
Updates record in data array and returns record ID .