DEFAULT_SOURCE_TYPE
DEFAULT_SOURCE_TYPE
Charcoal Model class
It is possible to attach a custom object type ($_obj_type
) to a Model. This will allow the
various loaders (metadata and source data)
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.
To access the metadata, use $this->metadata()
. To set metadata, use either
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.
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
.
...
...
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.
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
.
$metadataLoader : \Charcoal\Model\Service\MetadataLoader
$metadata : \Charcoal\Model\MetadataInterface
$validator : \Charcoal\Validator\ValidatorInterface
In-objet copy of the `ValidatorInterface` validator object
$source : \Charcoal\Source\SourceInterface
setData(array $data) : \Charcoal\Model\DescribableInterface
Describable object needs to have a `setData()` method
array | $data | The object's data. |
Chainable
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.
array|\Traversable | $data | The data to merge. |
Chainable
setFlatData(array $flatData) : \Charcoal\Model\AbstractModel
Sets the data
This function takes a 1-dimensional array and fill the object with its value.
array | $flatData | The data, as a flat (1-dimension) array. |
Chainable
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.
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. |
If the PDO query fails.
The matching language.
setMetadataLoader(\Charcoal\Model\Service\MetadataLoader $loader) : \Charcoal\Model\DescribableInterface
\Charcoal\Model\Service\MetadataLoader | $loader | The loader instance, used to load metadata. |
Chainable
setMetadata(array|\Charcoal\Model\MetadataInterface $metadata) : \Charcoal\Model\DescribableInterface
array|\Charcoal\Model\MetadataInterface | $metadata | The object's metadata. |
If the parameter is not an array or MetadataInterface.
Chainable
metadata() : \Charcoal\Model\MetadataInterface
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).
string | $metadataIdent | Optional ident. |
setMetadataIdent(string $metadataIdent) : \Charcoal\Model\DescribableInterface
string | $metadataIdent | The metadata ident. |
Chainable
setId(mixed $id) : \Charcoal\Source\StorableInterface
Set the object's ID. The actual property set depends on `key()`
mixed | $id | The object id (identifier / primary key value). |
If the argument is not scalar.
Chainable
setKey(string $key) : \Charcoal\Source\StorableInterface
Set the key property.
string | $key | The object key, or identifier "name". |
If the argument is not scalar.
Chainable
setSource(\Charcoal\Source\SourceInterface $source) : \Charcoal\Source\StorableInterface
Set the object's source.
\Charcoal\Source\SourceInterface | $source | The storable object's source. |
Chainable
source() : \Charcoal\Source\SourceInterface
Get the object's source.
load(mixed $id = null) : \Charcoal\Source\StorableInterface
Load an object from the database from its ID.
mixed | $id | The identifier to load. |
Chainable
loadFrom(string $key = null, mixed $value = null) : \Charcoal\Source\StorableInterface
Load an object from the database from its key $key.
string | $key | Key pointing a column's name. |
mixed | $value | Value of said column. |
Chainable.
loadFromQuery(string $query, array $binds = array()) : \Charcoal\Source\StorableInterface
Load an object from the database from a custom SQL query.
string | $query | The SQL query. |
array | $binds | Optional. The SQL query parameters. |
Chainable.
setValidator(\Charcoal\Validator\ValidatorInterface $validator) : \Charcoal\Validator\ValidatableInterface
\Charcoal\Validator\ValidatorInterface | $validator | The validator object to use for validation. |
Chainable
validator() : \Charcoal\Validator\ValidatorInterface
validate(\Charcoal\Validator\ValidatorInterface $v = null) : boolean
\Charcoal\Validator\ValidatorInterface | $v | Optional. A custom validator object to use for validation. If null, use object's. |
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.
array|\Traversable | $data | The object data. |
The object data without the pre-set ID.
createMetadata() : \Charcoal\Model\MetadataInterface
createSource() : \Charcoal\Source\SourceInterface
Create the model's source, with the Source Factory.
createValidator() : \Charcoal\Validator\ValidatorInterface
metadataLoader() : \Charcoal\Model\Service\MetadataLoader
Safe MetdataLoader getter. Create the loader if it does not exist.
If the metadata loader was not set.
setSourceFactory(\Charcoal\Factory\FactoryInterface $factory) : \Charcoal\Source\StorableInterface
\Charcoal\Factory\FactoryInterface | $factory | The source factory, which is useful to create source. |
Chainable