ManagesTimestamps
Trait for managing entity timestamps in repositories
Table of Contents
Methods
- createEntity() : T
- Prepare entity for creation by setting timestamps, saving, and refreshing
- ensureTimestamps() : void
- Set created_at and updated_at timestamps if not already set
- saveAndRefresh() : T
- Save entity and return the refreshed version from database
Methods
createEntity()
Prepare entity for creation by setting timestamps, saving, and refreshing
protected
createEntity(T $entity, callable $finder, string $entityType) : T
This combines the common pattern of:
- Setting timestamps if not already set
- Saving the entity
- Re-fetching from database to get all DB-generated values
Use this in create() methods after performing duplicate checks.
Parameters
- $entity : T
-
Entity to create
- $finder : callable
-
Callback to find entity by ID: function(int $id): ?T
- $entityType : string
-
Human-readable entity type for error messages
Tags
Return values
T —The refreshed entity from database
ensureTimestamps()
Set created_at and updated_at timestamps if not already set
protected
ensureTimestamps(object $entity) : void
Parameters
- $entity : object
-
Entity with getCreatedAt/setCreatedAt and getUpdatedAt/setUpdatedAt methods
saveAndRefresh()
Save entity and return the refreshed version from database
protected
saveAndRefresh(T $entity, callable $finder, string $entityType) : T
Ensures the returned entity has all database-generated values. Throws an exception if the entity cannot be found after save.
Parameters
- $entity : T
-
Entity to save
- $finder : callable
-
Callback to find entity by ID: function(int $id): ?T
- $entityType : string
-
Human-readable entity type for error messages
Tags
Return values
T —The refreshed entity from database