PHP Class Gdn_Model, vanilla

Inheritance: extends Gdn_Pluggable
Datei anzeigen Open project: vanilla/vanilla Class Usage Examples

Public Properties

Property Type Description
$Data An object representation of the current working dataset.
$Database Database object.
$DateInserted The name of the field that stores the insert date for a record. This field will be automatically filled by the model if it exists.
$DateUpdated The name of the field that stores the update date for a record. This field will be automatically filled by the model if it exists.
$InsertUserID The name of the field that stores the id of the user that inserted it. This field will be automatically filled by the model if it exists and
$Name The name of the table that this model is intended to represent. The default value assigned to $this->Name will be the name that the model was instantiated with (defined in $this->__construct()).
$PrimaryKey name of the primary key field of this model. The default is 'id'. If $this->DefineSchema() is called, this value will be automatically changed to any primary key discovered when examining the table schema.
$SQL Contains the sql driver for the object.
$Schema An object that is used to store and examine database schema information related to this model. This object is defined and populated with $this->DefineSchema().
$UpdateUserID The name of the field that stores the id of the user that updated it. This field will be automatically filled by the model if it exists and @@Session::UserID is a valid integer.
$Validation An object that is used to manage and execute data integrity rules on this object. By default, this object only enforces maxlength, data types, and required fields (defined when $this->DefineSchema() is called).

Protected Properties

Property Type Description
$filterFields The fields that should be filtered out via {@link Gdn_Model::filterForm()}.

Public Methods

Method Description
__construct ( string $Name = '' ) Class constructor. Defines the related database table name.
addFilterField ( string | array $field ) : Gdn_Model Add one or more filter field names to the list of fields that will be removed during save.
defineSchema ( ) : Gdn_Schema Connects to the database and defines the schema associated with $this->Name.
delete ( array | integer $where = [], array | true $options = [] ) : Gdn_Dataset Delete records from a table.
deleteID ( mixed $id, array $options = [] ) : boolean Delete a record by primary key.
filterForm ( array $data ) : array Filter out any potentially insecure fields before they go to the database.
filterSchema ( array $Data ) : array Returns an array with only those keys that are actually in the schema.
get ( string $OrderFields = '', string $OrderDirection = 'asc', integer | boolean $Limit = false, $PageNumber = false ) : Gdn_Dataset
getCount ( array $Wheres = '' ) : Gdn_Dataset Returns a count of the # of records in the table.
getFilterFields ( ) : array Get all of the field names that will be filtered out during save.
getID ( mixed $ID, string $DatasetType = false, array $Options = [] ) : array | object Get the data from the model based on its primary key.
getRecordAttribute ( array &$Record, string $Attribute, mixed $Default = null ) : mixed Get something from $Record['Attributes'] by dot-formatted key.
getWhere ( array | boolean $Where = false, string $OrderFields = '', string $OrderDirection = 'asc', integer | false $Limit = false, integer | false $Offset = false ) : Gdn_DataSet Get a dataset for the model with a where filter.
insert ( array $Fields ) : boolean
options ( string | array $Key, mixed $Value = null ) : mixed Gets/sets an option on the object.
removeFilterField ( string | array $field ) : Gdn_Model Remove one or more fields from the filter field array.
save ( array $FormPostValues, array $Settings = false ) : unknown Takes a set of form data ($Form->_PostValues), validates them, and inserts or updates them to the datatabase.
saveToSerializedColumn ( string $Column, integer $RowID, string $Name, string $Value = '' ) : boolean | Gdn_DataSet | object | string
serializeRow ( array &$Row ) Serialize Attributes and Data columns in a row.
setField ( integer $RowID, array | string $Property, atom $Value = false ) Update a row in the database.
setFilterFields ( array $fields ) : Gdn_Model Set the array of filter field names.
setProperty ( integer $RowID, string $Property, boolean $ForceValue = false ) : boolean | string
setRecordAttribute ( array &$Record, string $Attribute, mixed $Value ) : mixed Set something on $Record['Attributes'] by dot-formatted key.
update ( array $Fields, array $Where = false, array $Limit = false ) : Gdn_Dataset
validate ( array $FormPostValues, boolean $Insert = false ) : boolean
validationResults ( ) : array Returns the $this->Validation->ValidationResults() array.

Protected Methods

Method Description
_beforeGet ( ) A overridable function called before the various get queries.
addInsertFields ( array &$Fields ) Adds $this->InsertUserID and $this->DateInserted fields to an associative array of fieldname/values if those fields exist on the table being inserted.
addUpdateFields ( array &$Fields ) Adds $this->UpdateUserID and $this->DateUpdated fields to an associative array of fieldname/values if those fields exist on the table being updated.
coerceData ( array $row, boolean $strip = true ) : array Coerce the data in a given row to conform to the data types in this model's schema.
collapseAttributes ( array $Data, string $Name = 'Attributes' ) : array Take all of the values that aren't in the schema and put them into the attributes column.
expandAttributes ( array $Row, string $Name = 'Attributes' ) : array Expand all of the values in the attributes column so they become part of the row.

Method Details

__construct() public method

Class constructor. Defines the related database table name.
public __construct ( string $Name = '' )
$Name string An optional parameter that allows you to explicitly define the name of the table that this model represents. You can also explicitly set this value with $this->Name.

_beforeGet() protected method

A overridable function called before the various get queries.
protected _beforeGet ( )

addFilterField() public method

Add one or more filter field names to the list of fields that will be removed during save.
public addFilterField ( string | array $field ) : Gdn_Model
$field string | array Either a field name or an array of field names to filter.
return Gdn_Model Returns $this for chaining.

addInsertFields() protected method

Adds $this->InsertUserID and $this->DateInserted fields to an associative array of fieldname/values if those fields exist on the table being inserted.
protected addInsertFields ( array &$Fields )
$Fields array The array of fields to add the values to.

addUpdateFields() protected method

Adds $this->UpdateUserID and $this->DateUpdated fields to an associative array of fieldname/values if those fields exist on the table being updated.
protected addUpdateFields ( array &$Fields )
$Fields array The array of fields to add the values to.

coerceData() protected method

This method doesn't do full data cleansing yet because that is too much of a change at this point. For this reason this method has been kept protected. The main purpose here is to clean the data enough to work better with MySQL's strict mode.
protected coerceData ( array $row, boolean $strip = true ) : array
$row array The row of data to coerce.
$strip boolean Whether or not to strip missing columns.
return array Returns a new data row with cleansed data.

collapseAttributes() protected method

Take all of the values that aren't in the schema and put them into the attributes column.
protected collapseAttributes ( array $Data, string $Name = 'Attributes' ) : array
$Data array
$Name string
return array

defineSchema() public method

Also instantiates and automatically defines $this->Validation.
public defineSchema ( ) : Gdn_Schema
return Gdn_Schema Returns the schema for this model.

delete() public method

Delete records from a table.
public delete ( array | integer $where = [], array | true $options = [] ) : Gdn_Dataset
$where array | integer The where clause to delete or an integer value.
$options array | true An array of options to control the delete. - limit: A limit to the number of records to delete. - reset: Deprecated. Whether or not to reset this SQL statement after the delete. Defaults to false.
return Gdn_Dataset Returns the result of the delete.

deleteID() public method

Delete a record by primary key.
public deleteID ( mixed $id, array $options = [] ) : boolean
$id mixed The primary key value of the record to delete.
$options array An array of options to affect the delete behaviour. Reserved for future use.
return boolean Returns **true** if the delete was successful or **false** otherwise.

expandAttributes() protected method

Expand all of the values in the attributes column so they become part of the row.
Since: 2.2
protected expandAttributes ( array $Row, string $Name = 'Attributes' ) : array
$Row array
$Name string
return array

filterForm() public method

Filter out any potentially insecure fields before they go to the database.
public filterForm ( array $data ) : array
$data array The array of data to filter.
return array Returns a copy of {@link $data} with fields removed.

filterSchema() public method

Returns an array with only those keys that are actually in the schema.
public filterSchema ( array $Data ) : array
$Data array An array of key/value pairs.
return array The filtered array.

get() public method

public get ( string $OrderFields = '', string $OrderDirection = 'asc', integer | boolean $Limit = false, $PageNumber = false ) : Gdn_Dataset
$OrderFields string
$OrderDirection string
$Limit integer | boolean
return Gdn_Dataset

getCount() public method

Returns a count of the # of records in the table.
public getCount ( array $Wheres = '' ) : Gdn_Dataset
$Wheres array
return Gdn_Dataset

getFilterFields() public method

Get all of the field names that will be filtered out during save.
public getFilterFields ( ) : array
return array Returns an array of field names.

getID() public method

Get the data from the model based on its primary key.
Since: 2.3 Added the $Options parameter.
public getID ( mixed $ID, string $DatasetType = false, array $Options = [] ) : array | object
$ID mixed The value of the primary key in the database.
$DatasetType string The format of the result dataset.
$Options array options to pass to the database.
return array | object

getRecordAttribute() public static method

Pass record byref.
public static getRecordAttribute ( array &$Record, string $Attribute, mixed $Default = null ) : mixed
$Record array
$Attribute string
$Default mixed Optional.
return mixed

getWhere() public method

Get a dataset for the model with a where filter.
public getWhere ( array | boolean $Where = false, string $OrderFields = '', string $OrderDirection = 'asc', integer | false $Limit = false, integer | false $Offset = false ) : Gdn_DataSet
$Where array | boolean A filter suitable for passing to Gdn_SQLDriver::Where().
$OrderFields string A comma delimited string to order the data.
$OrderDirection string One of **asc** or **desc**.
$Limit integer | false The database limit.
$Offset integer | false The database offset.
return Gdn_DataSet

insert() public method

public insert ( array $Fields ) : boolean
$Fields array
return boolean

options() public method

Gets/sets an option on the object.
Since: 2.3
public options ( string | array $Key, mixed $Value = null ) : mixed
$Key string | array The key of the option.
$Value mixed The value of the option or not specified just to get the current value.
return mixed The value of the option or $this if $Value is specified.

removeFilterField() public method

Remove one or more fields from the filter field array.
public removeFilterField ( string | array $field ) : Gdn_Model
$field string | array One or more field names to remove.
return Gdn_Model Returns $this for chaining.

save() public method

Takes a set of form data ($Form->_PostValues), validates them, and inserts or updates them to the datatabase.
public save ( array $FormPostValues, array $Settings = false ) : unknown
$FormPostValues array An associative array of $Field => $Value pairs that represent data posted from the form in the $_POST or $_GET collection.
$Settings array If a custom model needs special settings in order to perform a save, they would be passed in using this variable as an associative array.
return unknown

saveToSerializedColumn() public method

public saveToSerializedColumn ( string $Column, integer $RowID, string $Name, string $Value = '' ) : boolean | Gdn_DataSet | object | string
$Column string
$RowID integer
$Name string
$Value string
return boolean | Gdn_DataSet | object | string

serializeRow() public static method

Serialize Attributes and Data columns in a row.
Since: 2.1
public static serializeRow ( array &$Row )
$Row array

setField() public method

Update a row in the database.
Since: 2.1
public setField ( integer $RowID, array | string $Property, atom $Value = false )
$RowID integer
$Property array | string
$Value atom

setFilterFields() public method

Set the array of filter field names.
public setFilterFields ( array $fields ) : Gdn_Model
$fields array An array of field names.
return Gdn_Model Returns $this for chaining.

setProperty() public method

public setProperty ( integer $RowID, string $Property, boolean $ForceValue = false ) : boolean | string
$RowID integer
$Property string
$ForceValue boolean
return boolean | string

setRecordAttribute() public static method

Pass record byref.
public static setRecordAttribute ( array &$Record, string $Attribute, mixed $Value ) : mixed
$Record array
$Attribute string
$Value mixed
return mixed

update() public method

public update ( array $Fields, array $Where = false, array $Limit = false ) : Gdn_Dataset
$Fields array
$Where array
$Limit array
return Gdn_Dataset

validate() public method

public validate ( array $FormPostValues, boolean $Insert = false ) : boolean
$FormPostValues array
$Insert boolean
return boolean

validationResults() public method

Returns the $this->Validation->ValidationResults() array.
public validationResults ( ) : array
return array

Property Details

$Data public_oe property

An object representation of the current working dataset.
public $Data

$Database public_oe property

Database object.
public $Database

$DateInserted public_oe property

The name of the field that stores the insert date for a record. This field will be automatically filled by the model if it exists.
public $DateInserted

$DateUpdated public_oe property

The name of the field that stores the update date for a record. This field will be automatically filled by the model if it exists.
public $DateUpdated

$InsertUserID public_oe property

The name of the field that stores the id of the user that inserted it. This field will be automatically filled by the model if it exists and
public $InsertUserID

$Name public_oe property

The name of the table that this model is intended to represent. The default value assigned to $this->Name will be the name that the model was instantiated with (defined in $this->__construct()).
public $Name

$PrimaryKey public_oe property

name of the primary key field of this model. The default is 'id'. If $this->DefineSchema() is called, this value will be automatically changed to any primary key discovered when examining the table schema.
public $PrimaryKey

$SQL public_oe property

Contains the sql driver for the object.
public $SQL

$Schema public_oe property

An object that is used to store and examine database schema information related to this model. This object is defined and populated with $this->DefineSchema().
public $Schema

$UpdateUserID public_oe property

The name of the field that stores the id of the user that updated it. This field will be automatically filled by the model if it exists and @@Session::UserID is a valid integer.
public $UpdateUserID

$Validation public_oe property

An object that is used to manage and execute data integrity rules on this object. By default, this object only enforces maxlength, data types, and required fields (defined when $this->DefineSchema() is called).
public $Validation

$filterFields protected_oe property

The fields that should be filtered out via {@link Gdn_Model::filterForm()}.
protected $filterFields