Documentation

FlexRepository
in package

Table of Contents

$db  : mixed
$meta  : array<string|int, mixed>
$freeze  : bool
If this is set to true, no database changes will be performed.
$instance  : FlexRepository
_clearIntermediateTable()  : bool
Internal method to clear an intermediate table in a 'HasAndBelongs' relation after updates.
_saveIntermediateTable()  : void
Internal method to save an intermediate table in a HasAndBelongs relationship
addModelFields()  : array<string|int, mixed>
Add the model fields defined in the meta that are not defined in the database table.
addNewFields()  : array<string|int, mixed>
Add new fields to the table if needed.
beginTransaction()  : void
If not frozen, start transaction
buildHaystack()  : array<string|int, mixed>
Get a full result from the database, and group them by the selected id.
commit()  : void
If not frozen, commit transaction
connect()  : bool
Connect to the database through the current driver
create()  : Flex
Helper method to create a new object If an object is created this way, all the fields will be created as TEXT by default.
delete()  : bool
Delete a model.
find()  : array<string|int, mixed>|false
Helper method to do quick finds For complex queries use PDO directly.
findOne()  : mixed
Alias for 'find' which just returns the first result and limits the query to one result.
freeze()  : void
Freeze the database, models will no longer update the schema.
frozen()  : bool
Check if the database is frozen.
get()  : FlexRepository
Get the current instance or create one.
getCollectionIds()  : array<string|int, mixed>
Get an array of ids from a Flex collection
getFieldsAndValues()  : array<string|int, mixed>
Get all the fields and their values from a Flex model
getOperations()  : array<string|int, mixed>
Get all the inserts/updates from a collection.
handleHasAndBelongsRelation()  : void
Handle a 'HasAndBelongs' relation after the parent model was saved
handleHasRelation()  : void
Handle a 'Has' relation after the parent model was saved
handlePostRelations()  : void
Handle the relations after saving the Flex model
handleRelations()  : void
Handle relations before saving the Flex model
hydrate()  : array<string|int, mixed>
Hydrate a collection.
insert()  : bool
Insert a model via DB query Table name is retrieved from the object meta.
nullifyUnusedChilds()  : bool
Set to null references to the main model in a 'Has' relation after updates.
performInserts()  : bool
Gets an array of models to insert
performUpdates()  : bool
Gets an array of updates and executes them one by one.
postSaves()  : void
Execute all the 'postSave' hooks from a model collection
prepare()  : bool
While not frozen the table will get updated just by saving records to it.
preSaves()  : array<string|int, mixed>
Trigger 'preSave' events in the collection
query()  : array<string|int, mixed>
Execute any query and get an array or a collection of models If the query has any parameters, they have to be prepared to avoid injection so name them in the PDO fashion and send an array of parameters/values.
removeUnusedChilds()  : bool
Delete orphaned records from a 'Has' relation after an update
resetInstance()  : FlexRepository
Re-create the instance
rollback()  : void
If not frozen, rollback transaction
save()  : bool
Save a model (create or update) if self::$freeze is true the 'prepare' method will try to syncronize the current schema with the database schema.
saveCollection()  : bool
Save a collection of models.
setTableForRawModels()  : void
When using Raw flex models as relations, the table is not set because it's loaded on run time. So we fetch it from the relation data.
update()  : bool
Update a model via DB query Table name is retrieved from the object meta.
updateCollection()  : bool
Update all the models from a collection
useDriver()  : void
Change the 'db' property to this value
__construct()  : mixed

Properties

$freeze

If this is set to true, no database changes will be performed.

protected static bool $freeze = false

Methods

_clearIntermediateTable()

Internal method to clear an intermediate table in a 'HasAndBelongs' relation after updates.

public _clearIntermediateTable(Flex $model, Relation $relation) : bool
Parameters
$model : Flex
$relation : Relation
Return values
bool

_saveIntermediateTable()

Internal method to save an intermediate table in a HasAndBelongs relationship

public _saveIntermediateTable(Flex $model, mixed $relation, mixed $key) : void
Parameters
$model : Flex

original model

$relation : mixed

relation data array

$key : mixed

key in the instances collection to use

Return values
void

addModelFields()

Add the model fields defined in the meta that are not defined in the database table.

public addModelFields(Flex $model, array<string|int, mixed> $tableFields[, array<string|int, mixed> $fields = [] ]) : array<string|int, mixed>
Parameters
$model : Flex
$tableFields : array<string|int, mixed>
$fields : array<string|int, mixed> = []
Return values
array<string|int, mixed>

addNewFields()

Add new fields to the table if needed.

public addNewFields(Flex $model, mixed $tableFields[, array<string|int, mixed> $fields = [] ]) : array<string|int, mixed>
Parameters
$model : Flex
$tableFields : mixed
$fields : array<string|int, mixed> = []
Return values
array<string|int, mixed>

beginTransaction()

If not frozen, start transaction

public beginTransaction() : void
Return values
void

buildHaystack()

Get a full result from the database, and group them by the selected id.

public buildHaystack(array<string|int, mixed> $result, string $table, mixed $id) : array<string|int, mixed>
Parameters
$result : array<string|int, mixed>
$table : string
$id : mixed
Return values
array<string|int, mixed>

commit()

If not frozen, commit transaction

public commit() : void
Return values
void

connect()

Connect to the database through the current driver

public connect(array<string|int, mixed> $params) : bool
Parameters
$params : array<string|int, mixed>
Tags
throws
Exception
Return values
bool

create()

Helper method to create a new object If an object is created this way, all the fields will be created as TEXT by default.

public create(string $table) : Flex
Parameters
$table : string
Return values
Flex

delete()

Delete a model.

public delete(Flex $model) : bool

Table name is retrieved from the object meta.

Triggers 'preDelete' and 'postDelete' events.

Parameters
$model : Flex
Return values
bool

find()

Helper method to do quick finds For complex queries use PDO directly.

public find(string $table[, string $condition = '' ][, array<string|int, mixed> $params = [] ][, array<string|int, mixed> $options = [] ]) : array<string|int, mixed>|false
Parameters
$table : string
$condition : string = ''
$params : array<string|int, mixed> = []
$options : array<string|int, mixed> = []
Return values
array<string|int, mixed>|false

findOne()

Alias for 'find' which just returns the first result and limits the query to one result.

public findOne(mixed $table[, string $condition = '' ][, array<string|int, mixed> $params = [] ][, array<string|int, mixed> $options = [] ]) : mixed
Parameters
$table : mixed
$condition : string = ''
$params : array<string|int, mixed> = []
$options : array<string|int, mixed> = []
Return values
mixed

freeze()

Freeze the database, models will no longer update the schema.

public static freeze() : void

Recommended for production environments.

Return values
void

frozen()

Check if the database is frozen.

public static frozen() : bool
Return values
bool

getCollectionIds()

Get an array of ids from a Flex collection

public getCollectionIds(array<string|int, Flex$collection) : array<string|int, mixed>
Parameters
$collection : array<string|int, Flex>
Return values
array<string|int, mixed>

getFieldsAndValues()

Get all the fields and their values from a Flex model

public getFieldsAndValues(Flex $model) : array<string|int, mixed>
Parameters
$model : Flex
Return values
array<string|int, mixed>

getOperations()

Get all the inserts/updates from a collection.

public getOperations(array<string|int, Flex$collection) : array<string|int, mixed>
Parameters
$collection : array<string|int, Flex>
Return values
array<string|int, mixed>

handleHasAndBelongsRelation()

Handle a 'HasAndBelongs' relation after the parent model was saved

public handleHasAndBelongsRelation(Relation $relation, Flex $model) : void
Parameters
$relation : Relation
$model : Flex
Return values
void

handleHasRelation()

Handle a 'Has' relation after the parent model was saved

public handleHasRelation(Relation $relation, Flex $model) : void
Parameters
$relation : Relation
$model : Flex
Return values
void

handlePostRelations()

Handle the relations after saving the Flex model

public handlePostRelations(Flex $model) : void
Parameters
$model : Flex
Return values
void

handleRelations()

Handle relations before saving the Flex model

public handleRelations(Flex $model) : void
Parameters
$model : Flex
Return values
void

hydrate()

Hydrate a collection.

public hydrate(array<string|int, mixed> $result, mixed $table[, string $class = 'Makiavelo\Flex\Flex' ]) : array<string|int, mixed>

By default all the results are Hydrated to a collection of Flex but the class to use can be modified.

Parameters
$result : array<string|int, mixed>
$table : mixed
$class : string = 'Makiavelo\Flex\Flex'
Return values
array<string|int, mixed>

insert()

Insert a model via DB query Table name is retrieved from the object meta.

public insert(Flex $model) : bool

Fields and values are retrieved from the object.

Parameters
$model : Flex
Return values
bool

nullifyUnusedChilds()

Set to null references to the main model in a 'Has' relation after updates.

public nullifyUnusedChilds(Flex $model, Relation $relation) : bool
Parameters
$model : Flex
$relation : Relation
Return values
bool

performInserts()

Gets an array of models to insert

public performInserts(array<string|int, Flex$inserts) : bool

Triggers all the 'preSave' and 'postSave' events.

All the models are inserted in a single query (transactional)

Parameters
$inserts : array<string|int, Flex>
Return values
bool

performUpdates()

Gets an array of updates and executes them one by one.

public performUpdates(array<string|int, Flex$updates) : bool
Parameters
$updates : array<string|int, Flex>
Return values
bool

postSaves()

Execute all the 'postSave' hooks from a model collection

public postSaves(array<string|int, mixed> $elems) : void
Parameters
$elems : array<string|int, mixed>
Return values
void

prepare()

While not frozen the table will get updated just by saving records to it.

public prepare(Flex $model) : bool

The Flex instance may have a meta definition for fields, which will be applied to the table. If no meta was defined for fields, non-existant fields will be created as TEXT fields in the table.

Parameters
$model : Flex
Return values
bool

preSaves()

Trigger 'preSave' events in the collection

public preSaves(array<string|int, Flex$elems) : array<string|int, mixed>
Parameters
$elems : array<string|int, Flex>
Return values
array<string|int, mixed>

query()

Execute any query and get an array or a collection of models If the query has any parameters, they have to be prepared to avoid injection so name them in the PDO fashion and send an array of parameters/values.

public query(mixed $query[, mixed $params = [] ][, array<string|int, mixed> $options = [] ]) : array<string|int, mixed>

Eg: $query = 'SELECT * FROM user WHERE id = :id'; $params = [':id' => $id];

In the $options parameter define if you want hydration, which is enabled by default to return a collection of Flex models.

In the case of Flex models, if the table name option is not defined, then the models can't be updated later on (unless the table meta is set manually).

Parameters
$query : mixed
$params : mixed = []
$options : array<string|int, mixed> = []
Return values
array<string|int, mixed>

removeUnusedChilds()

Delete orphaned records from a 'Has' relation after an update

public removeUnusedChilds(Flex $model, Relation $relation) : bool
Parameters
$model : Flex
$relation : Relation
Return values
bool

rollback()

If not frozen, rollback transaction

public rollback() : void
Return values
void

save()

Save a model (create or update) if self::$freeze is true the 'prepare' method will try to syncronize the current schema with the database schema.

public save(Flex $model) : bool

preSave and postSave hooks are triggered.

Parameters
$model : Flex
Return values
bool

saveCollection()

Save a collection of models.

public saveCollection(array<string|int, Flex$collection) : bool

All the models must be of the same type. There can be a mix of new and not-new objects insert and update operations will be performed separately.

The operation is atomic, transactions are used.

The db schema is synced.

Parameters
$collection : array<string|int, Flex>
Return values
bool

setTableForRawModels()

When using Raw flex models as relations, the table is not set because it's loaded on run time. So we fetch it from the relation data.

public setTableForRawModels(Relation $relation, mixed $key) : void
Parameters
$relation : Relation
$key : mixed

The instance key in the array

Return values
void

update()

Update a model via DB query Table name is retrieved from the object meta.

public update(Flex $model) : bool

Fields and values are retrieved from the object.

Parameters
$model : Flex
Return values
bool

updateCollection()

Update all the models from a collection

public updateCollection(array<string|int, Flex$updates) : bool
Parameters
$updates : array<string|int, Flex>
Return values
bool

useDriver()

Change the 'db' property to this value

public useDriver(mixed $driver) : void
Parameters
$driver : mixed
Return values
void

__construct()

private __construct([mixed $params = [] ]) : mixed
Parameters
$params : mixed = []
Return values
mixed

Search results