abstract class Schema (View source)

A schema for a database table.

Methods

__construct(ContainerInterface $container)

Schema constructor.

string
getModel()

Returns the model class used to operate on the records from this schema.

string
getTable()

Returns the name of the table for the schema.

array
getPrimaryKey()

Returns a list fields that define the primary key for the schema.

array
getFields()

Returns the list of fields for the schema.

array
getRelationshipDefinitions()

Returns an associative list of relationship definitions for the schema.

array
getRelationships()

Returns a list of all relationships for the schema.

getRelationship(string $name)

Returns a single relationship with the given name.

createRecord(Model $model = null)

Returns a new empty record for the schema.

createRecordFromValues(array $values)

Returns a new record with the given values for the fields.

createRecordFromRow(array $row, string $prefix = '')

Returns a new record with values taken from a result row with optional prefix for fields.

createModel(Record $record)

Returns a new model that is associated to the given record.

createModelFromRow(array $row, string $prefix = '', array $relationships = [])

Returns a new model with a new record that has values from the given result row with optional relationships.

Details

__construct(ContainerInterface $container)

Schema constructor.

Parameters

ContainerInterface $container Container used to load schemas for relationships

abstract string getModel()

Returns the model class used to operate on the records from this schema.

Return Value

string The model class used to operate on the records from this schema

abstract string getTable()

Returns the name of the table for the schema.

Return Value

string Name of the table for the schema

abstract array getPrimaryKey()

Returns a list fields that define the primary key for the schema.

Return Value

array List fields that define the primary key for the schema

abstract array getFields()

Returns the list of fields for the schema.

Return Value

array List of fields for the schema

abstract array getRelationshipDefinitions()

Returns an associative list of relationship definitions for the schema.

The returned array is an associative array of definitions, where each key defines the name of the relationship.

Each definition must contain the following fields: - key : Defines the field (or list of fields) in the schema that references fields in the referenced schema - schema : Name of the schema in the container that the the relationship references - field : Defines the field (or list of fields) in the referenced schema - unique : If relationship can ever only refer to a single record (optional)

If the unique value is missing or even if it's defined as false, the relationship will still be considered unique if the referenced fields contain the entire primary key of the referenced schema (as it is not possible to have multiple records with the same primary key).

Note that each relationship must have an appropriate reverse relationship defined in the referenced schema.

Return Value

array Associative list of relationship definitions for the schema

array getRelationships()

Returns a list of all relationships for the schema.

Return Value

array List of all relationships for the schema

Relationship getRelationship(string $name)

Returns a single relationship with the given name.

Parameters

string $name Name of the relationship

Return Value

Relationship Relationship with the given name

Exceptions

InvalidRelationshipException If a relationship with the given name does not exist

Record createRecord(Model $model = null)

Returns a new empty record for the schema.

Parameters

Model $model A model to associate to the record, if initialized

Return Value

Record A new empty record based on this schema

Record createRecordFromValues(array $values)

Returns a new record with the given values for the fields.

Parameters

array $values Values for the record fields

Return Value

Record New record with given values

Record createRecordFromRow(array $row, string $prefix = '')

Returns a new record with values taken from a result row with optional prefix for fields.

Parameters

array $row The result row from a database query
string $prefix Prefix for the fields used by this schema

Return Value

Record New record based on the fields taken from the result row

Model createModel(Record $record)

Returns a new model that is associated to the given record.

Parameters

Record $record The record associated to the model

Return Value

Model New model based on the given record

Model createModelFromRow(array $row, string $prefix = '', array $relationships = [])

Returns a new model with a new record that has values from the given result row with optional relationships.

Parameters

array $row Result row from the database
string $prefix Prefix for the fields of this schema
array $relationships Names of unique relationships also provided in the result row

Return Value

Model New model based on the values provided in the result row