Skip to content

Attributes

PHP Attributes are used to describe model and it's fields.

All attributes are in Blrf\Orm\Model\Attribute namespace.

#[Model]

Required attribute to mark a PHP class as a model. It has no arguments.

Note

You may ommit this attribute and only use #[Source] attribute.

#[Source]

This attributes describes the database table for the model. It is class level attribute and is optional. If not specified, Naming strategy will be used to detect table name from class name.

Example
<?php
use Blrf\Orm\Model\Attribute as Attr;
use Blrf\Orm\Model;

#[Attr\Source(name: 'table', schema: 'schema')]
class MyModel extends Model
{
}

#[DerivedModel]

This attribute marks a class as Derived model. Derived model is used when base model defines static ormHydrateModel() method and returns derived model (model from which derived model extends). It inherits all attributes of parent model.

Note

Derived model cannot define additional attributes or change them.

Example
<?php
use Blrf\Orm\Model\Attribute as Attr;
use Blrf\Orm\Model;

#[Attr\DerivedModel(BaseModel::class)]
class MyModel extends BaseModel
{
}

Example

See ShippingModel and it's derived ShippingMethod\Standard models in Bookstore example.

#[Index]

This attribute is used to describe database table indexes. It tells ORM which fields in model are indexed, unique, etc so methods like findByPk() may be used.

Parameter Type Required Values Description
type string PRIMARY, UNIQUE, KEY Type of index
fields array Array of fields
name string string If not provided it will be generated
Example
<?php
use Blrf\Orm\Model\Attribute as Attr;
use Blrf\Orm\Model;

#[Attr\Index(type: 'PRIMARY', fields: ['id'])]
#[Attr\Index(type: 'UNIQUE', fields: ['name'])]
#[Attr\Index(type: 'KEY', fields: ['lastname'])]
class MyModel extends Model
{
}

Question

Later this will be used by schema manager: issue#1

#[Field]

This attribute marks class property as field. Fields represent a map between database columns and model properties.

Parameter Type Required Values Description
name string string Property name. If not provided it will be detected
type string|array Types Defines data type and properties for database table column. Deteceted if not provided
column string string Database table column name. If not provided name will be used
attributes array Attributes Additional field attributes

Types

Field type are used to describe field data type. If ommited in #[Field] attribute, it will attempt to detect it with some reasonable defaults.

int

TBD;

float

TBD;

decimal

TBD;

string

TBD;

datetime

TBD;

date

TBD;

TBD;

#[GeneratedValue]

#[AutoIncrement]

#[Relation]