MetaFieldsTrait
Working with create/read/update/delete meta fields tables.
To use meta fields trait, set these properties and method properly in class constructor.
$this->storagePath = STORAGE_PATH . '/cache/Modules/MyModule/Models/my_table_fields'; $this->tableName = mytable'; $this->objectIdName = 'mytable_object_id'; $this->beginMetaFieldsTrait($Container);
And then it will be able to call getFields()
, addFieldsData()
, updateFieldsData()
, deleteFieldsData()
methods.
Table of Contents
Properties
- $builtCacheContent : array<string|int, mixed>|null
- $getFieldsNoData : bool
- $objectId : int
- $objectIdName : string
- $tableName : string
Methods
- buildCacheContent() : string
- Get DB result and build cache content.
- addFieldsData() : mixed
- Add a meta field data to meta `_fields` table.
- beginMetaFieldsTrait() : mixed
- Trait initialize method.
- deleteAllFieldsData() : bool
- Delete all fields for specific object ID.
- deleteFieldsData() : bool
- Delete a meta field data from meta `_fields` table.
- getFields() : mixed
- Get meta field(s) data by conditions.
- getFieldsNoCache() : mixed
- Get meta field(s) data by conditions but no cache.
- getFieldValueReformat() : mixed
- Get field value that will be re-formatted for insert, update, read.
- getStorageFileName() : string
- Get storage file name with .php extension.
- listObjectsFields() : array<string|int, mixed>
- List multiple objects and their fields.
- updateFieldsData() : mixed
- Update a meta field data to meta `_fields` table.
- updateFieldsMultipleData() : bool
- Update `_fields` table on multiple values.
- bindValuesForUpdateFieldsMultipleData() : mixed
- Call to `bindValue()` from `$Sth` argument.
- buildPlaceholdersAndBindValuesDescForUpdateFieldsMultipleData() : mixed
- Build/alter the variables for description column for use in `updateFieldsMultipleData()` method.
- buildPlaceholdersAndBindValuesForUpdateFieldsMultipleData() : mixed
- Build/alter the variables for use in `updateFieldsMultipleData()` method.
Properties
$builtCacheContent
protected
array<string|int, mixed>|null
$builtCacheContent
The temporary result where the query was called to selected object ID. The result of DB queried will be array wether it is empty or not.
This property's value was set in listObjectsFields()
method.
This will be reset to null
once called buildCacheContent()
method and found that this is not null
.
Tags
$getFieldsNoData
protected
bool
$getFieldsNoData
= false
Indicate that getFields()
and getFieldsNoCache()
methods contain values or not. The result will be true
if no value or no data, but will be false
if there is at least a value or data.
Tags
$objectId
protected
int
$objectId
The object_id.
$objectIdName
protected
string
$objectIdName
The field name of object_id.
$tableName
protected
string
$tableName
Table fields name.
Methods
buildCacheContent()
Get DB result and build cache content.
public
buildCacheContent() : string
This method must be public to be able to called from other method/class.
Return values
string —Return generated data in php language that is ready to use as cache.
addFieldsData()
Add a meta field data to meta `_fields` table.
protected
addFieldsData(int $objectId, string $field_name, mixed $field_value[, string $field_description = false ]) : mixed
This method is not recommended to call it directly, please call to updateFieldsData()
method instead and if the data is not exists, it will be call this method automatically.
Parameters
- $objectId : int
-
Object ID.
- $field_name : string
-
Field name.
- $field_value : mixed
-
Field value. If it is no scalar then it will be serialize automatically.
- $field_description : string = false
-
Field description.
Return values
mixed —Return insert ID on success, or false
if failure.
beginMetaFieldsTrait()
Trait initialize method.
protected
beginMetaFieldsTrait(Container $Container) : mixed
This method must be called before it can be working.
Parameters
- $Container : Container
-
The DI container class.
deleteAllFieldsData()
Delete all fields for specific object ID.
protected
deleteAllFieldsData(int $objectId) : bool
Also delete cached data.
Parameters
- $objectId : int
-
Object ID.
Return values
bool —Return true
on success, false
for otherwise.
deleteFieldsData()
Delete a meta field data from meta `_fields` table.
protected
deleteFieldsData(int $objectId, string $field_name) : bool
Also delete cached data.
Parameters
- $objectId : int
-
Object ID.
- $field_name : string
-
Field name.
Return values
bool —Return true
on success, false
for otherwise.
getFields()
Get meta field(s) data by conditions.
protected
getFields(int $objectId[, string $field_name = '' ]) : mixed
Parameters
- $objectId : int
-
The object ID.
- $field_name : string = ''
-
The field name to search in. If this is empty then it will return all fields.
Return values
mixed —Return a single row of field or all rows depend on field name to search. If it was not found then return null.
The return value may be unserialize if it is not scalar and not null
.
You can call to property getFieldsNoData
(boolean) to check that are there any data or value from this method.
getFieldsNoCache()
Get meta field(s) data by conditions but no cache.
protected
getFieldsNoCache(int $objectId[, string $field_name = '' ]) : mixed
This method work the same as getFields()
method but connect to DB without cache to make very sure that data is really exists.
Parameters
- $objectId : int
-
The object ID.
- $field_name : string = ''
-
The field name to search in. If this is empty then it will return all fields.
Tags
Return values
mixed —Return a single row of field or all rows depend on field name to search. If it was not found then return null.
The return value may be unserialize if it is not scalar and not null
.
You can call to property getFieldsNoData
(boolean) to check that are there any data or value from this method.
getFieldValueReformat()
Get field value that will be re-formatted for insert, update, read.
protected
getFieldValueReformat(mixed $field_value[, srting $getFor = 'update' ]) : mixed
Parameters
- $field_value : mixed
-
The field value data that will be re-formatted.
- $getFor : srting = 'update'
-
Get field value that will be re-formatted for. Accepted value: 'insert', 'update', 'read'.
The 'insert' and 'update' value will use the same algorithm.
Tags
Return values
mixed —If 'insert', or 'update' value then the field value type scalar or null
will be return as is. Otherwise it will be serialize.
If 'read' value then it will be un-serialize the field value.
getStorageFileName()
Get storage file name with .php extension.
protected
getStorageFileName(int $objectId) : string
Parameters
- $objectId : int
-
The object ID.
Tags
Return values
string —Return storage file name. Example: object-id-311-user_fields.php
listObjectsFields()
List multiple objects and their fields.
protected
listObjectsFields(array<string|int, mixed> $objectIds[, string $field_name = '' ]) : array<string|int, mixed>
Parameters
- $objectIds : array<string|int, mixed>
-
The object IDs to search in.
- $field_name : string = ''
-
The field name to search in. If this is empty then it will return all.
Tags
Return values
array<string|int, mixed> —Return associative array where key is each object ID (int) in the $objectIds
and its result will be the same as we get from getFields()
method with $field_name
parameter.
updateFieldsData()
Update a meta field data to meta `_fields` table.
protected
updateFieldsData(int $objectId, string $field_name, mixed $field_value[, string|false $field_description = false ][, mixed $previousValue = false ]) : mixed
This will be add if the data is not exists.
Parameters
- $objectId : int
-
Object ID.
- $field_name : string
-
Field name.
- $field_value : mixed
-
Field value. If field value is not scalar then it will be serialize automatically.
- $field_description : string|false = false
-
Field description. Set to
false
to not change. - $previousValue : mixed = false
-
Previous field value to check that it must be matched, otherwise it will not be update and return
false
. Set this tofalse
to skip checking.
Return values
mixed —Return meta field ID if it use add method, return true
if update success, false
for otherwise.
updateFieldsMultipleData()
Update `_fields` table on multiple values.
protected
updateFieldsMultipleData(int $objectId, array<string|int, mixed> $data[, array<string|int, mixed> $dataDesc = [] ]) : bool
It will be update if data exists, or it will be insert if data is not exists.
Parameters
- $objectId : int
-
The object ID.
- $data : array<string|int, mixed>
-
Associative array where key is match
field_name
column and value is matchfield_value
column. - $dataDesc : array<string|int, mixed> = []
-
Associative array of field description where array key is the
field_name
column and its value is matchfield_description
column.
Tags
Return values
bool —Return true
if all data have been updated, false
for otherwise.
bindValuesForUpdateFieldsMultipleData()
Call to `bindValue()` from `$Sth` argument.
private
bindValuesForUpdateFieldsMultipleData(PDOStatement $Sth, array<string|int, mixed> $bindValues) : mixed
This is for work with updateFieldsMultipleData()
method.
Parameters
- $Sth : PDOStatement
- $bindValues : array<string|int, mixed>
-
The array of bind values where key is placeholder and value is its value.
Tags
buildPlaceholdersAndBindValuesDescForUpdateFieldsMultipleData()
Build/alter the variables for description column for use in `updateFieldsMultipleData()` method.
private
buildPlaceholdersAndBindValuesDescForUpdateFieldsMultipleData(array<string|int, mixed> $dataDesc, array<string|int, mixed> $fieldNamesToCheck, array<string|int, mixed> &$descPlaceholders, array<string|int, mixed> &$descBindValues) : mixed
Parameters
- $dataDesc : array<string|int, mixed>
-
The input data description.
- $fieldNamesToCheck : array<string|int, mixed>
-
The list of field names to check input name (field name) that must be matched. For example field for check with exists (will update), or not exists (will insert).
- $descPlaceholders : array<string|int, mixed>
-
The field description placeholders to be altered.
- $descBindValues : array<string|int, mixed>
-
The field description for use with bind values to be altered.
Tags
buildPlaceholdersAndBindValuesForUpdateFieldsMultipleData()
Build/alter the variables for use in `updateFieldsMultipleData()` method.
private
buildPlaceholdersAndBindValuesForUpdateFieldsMultipleData(array<string|int, mixed> $data, array<string|int, mixed> $fieldNamesToCheck, array<string|int, mixed> &$namePlaceholders, array<string|int, mixed> &$nameBindValues, array<string|int, mixed> &$valuePlaceholders, array<string|int, mixed> &$valueBindValues) : mixed
Parameters
- $data : array<string|int, mixed>
-
The input data.
- $fieldNamesToCheck : array<string|int, mixed>
-
The list of field names to check input name (field name) that must be matched. For example field for check with exists (will update), or not exists (will insert).
- $namePlaceholders : array<string|int, mixed>
-
The field name placeholders to be altered.
- $nameBindValues : array<string|int, mixed>
-
The field name for use with bind values to be altered.
- $valuePlaceholders : array<string|int, mixed>
-
The field value placeholders to be altered.
- $valueBindValues : array<string|int, mixed>
-
The field value for use with bind values to be altered.