PHP Class Contao\Model

The class allows you to find and automatically join database records and to convert the result into objects. It also supports creating new objects and persisting them in the database. Usage: Write $user = new UserModel(); $user->name = 'Leo Feyer'; $user->city = 'Wuppertal'; $user->save(); Read $user = UserModel::findByCity('Wuppertal'); while ($user->next()) { echo $user->name; }
Inheritance: extends System
Mostrar archivo Open project: contao/core-bundle Class Usage Examples

Protected Properties

Property Type Description
$arrClassNames array Class name cache
$arrData array Data
$arrModified array Modified keys
$arrRelated array Related
$arrRelations array Relations
$blnPreventSaving boolean Prevent saving
$strPk string Primary key
$strTable string Table name

Public Methods

Method Description
__callStatic ( string $name, array $args ) : static | Collection | null Magic method to map Model::findByName() to Model::findBy('name')
__clone ( ) Unset the primary key when cloning an object
__construct ( Result $objResult = null ) Load the relations and optionally process a result set
__get ( string $strKey ) : mixed | null Return an object property
__isset ( string $strKey ) : boolean Check whether a property is set
__set ( string $strKey, mixed $varValue ) Set an object property
attach ( ) Attach the model to the registry
cloneOriginal ( ) : static Clone a model with its original values
countAll ( ) : integer Return the total number of rows
countBy ( mixed $strColumn = null, mixed $varValue = null, array $arrOptions = [] ) : integer Return the number of records matching certain criteria
current ( ) : static Return the object instance
delete ( ) : integer Delete the current record and return the number of affected rows
detach ( boolean $blnKeepClone = true ) Detach the model from the registry
findAll ( array $arrOptions = [] ) : Collection | null Find all records
findBy ( mixed $strColumn, mixed $varValue, array $arrOptions = [] ) : static | Collection | null Find records by various criteria
findByIdOrAlias ( mixed $varId, array $arrOptions = [] ) : static Find a single record by its ID or alias
findByPk ( mixed $varValue, array $arrOptions = [] ) : static Find a single record by its primary key
findMultipleByIds ( array $arrIds, array $arrOptions = [] ) : Collection | null Find multiple records by their IDs
findOneBy ( mixed $strColumn, mixed $varValue, array $arrOptions = [] ) : static Find a single record by various criteria
getClassFromTable ( string $strTable ) : string Compile a Model class name from a table name (e.g. tl_form_field becomes FormFieldModel)
getPk ( ) : string Return the name of the primary key
getRelated ( string $strKey, array $arrOptions = [] ) : static | Collection | null Lazy load related records
getTable ( ) : string Return the name of the related table
getUniqueFields ( ) : array Return an array of unique field/column names (without the PK)
isModified ( ) : boolean Return true if the model has been modified
markModified ( string $strKey ) Mark a field as modified
mergeRow ( array $arrData ) : static Set the current record from an array preserving modified but unsaved fields
onRegister ( Registry $registry ) Called when the model is attached to the model registry
onUnregister ( Registry $registry ) Called when the model is detached from the model registry
originalRow ( ) : array Return the original values as associative array
preventSaving ( boolean $blnKeepClone = true ) Prevent saving the model
refresh ( ) Reload the data from the database discarding all modifications
row ( ) : array Return the current record as associative array
save ( ) : static Save the current record
setRow ( array $arrData ) : static Set the current record from an array

Protected Methods

Method Description
buildCountQuery ( array $arrOptions ) : string Build a query based on the given options to count the number of records
buildFindQuery ( array $arrOptions ) : string Build a query based on the given options
createCollection ( array $arrModels, string $strTable ) : Collection Create a Model\Collection object
createCollectionFromDbResult ( Result $objResult, string $strTable ) : Collection Create a new collection from a database result
createModelFromDbResult ( Result $objResult ) : static Create a model from a database result
find ( array $arrOptions ) : static | Collection | null Find records and return the model or model collection
postFind ( Result $objResult ) : Result Modify the database result before the model is created
postSave ( integer $intType ) Modify the current row after it has been stored in the database
preFind ( Statement $objStatement ) : Statement Modify the database statement before it is executed
preSave ( array $arrSet ) : array Modify the current row before it is stored in the database

Method Details

__callStatic() public static method

Magic method to map Model::findByName() to Model::findBy('name')
public static __callStatic ( string $name, array $args ) : static | Collection | null
$name string The method name
$args array The passed arguments
return static | Contao\Model\Collection | null A model or model collection

__clone() public method

Unset the primary key when cloning an object
public __clone ( )

__construct() public method

Load the relations and optionally process a result set
public __construct ( Result $objResult = null )
$objResult Contao\Database\Result An optional database result

__get() public method

Return an object property
public __get ( string $strKey ) : mixed | null
$strKey string The property key
return mixed | null The property value or null

__isset() public method

Check whether a property is set
public __isset ( string $strKey ) : boolean
$strKey string The property key
return boolean True if the property is set

__set() public method

Set an object property
public __set ( string $strKey, mixed $varValue )
$strKey string The property name
$varValue mixed The property value

attach() public method

Attach the model to the registry
public attach ( )

buildCountQuery() protected static method

Build a query based on the given options to count the number of records
protected static buildCountQuery ( array $arrOptions ) : string
$arrOptions array The options array
return string The query string

buildFindQuery() protected static method

Build a query based on the given options
protected static buildFindQuery ( array $arrOptions ) : string
$arrOptions array The options array
return string The query string

cloneOriginal() public method

Clone a model with its original values
public cloneOriginal ( ) : static
return static The model

countAll() public static method

Return the total number of rows
public static countAll ( ) : integer
return integer The total number of rows

countBy() public static method

Return the number of records matching certain criteria
public static countBy ( mixed $strColumn = null, mixed $varValue = null, array $arrOptions = [] ) : integer
$strColumn mixed An optional property name
$varValue mixed An optional property value
$arrOptions array An optional options array
return integer The number of matching rows

createCollection() protected static method

Create a Model\Collection object
protected static createCollection ( array $arrModels, string $strTable ) : Collection
$arrModels array An array of models
$strTable string The table name
return Contao\Model\Collection The Model\Collection object

createCollectionFromDbResult() protected static method

Create a new collection from a database result
protected static createCollectionFromDbResult ( Result $objResult, string $strTable ) : Collection
$objResult Contao\Database\Result The database result object
$strTable string The table name
return Contao\Model\Collection The model collection

createModelFromDbResult() protected static method

Create a model from a database result
protected static createModelFromDbResult ( Result $objResult ) : static
$objResult Contao\Database\Result The database result object
return static The model

current() public method

Return the object instance
public current ( ) : static
return static The model object

delete() public method

Delete the current record and return the number of affected rows
public delete ( ) : integer
return integer The number of affected rows

detach() public method

Detach the model from the registry
public detach ( boolean $blnKeepClone = true )
$blnKeepClone boolean Keeps a clone of the model in the registry

find() protected static method

Supported options: * column: the field name * value: the field value * limit: the maximum number of rows * offset: the number of rows to skip * order: the sorting order * eager: load all related records eagerly
protected static find ( array $arrOptions ) : static | Collection | null
$arrOptions array The options array
return static | Contao\Model\Collection | null A model, model collection or null if the result is empty

findAll() public static method

Find all records
public static findAll ( array $arrOptions = [] ) : Collection | null
$arrOptions array An optional options array
return Contao\Model\Collection | null The model collection or null if the result is empty

findBy() public static method

Find records by various criteria
public static findBy ( mixed $strColumn, mixed $varValue, array $arrOptions = [] ) : static | Collection | null
$strColumn mixed The property name
$varValue mixed The property value
$arrOptions array An optional options array
return static | Contao\Model\Collection | null A model, model collection or null if the result is empty

findByIdOrAlias() public static method

Find a single record by its ID or alias
public static findByIdOrAlias ( mixed $varId, array $arrOptions = [] ) : static
$varId mixed The ID or alias
$arrOptions array An optional options array
return static The model or null if the result is empty

findByPk() public static method

Find a single record by its primary key
public static findByPk ( mixed $varValue, array $arrOptions = [] ) : static
$varValue mixed The property value
$arrOptions array An optional options array
return static The model or null if the result is empty

findMultipleByIds() public static method

Find multiple records by their IDs
public static findMultipleByIds ( array $arrIds, array $arrOptions = [] ) : Collection | null
$arrIds array An array of IDs
$arrOptions array An optional options array
return Contao\Model\Collection | null The model collection or null if there are no records

findOneBy() public static method

Find a single record by various criteria
public static findOneBy ( mixed $strColumn, mixed $varValue, array $arrOptions = [] ) : static
$strColumn mixed The property name
$varValue mixed The property value
$arrOptions array An optional options array
return static The model or null if the result is empty

getClassFromTable() public static method

Compile a Model class name from a table name (e.g. tl_form_field becomes FormFieldModel)
public static getClassFromTable ( string $strTable ) : string
$strTable string The table name
return string The model class name

getPk() public static method

Return the name of the primary key
public static getPk ( ) : string
return string The primary key

getRelated() public method

Lazy load related records
public getRelated ( string $strKey, array $arrOptions = [] ) : static | Collection | null
$strKey string The property name
$arrOptions array An optional options array
return static | Contao\Model\Collection | null The model or a model collection if there are multiple rows

getTable() public static method

Return the name of the related table
public static getTable ( ) : string
return string The table name

getUniqueFields() public static method

Return an array of unique field/column names (without the PK)
public static getUniqueFields ( ) : array
return array

isModified() public method

Return true if the model has been modified
public isModified ( ) : boolean
return boolean True if the model has been modified

markModified() public method

Mark a field as modified
public markModified ( string $strKey )
$strKey string The field key

mergeRow() public method

Set the current record from an array preserving modified but unsaved fields
public mergeRow ( array $arrData ) : static
$arrData array The data record
return static The model object

onRegister() public method

Called when the model is attached to the model registry
public onRegister ( Registry $registry )
$registry Contao\Model\Registry The model registry

onUnregister() public method

Called when the model is detached from the model registry
public onUnregister ( Registry $registry )
$registry Contao\Model\Registry The model registry

originalRow() public method

Return the original values as associative array
public originalRow ( ) : array
return array The original data

postFind() protected static method

Modify the database result before the model is created
protected static postFind ( Result $objResult ) : Result
$objResult Contao\Database\Result The database result object
return Contao\Database\Result The database result object

postSave() protected method

Modify the current row after it has been stored in the database
protected postSave ( integer $intType )
$intType integer The query type (Model::INSERT or Model::UPDATE)

preFind() protected static method

Modify the database statement before it is executed
protected static preFind ( Statement $objStatement ) : Statement
$objStatement Contao\Database\Statement The database statement object
return Contao\Database\Statement The database statement object

preSave() protected method

Modify the current row before it is stored in the database
protected preSave ( array $arrSet ) : array
$arrSet array The data array
return array The modified data array

preventSaving() public method

Prevent saving the model
public preventSaving ( boolean $blnKeepClone = true )
$blnKeepClone boolean Keeps a clone of the model in the registry

refresh() public method

Reload the data from the database discarding all modifications
public refresh ( )

row() public method

Return the current record as associative array
public row ( ) : array
return array The data record

save() public method

Save the current record
public save ( ) : static
return static The model object

setRow() public method

Set the current record from an array
public setRow ( array $arrData ) : static
$arrData array The data record
return static The model object

Property Details

$arrClassNames protected_oe static_oe property

Class name cache
protected static array $arrClassNames
return array

$arrData protected_oe property

Data
protected array $arrData
return array

$arrModified protected_oe property

Modified keys
protected array $arrModified
return array

$arrRelated protected_oe property

Related
protected array $arrRelated
return array

$arrRelations protected_oe property

Relations
protected array $arrRelations
return array

$blnPreventSaving protected_oe property

Prevent saving
protected bool $blnPreventSaving
return boolean

$strPk protected_oe static_oe property

Primary key
protected static string $strPk
return string

$strTable protected_oe static_oe property

Table name
protected static string $strTable
return string