Record
+ Inheritance diagram for Record:

Description

Represents an "active record" table, derived from an annotated class implementing EntityInterface.

Class Annotations:

  • @record TABLE

Property Annotations:

  • @col or @column
  • @unique or @unique <SHARED_IDENTIFIER> for a single or multi-column unique-key.
    • The shared identifier must be alphabetical, allowing underscores.
    • The identifier can be arbitrary, but it's necessary in order to associate component properties.
    • The column/s may be nullable; MySQL and SQLite don't enforce uniqueness for NULL.
  • @eav <TABLE>

Property types are preserved. Properties which are objects can be dehydrated/rehydrated if they're strictly typed. Strict typing is preferred, but annotations and finally default values are used as fallbacks.

Annotating the types String (capital "S") or STRING (all caps) results in TEXT and BLOB

Protected Properties

EAV[] $eav = []
 [property => EAV] More...
 
EntityInterface $proto
 A boilerplate instance of the class, to clone and populate. More...
 
Serializer $serializer
 
const EAV_BATCH_LOAD = 256
 The number of entities to load EAV entries for at a time, during Record::fetchEach() iteration. More...
 
- Protected Properties inherited from Table
Statement[] $_cache = []
 Prepared statement cache, keyed by function name. More...
 
Column[] $columns = []
 [name => Column] More...
 
string $name
 
- Protected Properties inherited from AbstractTable
DB $db
 

Public Static Methods

magic static self factory (DB $db, string|EntityInterface $class)
 
- Public Static Methods inherited from Table
magic static self factory (DB $db, string $name, array $columns)
 

Public Methods

 __construct (DB $db, $class)
 
EntityInterface[] fetchAll (Statement $statement)
 Fetches from a statement into clones of the entity prototype. More...
 
Generator EntityInterface[] fetchEach (Statement $statement)
 Fetches in chunks and yields each loaded entity. More...
 
Select EntityInterface[] findAll (array $match, array $eavMatch=[])
 Similar to loadAll() except this can additionally search by EAV values. More...
 
null EntityInterface findFirst (array $match, array $eavMatch=[])
 Returns an instance for the first row matching the criteria. More...
 
string getClass ()
 
EAV[] getEav ()
 
EntityInterface getProto ()
 
Serializer getSerializer ()
 
null EntityInterface load ($id)
 Loads all data for a given ID (clones the prototype), or an existing instance. More...
 
Select EntityInterface[] loadAll ()
 Returns a Select that fetches instances. More...
 
int save (EntityInterface $entity)
 Upserts record and EAV data. More...
 
$this setProto (EntityInterface $proto)
 
- Public Methods inherited from Table
 __construct (DB $db, string $name, array $columns)
 
string __toString ()
 Returns the table name. More...
 
int apply (array $values)
 INSERT IGNORE More...
 
int count (array $match=[])
 
int delete (array $match)
 Executes a deletion using arbitrary columns. More...
 
Column[] getColumns ()
 
string getName ()
 
Statement insert (array $values)
 Executes an insertion using arbitrary columns. More...
 
Column offsetGet ($column)
 
Select array[] select ($expressions=[' *'])
 Returns a selection object for columns or expressions in the table. More...
 
Table setName (string $name)
 Returns an aliased clone for joins. More...
 
int update (array $values, array $match)
 Executes an update using arbitrary columns. More...
 
- Public Methods inherited from AbstractTable
 __construct (DB $db)
 
bool offsetExists ($column)
 
void offsetSet ($offset, $value)
 Throws. More...
 
void offsetUnset ($name)
 Throws. More...
 

Protected Methods

void loadEav (array $entities)
 Loads and sets all EAV properties for an array of entities keyed by ID. More...
 
void saveEav (EntityInterface $entity)
 
void saveInsert (EntityInterface $entity)
 Inserts a new row and updates the entity's ID. More...
 
void saveUpdate (EntityInterface $entity)
 Updates the existing row for the entity. More...
 
- Protected Methods inherited from Table
Statement cache (string $key, Closure $prepare)
 Caches a prepared statement. More...
 

Properties

◆ $eav

EAV [] $eav = []
protected

[property => EAV]

◆ $proto

EntityInterface $proto
protected

A boilerplate instance of the class, to clone and populate.

◆ EAV_BATCH_LOAD

const EAV_BATCH_LOAD = 256
protected

The number of entities to load EAV entries for at a time, during Record::fetchEach() iteration.

Constructor

◆ __construct()

__construct ( DB  $db,
  $class 
)
Parameters
DB$db
string | EntityInterface$class

Methods

◆ factory()

magic static self factory ( DB  $db,
string|EntityInterface  $class 
)
static
Returns
self

◆ fetchAll()

EntityInterface [] fetchAll ( Statement  $statement)

Fetches from a statement into clones of the entity prototype.

Parameters
Statement$statement
Returns
EntityInterface[] Keyed by ID

◆ fetchEach()

Generator EntityInterface [] fetchEach ( Statement  $statement)

Fetches in chunks and yields each loaded entity.

This is preferable over fetchAll() for iterating large result sets.

Parameters
Statement$statement
Returns
Generator|EntityInterface[] Keyed by ID

◆ findAll()

Select EntityInterface [] findAll ( array  $match,
array  $eavMatch = [] 
)

Similar to loadAll() except this can additionally search by EAV values.

See also
Predicate::match()
Parameters
array$match[property => value]
array[]$eavMatch[eav property => attribute => value]
Returns
Select|EntityInterface[]

◆ findFirst()

null EntityInterface findFirst ( array  $match,
array  $eavMatch = [] 
)

Returns an instance for the first row matching the criteria.

Parameters
array$match[property => value]
array$eavMatch[eav property => attribute => value]
Returns
null|EntityInterface

◆ getClass()

string getClass ( )
final
Returns
string

◆ getEav()

EAV [] getEav ( )
Returns
EAV[]

◆ getProto()

EntityInterface getProto ( )

◆ getSerializer()

Serializer getSerializer ( )
Returns
Serializer

◆ load()

null EntityInterface load (   $id)

Loads all data for a given ID (clones the prototype), or an existing instance.

Parameters
int | EntityInterface$idThe given instance may be a subclass of the prototype.
Returns
null|EntityInterface

◆ loadAll()

Select EntityInterface [] loadAll ( )

Returns a Select that fetches instances.

Returns
Select|EntityInterface[]

◆ loadEav()

void loadEav ( array  $entities)
protected

Loads and sets all EAV properties for an array of entities keyed by ID.

Parameters
EntityInterface[]$entitiesKeyed by ID

◆ save()

int save ( EntityInterface  $entity)

Upserts record and EAV data.

Parameters
EntityInterface$entity
Returns
int ID

◆ saveEav()

void saveEav ( EntityInterface  $entity)
protected
Parameters
EntityInterface$entity

◆ saveInsert()

void saveInsert ( EntityInterface  $entity)
protected

Inserts a new row and updates the entity's ID.

Parameters
EntityInterface$entity

◆ saveUpdate()

void saveUpdate ( EntityInterface  $entity)
protected

Updates the existing row for the entity.

Parameters
EntityInterface$entity

◆ setProto()

$this setProto ( EntityInterface  $proto)
Parameters
EntityInterface$proto
Returns
$this