\Charcoal\ModelAbstractModel

An abstract class that implements most of `ModelInterface`.

In addition to ModelInterface, the abstract model implements the following interfaces:

  • DescribableInterface
  • `StorableInterface
  • ValidatableInterface
  • ViewableInterface.

Those interfaces are implemented (in parts, at least) with the DescribableTrait, StorableTrait, ValidatableTrait and the ViewableTrait.

The JsonSerializable interface is fully provided by the DescribableTrait.

Summary

Methods
Properties
Constants
setData()
setMetadataLoader()
setMetadata()
metadata()
loadMetadata()
setMetadataIdent()
metadataIdent()
setId()
id()
setKey()
key()
setSource()
source()
load()
loadFrom()
loadFromQuery()
save()
update()
delete()
setValidator()
validator()
validate()
__construct()
setDependencies()
data()
mergeData()
defaultData()
setFlatData()
flatData()
propertyValue()
saveProperties()
loadFromL10n()
createView()
objType()
No public properties found
DEFAULT_SOURCE_TYPE
metadataLoader()
createMetadata()
generateMetadataIdent()
setSourceFactory()
sourceFactory()
createSource()
preSave()
postSave()
preUpdate()
postUpdate()
preDelete()
postDelete()
createValidator()
setIdFromData()
$metadataLoader
$metadata
$metadataIdent
$id
$key
$sourceFactory
$validator
N/A
serializedValue()
$source
N/A

Constants

DEFAULT_SOURCE_TYPE

DEFAULT_SOURCE_TYPE

Properties

$metadataIdent

$metadataIdent : string

Type

string

$id

$id : mixed

Type

mixed — The object (unique) identifier

$key

$key : string

Type

string — The object key

$sourceFactory

$sourceFactory : \Charcoal\Factory\FactoryInterface

Type

\Charcoal\Factory\FactoryInterface

Methods

setData()

setData(array  $data) : \Charcoal\Model\AbstractModel

Sets the object data, from an associative array map (or any other Traversable).

Parameters

array $data

The entity data. Will call setters.

Returns

\Charcoal\Model\AbstractModel

Chainable

setMetadata()

setMetadata(array|\Charcoal\Model\MetadataInterface  $metadata) : \Charcoal\Model\DescribableInterface

Parameters

array|\Charcoal\Model\MetadataInterface $metadata

The object's metadata.

Throws

\InvalidArgumentException

If the parameter is not an array or MetadataInterface.

Returns

\Charcoal\Model\DescribableInterface

Chainable

loadMetadata()

loadMetadata(string  $metadataIdent = null) : \Charcoal\Model\MetadataInterface

Load a metadata file and store it as a static var.

Use a MetadataLoader object and the object's metadataIdent to load the metadata content (typically from the filesystem, as json).

Parameters

string $metadataIdent

Optional ident.

Returns

\Charcoal\Model\MetadataInterface

setMetadataIdent()

setMetadataIdent(string  $metadataIdent) : \Charcoal\Model\DescribableInterface

Parameters

string $metadataIdent

The metadata ident.

Returns

\Charcoal\Model\DescribableInterface

Chainable

metadataIdent()

metadataIdent() : string

Get the metadata ident, or generate it from class name.

Returns

string

setId()

setId(mixed  $id) : \Charcoal\Source\StorableInterface

Set the object's ID. The actual property set depends on `key()`

Parameters

mixed $id

The object id (identifier / primary key value).

Throws

\InvalidArgumentException

If the argument is not scalar.

Returns

\Charcoal\Source\StorableInterface

Chainable

id()

id() : mixed

Get the object's (unique) ID. The actualy property get depends on `key()`

Throws

\Exception

If the set key is invalid.

Returns

mixed

setKey()

setKey(string  $key) : \Charcoal\Source\StorableInterface

Set the key property.

Parameters

string $key

The object key, or identifier "name".

Throws

\InvalidArgumentException

If the argument is not scalar.

Returns

\Charcoal\Source\StorableInterface

Chainable

key()

key() : string

Get the key property.

Returns

string

load()

load(mixed  $id = null) : \Charcoal\Source\StorableInterface

Load an object from the database from its ID.

Parameters

mixed $id

The identifier to load.

Returns

\Charcoal\Source\StorableInterface

Chainable

loadFrom()

loadFrom(string  $key = null, mixed  $value = null) : \Charcoal\Source\StorableInterface

Load an object from the database from its key $key.

Parameters

string $key

Key pointing a column's name.

mixed $value

Value of said column.

Returns

\Charcoal\Source\StorableInterface

Chainable.

loadFromQuery()

loadFromQuery(string  $query, array  $binds = array()) : \Charcoal\Source\StorableInterface

Load an object from the database from a custom SQL query.

Parameters

string $query

The SQL query.

array $binds

Optional. The SQL query parameters.

Returns

\Charcoal\Source\StorableInterface

Chainable.

save()

save() : boolean

Save the object's current state to storage.

Overrides default StorableTrait save() method to also save properties.

Returns

boolean

update()

update(array  $properties = null) : boolean

Update the object in storage to the current object state.

Parameters

array $properties

If set, only update the properties specified in this array.

Returns

boolean

delete()

delete() : boolean

Delete an object from storage.

Returns

boolean

validate()

validate(\Charcoal\Validator\ValidatorInterface  $v = null) : boolean

Parameters

\Charcoal\Validator\ValidatorInterface $v

Optional. A custom validator object to use for validation. If null, use object's.

Returns

boolean

__construct()

__construct(array  $data = null) 

Parameters

array $data

Dependencies.

setDependencies()

setDependencies(\Pimple\Container  $container) : void

Inject dependencies from a DI Container.

Parameters

\Pimple\Container $container

A dependencies container instance.

data()

data(array  $propertyFilters = null) : array

Return the object data as an array.

Parameters

array $propertyFilters

Optional. Property filter.

Returns

array

mergeData()

mergeData(array|\Traversable  $data) : \Charcoal\Model\EntityInterface

Override's `\Charcoal\Config\AbstractEntity`'s `setData` method to take properties into consideration.

Also add a special case, to merge values for l10n properties.

Parameters

array|\Traversable $data

The data to merge.

Returns

\Charcoal\Model\EntityInterface —

Chainable

defaultData()

defaultData() : array

Returns

array

setFlatData()

setFlatData(array  $flatData) : \Charcoal\Model\AbstractModel

Sets the data

This function takes a 1-dimensional array and fill the object with its value.

Parameters

array $flatData

The data, as a flat (1-dimension) array.

Returns

\Charcoal\Model\AbstractModel

Chainable

flatData()

flatData() : array

Returns

array

propertyValue()

propertyValue(string  $propertyIdent) : mixed

Parameters

string $propertyIdent

The property ident to get the value from.

Returns

mixed

saveProperties()

saveProperties(array  $properties = null) : boolean

Parameters

array $properties

Optional array of properties to save. If null, use all object's properties.

Returns

boolean

loadFromL10n()

loadFromL10n(string  $key, mixed  $value, array  $langs) : string

Load an object from the database from its l10n key $key.

Also retrieve and return the actual language that matched.

Parameters

string $key

Key pointing a column's l10n base ident.

mixed $value

Value to search in all languages.

array $langs

List of languages (code, ex: "en") to check into.

Throws

\Exception

If the PDO query fails.

Returns

string —

The matching language.

createView()

createView(array  $data = null) : \Charcoal\Model\ViewInterface

Parameters

array $data

Optional. View data.

Returns

\Charcoal\Model\ViewInterface

objType()

objType() : string

Convert the current class name in "type-ident" format.

Returns

string

metadataLoader()

metadataLoader() : \Charcoal\Model\Service\MetadataLoader

Safe MetdataLoader getter. Create the loader if it does not exist.

Throws

\Exception

If the metadata loader was not set.

Returns

\Charcoal\Model\Service\MetadataLoader

generateMetadataIdent()

generateMetadataIdent() : string

Generate a metadata ident from class name.

Change \ and . to / and force lowercase

Returns

string

setSourceFactory()

setSourceFactory(\Charcoal\Factory\FactoryInterface  $factory) : \Charcoal\Source\StorableInterface

Parameters

\Charcoal\Factory\FactoryInterface $factory

The source factory, which is useful to create source.

Returns

\Charcoal\Source\StorableInterface

Chainable

sourceFactory()

sourceFactory() : \Charcoal\Factory\FactoryInterface

Throws

\Exception

If the source factory was not previously set.

Returns

\Charcoal\Factory\FactoryInterface

createSource()

createSource() : \Charcoal\Model\SourceInterface

StorableInterface > createSource()

Throws

\Exception

If the metadata source can not be found.

Returns

\Charcoal\Model\SourceInterface

preSave()

preSave() : boolean

StorableTrait > preSave(). Save hook called before saving the model.

Returns

boolean

postSave()

postSave() : boolean

Returns

boolean

preUpdate()

preUpdate(array<mixed,string>  $properties = null) : boolean

StorableTrait > preUpdate(). Update hook called before updating the model.

Parameters

array<mixed,string> $properties

Optional. The properties to update.

Returns

boolean

postUpdate()

postUpdate(array<mixed,string>  $keys = null) : boolean

Parameters

array<mixed,string> $keys

Optional. The list of keys to update.

Returns

boolean

preDelete()

preDelete() : boolean

Returns

boolean

postDelete()

postDelete() : boolean

Returns

boolean

createValidator()

createValidator(array  $data = null) : \Charcoal\Model\ValidatorInterface

ValidatableInterface > create_validator().

Parameters

array $data

Optional.

Returns

\Charcoal\Model\ValidatorInterface

setIdFromData()

setIdFromData(array|\Traversable  $data) : array|\Traversable

Set the object's ID from an associative array map (or any other Traversable).

Useful for setting the object ID before the rest of the object's data.

Parameters

array|\Traversable $data

The object data.

Returns

array|\Traversable —

The object data without the pre-set ID.

serializedValue()

serializedValue(mixed  $val) : mixed

Parameters

mixed $val

The value to serialize.

Returns

mixed