\Charcoal\ModelModel

Charcoal Model class

The Charcoal Model

The Charcoal Model UML
The Charcoal Model Class Diagram

Custom Object Type

It is possible to attach a custom object type ($_obj_type) to a Model. This will allow the various loaders (metadata and source data)

Metadata

In Charcoal, all models are held in an instance of \Charcoal\Model\Model and its configuration meta-data structure is defined in a \Charcoal\Model\Metadata object.

Loading metadata

To access the metadata, use $this->metadata(). To set metadata, use either

Properties

The Model Attributes are stored in \Charcoal\Property\PropertyInterface objects. The properties are defined in the Model's metadata and can be accessed either with p($ident) to retrieve a property or with properties() to get all properties.

Data Source

The Model data (which is stored internally in the class) can be stored in a storage Source object. There is only one source type currently implemented: \Charcoal\Source\Database.

Loading from source

...

Loading into Collection

...

Data validation

Once an object has had its data filled (from a form, database, or other source), it is possible to check wether the data is conform to the object definition, as defined by it's properties and meta-properties. This check is done with the validate() function.

The validate() method always return a boolean (true for success and false if there was any validation error(s)). The validation details are held in a Validator object which can then be accessed with the validator() method.

Rendering a model

Every Charcoal Model can be rendered with the help of a View and a ViewController. Or, more precisely, a \Charcoal\View\ModelView and a \Charcoal\View\ModelViewController.

Summary

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

__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.

setData()

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

Describable object needs to have a `setData()` method

Parameters

array $data

The object's data.

Returns

\Charcoal\Model\DescribableInterface

Chainable

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.

save()

save() : boolean

Save an object current state to storage

Returns

boolean

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

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.

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

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.

preSave()

preSave() : boolean

Returns

boolean

preUpdate()

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

Parameters

array<mixed,string> $keys

Optional. The list of keys to update.

Returns

boolean

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

postSave()

postSave() : boolean

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

serializedValue()

serializedValue(mixed  $val) : mixed

Parameters

mixed $val

The value to serialize.

Returns

mixed