BaseModel

Type
Abstract Class
Namespace
Craft
Inherits
Craft\BaseModel » CModel (opens new window) » CComponent (opens new window)
Implements
ArrayAccess (opens new window), IteratorAggregate (opens new window)
Extended by
Craft\AccountSettingsModel, Craft\AppNewReleaseModel, Craft\AppUpdateModel, Craft\AssetFileModel, Craft\AssetFolderModel, Craft\AssetIndexDataModel, Craft\AssetOperationResponseModel, Craft\AssetSourceModel, Craft\AssetTransformIndexModel, Craft\AssetTransformModel, Craft\BaseComponentModel, Craft\BaseElementModel, Craft\BaseEntryRevisionModel, Craft\CategoryGroupLocaleModel, Craft\CategoryGroupModel, Craft\CategoryModel, Craft\ContentModel, Craft\DeprecationErrorModel, Craft\ElementCriteriaModel, Craft\EmailModel, Craft\EmailSettingsModel, Craft\EntryDraftModel, Craft\EntryModel, Craft\EntryTypeModel, Craft\EntryVersionModel, Craft\EtModel, Craft\FieldGroupModel, Craft\FieldLayoutFieldModel, Craft\FieldLayoutModel, Craft\FieldLayoutTabModel, Craft\FieldModel, Craft\FolderCriteriaModel, Craft\GetHelpModel, Craft\GlobalSetModel, Craft\InfoModel, Craft\LogEntryModel, Craft\MatrixBlockModel, Craft\MatrixBlockTypeModel, Craft\MatrixSettingsModel, Craft\Model, Craft\NumberFieldTypeSettingsModel, Craft\PasswordModel, Craft\PluginNewReleaseModel, Craft\PluginUpdateModel, Craft\RebrandEmailModel, Craft\SectionLocaleModel, Craft\SectionModel, Craft\SiteSettingsModel, Craft\StructureModel, Craft\TagGroupModel, Craft\TagModel, Craft\TaskModel, Craft\UpdateModel, Craft\UpgradeInfoModel, Craft\UpgradePurchaseModel, Craft\UrlModel, Craft\UserGroupModel, Craft\UserModel, Craft\UsernameModel, Craft\WidgetModel
Since
1.0

Model base class.

See also http://craftcms.com

View source (opens new window)

# Protected Properties

Property Description
$classSuffix string (opens new window)
$strictAttributes boolean (opens new window) – Whether this model should be strict about only allowing values to be set on defined attributes

# $classSuffix

Signature

protected string $classSuffix = 'Model'

# $strictAttributes

Type
boolean (opens new window)

Whether this model should be strict about only allowing values to be set on defined attributes

View source (opens new window)

Signature

protected boolean $strictAttributes = true

# Public Methods

Method Description
__call() Magic __call() method, used for chain-setting attribute values.
__construct() Constructor
__get() PHP getter magic method.
__isset() Treats attributes defined by defineAttributes() as properties.
__set() PHP setter magic method.
__unset() (opens new window) Sets a component property to be null.
addError() (opens new window) Adds a new error to the specified attribute.
addErrors() (opens new window) Adds a list of errors.
asa() (opens new window) Returns the named behavior object.
attachBehavior() (opens new window) Attaches a behavior to this component.
attachBehaviors() (opens new window) Attaches a list of behaviors to the component.
attachEventHandler() (opens new window) Attaches an event handler to an event.
attributeLabels() Returns the attribute labels.
attributeNames() Returns the list of this model's attribute names.
behaviors() (opens new window) Returns a list of behaviors that this model should behave as.
canGetProperty() (opens new window) Determines whether a property can be read.
canSetProperty() (opens new window) Determines whether a property can be set.
clearErrors() (opens new window) Removes errors for all attributes or a single attribute.
copy() Returns a copy of this model.
createValidators() (opens new window) Creates validator objects based on the specification in {@link rules}.
detachBehavior() (opens new window) Detaches a behavior from the component.
detachBehaviors() (opens new window) Detaches all behaviors from the component.
detachEventHandler() (opens new window) Detaches an existing event handler.
disableBehavior() (opens new window) Disables an attached behavior.
disableBehaviors() (opens new window) Disables all behaviors attached to this component.
enableBehavior() (opens new window) Enables an attached behavior.
enableBehaviors() (opens new window) Enables all behaviors attached to this component.
evaluateExpression() (opens new window) Evaluates a PHP expression or callback under the context of this component.
generateAttributeLabel() Generates a user friendly attribute label.
getAllErrors() Returns all errors in a single, flattened list, devoid of attribute names.
getAttribute() Gets an attribute’s value.
getAttributeConfigs() Returns this model's normalized attribute configs.
getAttributeLabel() (opens new window) Returns the text label for the specified attribute.
getAttributes() Returns an array of attribute values.
getClassHandle() Get the class name, sans namespace and suffix.
getError() (opens new window) Returns the first error of the specified attribute.
getErrors() (opens new window) Returns the errors for all attribute or a single attribute.
getEventHandlers() (opens new window) Returns the list of attached event handlers for an event.
getExtraAttributeNames() Returns a list of the names of the extra attributes that have been saved on this model, if it's not strict.
getIterator() (opens new window) Returns an iterator for traversing the attributes in the model.
getSafeAttributeNames() (opens new window) Returns the attribute names that are safe to be massively assigned.
getScenario() (opens new window) Returns the scenario that this model is used in.
getValidatorList() (opens new window) Returns all the validators declared in the model.
getValidators() (opens new window) Returns the validators applicable to the current {@link scenario}.
hasErrors() (opens new window) Returns a value indicating whether there is any validation error.
hasEvent() (opens new window) Determines whether an event is defined.
hasEventHandler() (opens new window) Checks whether the named event has attached handlers.
hasProperty() (opens new window) Determines whether a property is defined.
isAttributeRequired() (opens new window) Returns a value indicating whether the attribute is required.
isAttributeSafe() (opens new window) Returns a value indicating whether the attribute is safe for massive assignments.
offsetExists() Treats attributes defined by defineAttributes() as array offsets.
offsetGet() (opens new window) Returns the element at the specified offset.
offsetSet() (opens new window) Sets the element at the specified offset.
offsetUnset() (opens new window) Unsets the element at the specified offset.
onAfterConstruct() (opens new window) This event is raised after the model instance is created by new operator.
onAfterValidate() (opens new window) This event is raised after the validation is performed.
onBeforeValidate() (opens new window) This event is raised before the validation is performed.
onUnsafeAttribute() (opens new window) This method is invoked when an unsafe attribute is being massively assigned.
populateModel() Populates a new model instance with a given set of attributes.
populateModels() Mass-populates models based on an array of attribute arrays.
raiseEvent() (opens new window) Raises an event.
rules() Returns this model's validation rules.
setAttribute() Sets an attribute's value.
setAttributes() Sets multiple attribute values at once.
setScenario() (opens new window) Sets the scenario for the model.
unsetAttributes() (opens new window) Sets the attributes to be null.
validate() Validates all of the attributes for the current Model. Any attributes that fail validation will additionally get logged to the craft/storage/runtime/logs folder with a level of LogLevel::Warning.

# __call()

Magic __call() method, used for chain-setting attribute values.

View source (opens new window)

Arguments

Returns

Craft\BaseModel

Signature

public Craft\BaseModel __call ( $name, $arguments )

# __construct()

Constructor

View source (opens new window)

Arguments

  • $attributes (mixed)

Returns

Craft\BaseModel

Signature

public Craft\BaseModel __construct ( $attributes = null )

# __get()

PHP getter magic method.

View source (opens new window)

Arguments

Returns

mixed

Signature

public mixed __get ( $name )

# __isset()

Treats attributes defined by defineAttributes() as properties.

View source (opens new window)

Arguments

Returns

boolean (opens new window)

Signature

public boolean __isset ( $name )

# __set()

PHP setter magic method.

View source (opens new window)

Arguments

Returns

mixed

Signature

public mixed __set ( $name, $value )

# attributeLabels()

Returns the attribute labels.

View source (opens new window)

Returns

array (opens new window)

Signature

public array attributeLabels ( )

# attributeNames()

Returns the list of this model's attribute names.

View source (opens new window)

Returns

array (opens new window)

Signature

public array attributeNames ( )

# copy()

Returns a copy of this model.

View source (opens new window)

Returns

Craft\BaseModel

Signature

public Craft\BaseModel copy ( )

# generateAttributeLabel()

Generates a user friendly attribute label. This is done by replacing underscores or dashes with blanks and changing the first letter of each word to upper case. For example, 'department_name' or 'DepartmentName' becomes 'Department Name'.

Overriding CModel's implementation to be UTF-8 friendly since they don't consider this a bug: https://github.com/yiisoft/yii/issues/1851

View source (opens new window)

Arguments

Returns

string (opens new window) – The attribute label

Signature

public string generateAttributeLabel ( $name )

# getAllErrors()

Returns all errors in a single, flattened list, devoid of attribute names. If you need a multi-dimensional list of all errors along with their attributes, use getErrors() instead.

View source (opens new window)

Returns

array (opens new window)

Signature

public array getAllErrors ( )

# getAttribute()

Gets an attribute’s value.

View source (opens new window)

Arguments

Returns

mixed

Signature

public mixed getAttribute ( $name, $flattenValue = false )

# getAttributeConfigs()

Returns this model's normalized attribute configs.

View source (opens new window)

Returns

array (opens new window)

Signature

public array getAttributeConfigs ( )

# getAttributes()

Returns an array of attribute values.

View source (opens new window)

Arguments

Returns

array (opens new window)

Signature

public array getAttributes ( $names = null, $flattenValues = false )

# getClassHandle()

Get the class name, sans namespace and suffix.

View source (opens new window)

Returns

string (opens new window)

Signature

public string getClassHandle ( )

# getExtraAttributeNames()

Returns a list of the names of the extra attributes that have been saved on this model, if it's not strict.

View source (opens new window)

Returns

array (opens new window)

Signature

public array getExtraAttributeNames ( )

# offsetExists()

Treats attributes defined by defineAttributes() as array offsets.

View source (opens new window)

Arguments

  • $offset (mixed)

Returns

boolean (opens new window)

Signature

public boolean offsetExists ( $offset )

# populateModel()

Populates a new model instance with a given set of attributes.

View source (opens new window)

Arguments

  • $values (mixed)

Returns

Craft\BaseModel

Signature

public static Craft\BaseModel populateModel ( $values )

# populateModels()

Mass-populates models based on an array of attribute arrays.

View source (opens new window)

Arguments

Returns

array (opens new window)

Signature

public static array populateModels ( $data, $indexBy = null )

# rules()

Returns this model's validation rules.

View source (opens new window)

Returns

array (opens new window)

Signature

public array rules ( )

# setAttribute()

Sets an attribute's value.

View source (opens new window)

Arguments

Returns

boolean (opens new window)

Signature

public boolean setAttribute ( $name, $value )

# setAttributes()

Sets multiple attribute values at once.

View source (opens new window)

Arguments

  • $values (mixed)

Returns

null (opens new window)

Signature

public null setAttributes ( $values )

# validate()

Validates all of the attributes for the current Model. Any attributes that fail validation will additionally get logged to the craft/storage/runtime/logs folder with a level of LogLevel::Warning.

View source (opens new window)

Arguments

Returns

boolean (opens new window)

Signature

public boolean validate ( $attributes = null, $clearErrors = true )

# Protected Methods

Method Description
afterConstruct() (opens new window) This method is invoked after a model instance is created by new operator.
afterValidate() (opens new window) This method is invoked after validation ends.
beforeValidate() (opens new window) This method is invoked before validation starts.
defineAttributes() Defines this model's attributes.

# defineAttributes()

Defines this model's attributes.

View source (opens new window)

Returns

array (opens new window)

Signature

protected array defineAttributes ( )