CheckAddTrait
Common methods checking if a record exists, and adds the object if it doesn't.
Tags
Table of Contents
Properties
- $cacheReadCnt : int
- track successful cache read counts.
- $checkAdd_new_cnt : int
- Track the new count for each query
- $checkAdd_update_cnt : int
- Track the updated count for each query
- $checkAddIds : array<string, array<string|int, mixed>>
- Keeps track of the ids that have been created/checked previously.
- $dbReadCnt : int
- track successful database read counts.
- $dbWriteCnt : int
- track successful database write counts.
- $useCaching : bool
- If we should use memory caching, always reach out to the database.
- $checkAddName : string
- The default field name for the name field
- $checkAddSlug : string
- The default field name for the slug field
- $lastEntity : Entity|null
- Hold the last record checked/created
Methods
- checkAdd() : EntityInterface|int
- Checks if a record exist by it's slug, if not, it creates the record
- fixNameCommon() : string
- Common manipulation of an entity's name
- getCheckAdd() : Entity|null
- Checks the checkadd cache and returns the id, or false
- getLastEntity() : null|Entity
- Returns the last entity added to the cache
- getNameField() : string
- Gets the nameField()
- getSlugField() : string
- Gets the slugField()
- getUseCache() : bool
- Gets if we're using memory caching.
- initCheckAdd() : void
- Initialize method
- setCheckAdd() : Entity
- Adds the id to the checkadd cache
- setLastEntity() : void
- Sets tracking on the last entity that was added to the cache
- setNameField() : void
- Sets the name field, if different from the default
- setSlugField() : void
- Sets the slug field, if different from the default
- setUseCache() : void
- Sets if we're using memory caching.
- slugify() : string
- Creates a slug from the input
Properties
$cacheReadCnt
track successful cache read counts.
public
int
$cacheReadCnt
= 0
$checkAdd_new_cnt
Track the new count for each query
public
int
$checkAdd_new_cnt
= 0
$checkAdd_update_cnt
Track the updated count for each query
public
int
$checkAdd_update_cnt
= 0
$checkAddIds
Keeps track of the ids that have been created/checked previously.
public
array<string, array<string|int, mixed>>
$checkAddIds
= []
$dbReadCnt
track successful database read counts.
public
int
$dbReadCnt
= 0
$dbWriteCnt
track successful database write counts.
public
int
$dbWriteCnt
= 0
$useCaching
If we should use memory caching, always reach out to the database.
public
bool
$useCaching
= true
In benchmarking, it seems faster right now to disable memory caching.
$checkAddName
The default field name for the name field
protected
string
$checkAddName
= 'name'
$checkAddSlug
The default field name for the slug field
protected
string
$checkAddSlug
= 'slug'
$lastEntity
Hold the last record checked/created
protected
Entity|null
$lastEntity
= null
Methods
checkAdd()
Checks if a record exist by it's slug, if not, it creates the record
public
checkAdd([mixed $name = null ][, null|string $slug = null ][, array<string, mixed> $fields = [] ][, bool|array<string|int, mixed> $returnEntity = false ]) : EntityInterface|int
Parameters
- $name : mixed = null
-
The name of the record
- $slug : null|string = null
-
The unique slug to look for
- $fields : array<string, mixed> = []
-
The list of other details to include in the new record
- $returnEntity : bool|array<string|int, mixed> = false
-
If true, return the entity, otherwise return the entity id. If it's an array, treat it as options
Tags
Return values
EntityInterface|int —Either the existing entity, or the newly created entity's id.
fixNameCommon()
Common manipulation of an entity's name
public
fixNameCommon(string $name) : string
Parameters
- $name : string
-
The name to fix/normalize
Return values
string —The fixed name.
getCheckAdd()
Checks the checkadd cache and returns the id, or false
public
getCheckAdd(string $slug[, null|string $alias = null ]) : Entity|null
Parameters
- $slug : string
-
The slug to look up
- $alias : null|string = null
-
The model alias so look up.
Return values
Entity|null —This id if found, or false if not
getLastEntity()
Returns the last entity added to the cache
public
getLastEntity() : null|Entity
Return values
null|Entity —The last entity added to the cache
getNameField()
Gets the nameField()
public
getNameField() : string
Return values
string —The field name for name
getSlugField()
Gets the slugField()
public
getSlugField() : string
Return values
string —The field name for slug
getUseCache()
Gets if we're using memory caching.
public
getUseCache() : bool
Return values
bool —If we're using caching.
initCheckAdd()
Initialize method
public
initCheckAdd([array<string, mixed> $config = [] ]) : void
Parameters
- $config : array<string, mixed> = []
-
The configuration for the Table.
setCheckAdd()
Adds the id to the checkadd cache
public
setCheckAdd(string $slug, Entity $entity[, null|string $alias = null ]) : Entity
Parameters
- $slug : string
-
The slug to look up
- $entity : Entity
-
The object record that was added
- $alias : null|string = null
-
The model alias so look up.
Return values
EntitysetLastEntity()
Sets tracking on the last entity that was added to the cache
public
setLastEntity(Entity $entity) : void
Parameters
- $entity : Entity
-
The last entity added to the cache
setNameField()
Sets the name field, if different from the default
public
setNameField(string $field) : void
Parameters
- $field : string
-
The field name to have the name set to
setSlugField()
Sets the slug field, if different from the default
public
setSlugField(string $field) : void
Parameters
- $field : string
-
The field name to have the slug set to
setUseCache()
Sets if we're using memory caching.
public
setUseCache([bool $toCache = false ]) : void
Parameters
- $toCache : bool = false
-
Set whether or not to use memory caching.
slugify()
Creates a slug from the input
public
slugify([mixed $input = null ]) : string
Parameters
- $input : mixed = null
-
The input to create the slug from
Tags
Return values
string —the slugged string