Craft 2 Class Reference

Class BaseElementModel

Package
craft.app.models
Namespace
Craft
Inheritance
abstract class BaseElementModel » BaseModel » \CModel » \CComponent
Implements
\IteratorAggregate, \Traversable, \ArrayAccess
Subclasses
AssetFileModel, CategoryModel, EntryModel, GlobalSetModel, MatrixBlockModel, TagModel, UserModel
Since
1.0
Source Code
craft/app/models/BaseElementModel.php

Base element model class.

Public Properties
Property Type Description Defined By
allErrors array Returns all errors in a single, flattened list, devoid of attribute names. BaseModel
ancestors ElementCriteriaModel Returns the element's ancestors. BaseElementModel
attributeConfigs array Returns this model's normalized attribute configs. BaseModel
attributes array Returns an array of attribute values. BaseModel
children ElementCriteriaModel Returns the element's children. BaseElementModel
classHandle string Get the class name, sans namespace and suffix. BaseModel
content ContentModel Returns the content for the element. BaseElementModel
contentFromPost array Returns the raw content from the post data, before it was passed through prepValueFromPost(). BaseElementModel
contentPostLocation string / null Returns the location in POST that the content was pulled from. BaseElementModel
contentTable string Returns the name of the table this element's content is stored in. BaseElementModel
cpEditUrl string / false Returns the element's CP edit URL. BaseElementModel
descendants ElementCriteriaModel Returns the element's descendants. BaseElementModel
elementType string Returns the type of element this is. BaseElementModel
errors array Returns the errors for all attribute or a single attribute. \CModel
extraAttributeNames array Returns a list of the names of the extra attributes that have been saved on this model, if it's not strict. BaseModel
fieldColumnPrefix string Returns the field column prefix this element's content uses. BaseElementModel
fieldContext string Returns the field context this element's content uses. BaseElementModel
fieldLayout FieldLayoutModel / null Returns the field layout used by this element. BaseElementModel
hasFreshContent bool Returns whether the element’s content is "fresh" (unsaved and without validation errors). BaseElementModel
iterator \CMapIterator Returns an iterator for traversing the attributes in the model. \CModel
locales array Returns the locale IDs this element is available in. BaseElementModel
next ElementCriteriaModel / null Returns the next element relative to this one, from a given set of criteria. BaseElementModel
nextSibling BaseElementModel / null Returns the element's next sibling. BaseElementModel
parent BaseElementModel / null Get the element's parent. BaseElementModel
parents ElementCriteriaModel Returns a new ElementCriteriaModel prepped to return this element's same-type parents. BaseElementModel
prev ElementCriteriaModel / null Returns the previous element relative to this one, from a given set of criteria. BaseElementModel
prevSibling BaseElementModel / null Returns the element's previous sibling. BaseElementModel
rawContent mixed Returns the raw content saved on this entity. BaseElementModel
ref string / null Returns the reference string to this element. BaseElementModel
safeAttributeNames array Returns the attribute names that are safe to be massively assigned. \CModel
scenario string Returns the scenario that this model is used in. \CModel
siblings ElementCriteriaModel Returns all of the element's siblings. BaseElementModel
status string / null Returns the element's status. BaseElementModel
thumbUrl string / null Returns the URL to the element's thumbnail, if there is one. BaseElementModel
title string Returns the element's title. BaseElementModel
totalDescendants bool Returns the total number of descendants that the element has. BaseElementModel
url string Returns the element's full URL. BaseElementModel
urlFormat string / null Returns the URL format used to generate this element's URL. BaseElementModel
validatorList \CList Returns all the validators declared in the model. \CModel
validators array Returns the validators applicable to the current scenario. \CModel
Protected Properties
Property Type Description Defined By
classSuffix string BaseModel
strictAttributes bool Whether this model should be strict about only allowing values to be set on defined attributes BaseModel
Public Methods
Method Description Defined By
__call() Magic __call() method, used for chain-setting attribute values. BaseModel
__construct() Constructor BaseModel
__get() Getter BaseElementModel
__isset() Treats custom fields as properties. BaseElementModel
__set() PHP setter magic method. BaseModel
__toString() Use the element's title as its string representation. BaseElementModel
__unset() Sets a component property to be null. \CComponent
addError() Adds a new error to the specified attribute. \CModel
addErrors() Adds a list of errors. \CModel
asa() Returns the named behavior object. \CComponent
attachBehavior() Attaches a behavior to this component. \CComponent
attachBehaviors() Attaches a list of behaviors to the component. \CComponent
attachEventHandler() Attaches an event handler to an event. \CComponent
attributeLabels() Returns the attribute labels. BaseModel
attributeNames() Returns the list of this model's attribute names. BaseModel
behaviors() Returns a list of behaviors that this model should behave as. \CModel
canGetProperty() Determines whether a property can be read. \CComponent
canSetProperty() Determines whether a property can be set. \CComponent
clearErrors() Removes errors for all attributes or a single attribute. \CModel
copy() Returns a copy of this model. BaseModel
createValidators() Creates validator objects based on the specification in rules. \CModel
detachBehavior() Detaches a behavior from the component. \CComponent
detachBehaviors() Detaches all behaviors from the component. \CComponent
detachEventHandler() Detaches an existing event handler. \CComponent
disableBehavior() Disables an attached behavior. \CComponent
disableBehaviors() Disables all behaviors attached to this component. \CComponent
enableBehavior() Enables an attached behavior. \CComponent
enableBehaviors() Enables all behaviors attached to this component. \CComponent
evaluateExpression() Evaluates a PHP expression or callback under the context of this component. \CComponent
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'. BaseModel
getAllErrors() Returns all errors in a single, flattened list, devoid of attribute names. BaseModel
getAncestors() Returns the element's ancestors. BaseElementModel
getAttribute() Gets an attribute’s value. BaseElementModel
getAttributeConfigs() Returns this model's normalized attribute configs. BaseModel
getAttributeLabel() Returns the text label for the specified attribute. \CModel
getAttributes() Returns an array of attribute values. BaseModel
getChildren() Returns the element's children. BaseElementModel
getClassHandle() Get the class name, sans namespace and suffix. BaseModel
getContent() Returns the content for the element. BaseElementModel
getContentFromPost() Returns the raw content from the post data, before it was passed through prepValueFromPost(). BaseElementModel
getContentPostLocation() Returns the location in POST that the content was pulled from. BaseElementModel
getContentTable() Returns the name of the table this element's content is stored in. BaseElementModel
getCpEditUrl() Returns the element's CP edit URL. BaseElementModel
getDescendants() Returns the element's descendants. BaseElementModel
getEagerLoadedElements() Returns some eager-loaded elements on a given handle. BaseElementModel
getElementType() Returns the type of element this is. BaseElementModel
getError() Returns the first error of the specified attribute. \CModel
getErrors() Returns the errors for all attribute or a single attribute. \CModel
getEventHandlers() Returns the list of attached event handlers for an event. \CComponent
getExtraAttributeNames() Returns a list of the names of the extra attributes that have been saved on this model, if it's not strict. BaseModel
getFieldColumnPrefix() Returns the field column prefix this element's content uses. BaseElementModel
getFieldContext() Returns the field context this element's content uses. BaseElementModel
getFieldLayout() Returns the field layout used by this element. BaseElementModel
getFieldValue() Returns the prepped content for a given field. BaseElementModel
getHasFreshContent() Returns whether the element’s content is "fresh" (unsaved and without validation errors). BaseElementModel
getIterator() Returns an iterator for traversing the attributes in the model. \CModel
getLocales() Returns the locale IDs this element is available in. BaseElementModel
getNext() Returns the next element relative to this one, from a given set of criteria. BaseElementModel
getNextSibling() Returns the element's next sibling. BaseElementModel
getParent() Get the element's parent. BaseElementModel
getParents() Returns a new ElementCriteriaModel prepped to return this element's same-type parents. BaseElementModel
getPrev() Returns the previous element relative to this one, from a given set of criteria. BaseElementModel
getPrevSibling() Returns the element's previous sibling. BaseElementModel
getRawContent() Returns the raw content saved on this entity. BaseElementModel
getRef() Returns the reference string to this element. BaseElementModel
getSafeAttributeNames() Returns the attribute names that are safe to be massively assigned. \CModel
getScenario() Returns the scenario that this model is used in. \CModel
getSiblings() Returns all of the element's siblings. BaseElementModel
getStatus() Returns the element's status. BaseElementModel
getThumbUrl() Returns the URL to the element's thumbnail, if there is one. BaseElementModel
getTitle() Returns the element's title. BaseElementModel
getTotalDescendants() Returns the total number of descendants that the element has. BaseElementModel
getUrl() Returns the element's full URL. BaseElementModel
getUrlFormat() Returns the URL format used to generate this element's URL. BaseElementModel
getValidatorList() Returns all the validators declared in the model. \CModel
getValidators() Returns the validators applicable to the current scenario. \CModel
hasDescendants() Returns whether the element has descendants. BaseElementModel
hasEagerLoadedElements() Returns whether elements have been eager-loaded with a given handle. BaseElementModel
hasErrors() Returns a value indicating whether there is any validation error. \CModel
hasEvent() Determines whether an event is defined. \CComponent
hasEventHandler() Checks whether the named event has attached handlers. \CComponent
hasProperty() Determines whether a property is defined. \CComponent
isAncestorOf() Returns whether this element is an ancestor of another one. BaseElementModel
isAttributeRequired() Returns a value indicating whether the attribute is required. \CModel
isAttributeSafe() Returns a value indicating whether the attribute is safe for massive assignments. \CModel
isChildOf() Returns whether this element is a direct child of another one. BaseElementModel
isDescendantOf() Returns whether this element is a descendant of another one. BaseElementModel
isEditable() Returns whether the current user can edit the element. BaseElementModel
isNextSiblingOf() Returns whether this element is the direct next sibling of another one. BaseElementModel
isParentOf() Returns whether this element is a direct parent of another one. BaseElementModel
isPrevSiblingOf() Returns whether this element is the direct previous sibling of another one. BaseElementModel
isSiblingOf() Returns whether this element is a sibling of another one. BaseElementModel
offsetExists() Treats custom fields as array offsets. BaseElementModel
offsetGet() Returns the element at the specified offset. \CModel
offsetSet() Sets the element at the specified offset. \CModel
offsetUnset() Unsets the element at the specified offset. \CModel
onAfterConstruct() This event is raised after the model instance is created by new operator. \CModel
onAfterValidate() This event is raised after the validation is performed. \CModel
onBeforeValidate() This event is raised before the validation is performed. \CModel
onUnsafeAttribute() This method is invoked when an unsafe attribute is being massively assigned. \CModel
populateModel() Populates a new model instance with a given set of attributes. BaseElementModel
populateModels() Mass-populates models based on an array of attribute arrays. BaseModel
raiseEvent() Raises an event. \CComponent
rules() Returns this model's validation rules. BaseModel
setAttribute() Sets an attribute's value. BaseModel
setAttributes() Sets multiple attribute values at once. BaseModel
setContent() Sets the content for the element. BaseElementModel
setContentFromPost() Sets the content from post data, calling prepValueFromPost() on the field types. BaseElementModel
setContentPostLocation() Sets the location in POST that the content was pulled from. BaseElementModel
setEagerLoadedElements() Sets some eager-loaded elements on a given handle. BaseElementModel
setNext() Sets the default next element. BaseElementModel
setParent() Sets the element's parent. BaseElementModel
setPrev() Sets the default previous element. BaseElementModel
setRawPostContent() Sets a field’s raw post content. BaseElementModel
setScenario() Sets the scenario for the model. \CModel
unsetAttributes() Sets the attributes to be null. \CModel
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. BaseModel
Protected Methods
Method Description Defined By
afterConstruct() This method is invoked after a model instance is created by new operator. \CModel
afterValidate() This method is invoked after validation ends. \CModel
beforeValidate() This method is invoked before validation starts. \CModel
createContent() Creates the content model associated with this element. BaseElementModel
defineAttributes() Defines this model's attributes. BaseElementModel
getFieldByHandle() Returns the field with a given handle. BaseElementModel
Events
Event Description Defined By
onAfterConstruct This event is raised after the model instance is created by new operator. \CModel
onBeforeValidate This event is raised before the validation is performed. \CModel
onAfterValidate This event is raised after the validation is performed. \CModel
onUnsafeAttribute This method is invoked when an unsafe attribute is being massively assigned. \CModel

Property Details

ancestors public property #

Returns the element's ancestors.

public ElementCriteriaModel getAncestors(int|null $dist = null)

children public property #

Returns the element's children.

public ElementCriteriaModel getChildren(mixed $field = null)

content public property #

Returns the content for the element.

public ContentModel getContent()
public null setContent(ContentModel|array $content)

contentFromPost public property #

Returns the raw content from the post data, before it was passed through prepValueFromPost().

public array getContentFromPost()
public null setContentFromPost(array|string $content)

contentPostLocation public property #

Returns the location in POST that the content was pulled from.

public string|null getContentPostLocation()
public string|null setContentPostLocation($contentPostLocation $contentPostLocation)

contentTable public property #

Returns the name of the table this element's content is stored in.

public string getContentTable()

cpEditUrl public property #

Returns the element's CP edit URL.

public string|false getCpEditUrl()

descendants public property #

Returns the element's descendants.

public ElementCriteriaModel getDescendants(int|null $dist = null)

elementType public property #

Returns the type of element this is.

public string getElementType()

fieldColumnPrefix public property #

Returns the field column prefix this element's content uses.

public string getFieldColumnPrefix()

fieldContext public property #

Returns the field context this element's content uses.

public string getFieldContext()

fieldLayout public property #

Returns the field layout used by this element.

public FieldLayoutModel|null getFieldLayout()

hasFreshContent public property #

Returns whether the element’s content is "fresh" (unsaved and without validation errors).

public bool getHasFreshContent()

Returns an anchor pre-filled with this element's URL and title.

public \Twig_Markup getLink()

locales public property #

Returns the locale IDs this element is available in.

public array getLocales()

next public property #

Returns the next element relative to this one, from a given set of criteria.

public ElementCriteriaModel|null getNext(mixed $criteria = false)
public null setNext(BaseElementModel|false $element)

nextSibling public property #

Returns the element's next sibling.

public BaseElementModel|null getNextSibling()

parent public property #

Get the element's parent.

public BaseElementModel|null getParent()
public null setParent(BaseElementModel|null $parent)

parents public property #

Returns a new ElementCriteriaModel prepped to return this element's same-type parents.

Deprecated in 1.3. Use the relatedTo param instead.

public ElementCriteriaModel getParents(mixed $field = null)

prev public property #

Returns the previous element relative to this one, from a given set of criteria.

public ElementCriteriaModel|null getPrev(mixed $criteria = false)
public void setPrev(BaseElementModel|false $element)

prevSibling public property #

Returns the element's previous sibling.

public BaseElementModel|null getPrevSibling()

rawContent public property #

Returns the raw content saved on this entity.

This is now deprecated. Use getContent() to get the ContentModel instead.

Deprecated in 2.0. Use getContent() instead.

public mixed getRawContent(string|null $fieldHandle = null)

ref public property #

Returns the reference string to this element.

public string|null getRef()

siblings public property #

Returns all of the element's siblings.

public ElementCriteriaModel getSiblings()

status public property #

Returns the element's status.

public string|null getStatus()

thumbUrl public property #

Returns the URL to the element's thumbnail, if there is one.

public string|null getThumbUrl(int|null $size = null)

title public property #

Returns the element's title.

public string getTitle()

totalDescendants public property #

Returns the total number of descendants that the element has.

public bool getTotalDescendants()

url public property #

Returns the element's full URL.

public string getUrl()

urlFormat public property #

Returns the URL format used to generate this element's URL.

public string|null getUrlFormat()

Method Details

__get() public method #

Getter

public mixed __get(string $name)
$name string
Returns mixed

__isset() public method #

Treats custom fields as properties.

public bool __isset(string $name)
$name string
Returns bool

__toString() public method #

Use the element's title as its string representation.

public string __toString()
Returns string

createContent() protected method #

Creates the content model associated with this element.

protected ContentModel createContent()
Returns ContentModel The content model associated with this element

defineAttributes() protected method #

Defines this model's attributes.

protected array defineAttributes()
Returns array

getAncestors() public method #

Returns the element's ancestors.

public ElementCriteriaModel getAncestors(int|null $dist = null)
$dist int / null
Returns ElementCriteriaModel

getAttribute() public method #

Gets an attribute’s value.

public mixed getAttribute(string $name, bool $flattenValue = false)
$name string The attribute’s name.
$flattenValue bool
Returns mixed

getChildren() public method #

Returns the element's children.

public ElementCriteriaModel getChildren(mixed $field = null)
$field mixed If this function is being used in the deprecated relationship-focused way, $field defines which field (if any) to limit the relationships by.
Returns ElementCriteriaModel

getContent() public method #

Returns the content for the element.

public ContentModel getContent()
Returns ContentModel

getContentFromPost() public method #

Returns the raw content from the post data, before it was passed through prepValueFromPost().

public array getContentFromPost()
Returns array

getContentPostLocation() public method #

Returns the location in POST that the content was pulled from.

public string|null getContentPostLocation()
Returns string / null

getContentTable() public method #

Returns the name of the table this element's content is stored in.

public string getContentTable()
Returns string

getCpEditUrl() public method #

Returns the element's CP edit URL.

public string|false getCpEditUrl()
Returns string / false

getDescendants() public method #

Returns the element's descendants.

public ElementCriteriaModel getDescendants(int|null $dist = null)
$dist int / null
Returns ElementCriteriaModel

getEagerLoadedElements() public method #

Returns some eager-loaded elements on a given handle.

public BaseElementModel[]|null getEagerLoadedElements(string $handle)
$handle string The handle of the eager-loaded elements
Returns BaseElementModel[] / null The eager-loaded elements, or null

getElementType() public method #

Returns the type of element this is.

public string getElementType()
Returns string

getFieldByHandle() protected method #

Returns the field with a given handle.

protected FieldModel|null getFieldByHandle(string $handle)
$handle string
Returns FieldModel / null

getFieldColumnPrefix() public method #

Returns the field column prefix this element's content uses.

public string getFieldColumnPrefix()
Returns string

getFieldContext() public method #

Returns the field context this element's content uses.

public string getFieldContext()
Returns string

getFieldLayout() public method #

Returns the field layout used by this element.

public FieldLayoutModel|null getFieldLayout()
Returns FieldLayoutModel / null

getFieldValue() public method #

Returns the prepped content for a given field.

public mixed getFieldValue(string $fieldHandle)
$fieldHandle string
Returns mixed

getHasFreshContent() public method #

Returns whether the element’s content is "fresh" (unsaved and without validation errors).

public bool getHasFreshContent()
Returns bool Whether the element’s content is fresh

Returns an anchor pre-filled with this element's URL and title.

public \Twig_Markup getLink()
Returns \Twig_Markup

getLocales() public method #

Returns the locale IDs this element is available in.

public array getLocales()
Returns array

getNext() public method #

Returns the next element relative to this one, from a given set of criteria.

public ElementCriteriaModel|null getNext(mixed $criteria = false)
$criteria mixed
Returns ElementCriteriaModel / null

getNextSibling() public method #

Returns the element's next sibling.

public BaseElementModel|null getNextSibling()
Returns BaseElementModel / null

getParent() public method #

Get the element's parent.

public BaseElementModel|null getParent()
Returns BaseElementModel / null

getParents() public method #

Returns a new ElementCriteriaModel prepped to return this element's same-type parents.

Deprecated in 1.3. Use the relatedTo param instead.

public ElementCriteriaModel getParents(mixed $field = null)
$field mixed
Returns ElementCriteriaModel

getPrev() public method #

Returns the previous element relative to this one, from a given set of criteria.

public ElementCriteriaModel|null getPrev(mixed $criteria = false)
$criteria mixed
Returns ElementCriteriaModel / null

getPrevSibling() public method #

Returns the element's previous sibling.

public BaseElementModel|null getPrevSibling()
Returns BaseElementModel / null

getRawContent() public method #

Returns the raw content saved on this entity.

This is now deprecated. Use getContent() to get the ContentModel instead.

Deprecated in 2.0. Use getContent() instead.

public mixed getRawContent(string|null $fieldHandle = null)
$fieldHandle string / null
Returns mixed

getRef() public method #

Returns the reference string to this element.

public string|null getRef()
Returns string / null

getSiblings() public method #

Returns all of the element's siblings.

public ElementCriteriaModel getSiblings()
Returns ElementCriteriaModel

getStatus() public method #

Returns the element's status.

public string|null getStatus()
Returns string / null

getThumbUrl() public method #

Returns the URL to the element's thumbnail, if there is one.

public string|null getThumbUrl(int|null $size = null)
$size int / null
Returns string / null

getTitle() public method #

Returns the element's title.

public string getTitle()
Returns string

getTotalDescendants() public method #

Returns the total number of descendants that the element has.

public bool getTotalDescendants()
Returns bool

getUrl() public method #

Returns the element's full URL.

public string getUrl()
Returns string

getUrlFormat() public method #

Returns the URL format used to generate this element's URL.

public string|null getUrlFormat()
Returns string / null

hasDescendants() public method #

Returns whether the element has descendants.

public bool hasDescendants()
Returns bool

hasEagerLoadedElements() public method #

Returns whether elements have been eager-loaded with a given handle.

public bool hasEagerLoadedElements(string $handle)
$handle string The handle of the eager-loaded elements
Returns bool Whether elements have been eager-loaded with the given handle

isAncestorOf() public method #

Returns whether this element is an ancestor of another one.

public bool isAncestorOf(BaseElementModel $element)
$element BaseElementModel
Returns bool

isChildOf() public method #

Returns whether this element is a direct child of another one.

public bool isChildOf(BaseElementModel $element)
$element BaseElementModel
Returns bool

isDescendantOf() public method #

Returns whether this element is a descendant of another one.

public bool isDescendantOf(BaseElementModel $element)
$element BaseElementModel
Returns bool

isEditable() public method #

Returns whether the current user can edit the element.

public bool isEditable()
Returns bool

isNextSiblingOf() public method #

Returns whether this element is the direct next sibling of another one.

public bool isNextSiblingOf(BaseElementModel $element)
$element BaseElementModel
Returns bool

isParentOf() public method #

Returns whether this element is a direct parent of another one.

public bool isParentOf(BaseElementModel $element)
$element BaseElementModel
Returns bool

isPrevSiblingOf() public method #

Returns whether this element is the direct previous sibling of another one.

public bool isPrevSiblingOf(BaseElementModel $element)
$element BaseElementModel
Returns bool

isSiblingOf() public method #

Returns whether this element is a sibling of another one.

public bool isSiblingOf(BaseElementModel $element)
$element BaseElementModel
Returns bool

offsetExists() public method #

Treats custom fields as array offsets.

public bool offsetExists(mixed $offset)
$offset mixed
Returns bool

populateModel() public static method #

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

public static BaseModel populateModel(mixed $values)
$values mixed
Returns BaseModel

setContent() public method #

Sets the content for the element.

public null setContent(ContentModel|array $content)
$content ContentModel / array

setContentFromPost() public method #

Sets the content from post data, calling prepValueFromPost() on the field types.

public null setContentFromPost(array|string $content)
$content array / string

setContentPostLocation() public method #

Sets the location in POST that the content was pulled from.

public string|null setContentPostLocation($contentPostLocation $contentPostLocation)
$contentPostLocation $contentPostLocation
Returns string / null

setEagerLoadedElements() public method #

Sets some eager-loaded elements on a given handle.

public void setEagerLoadedElements(string $handle, array $elements)
$handle string The handle to load the elements with in the future
$elements array The eager-loaded elements

setNext() public method #

Sets the default next element.

public null setNext(BaseElementModel|false $element)
$element BaseElementModel / false

setParent() public method #

Sets the element's parent.

public null setParent(BaseElementModel|null $parent)
$parent BaseElementModel / null

setPrev() public method #

Sets the default previous element.

public void setPrev(BaseElementModel|false $element)
$element BaseElementModel / false return void

setRawPostContent() public method #

Sets a field’s raw post content.

public void setRawPostContent(string $handle, string|array $value)
$handle string The field handle.
$value string / array The posted field value.