Entity Behaviors
The Entity Behavior package is a collection of attributes that adds behaviors to entities.
The package provides functionality that hooks in to the lifecycle events of entities. Those events are create, update, and delete.
Note that embedded entities are not supported by the Entity Behavior package.
UUID#
The UUID behavior adds a UUID field to an entity. It for this it uses the ramsey/uuid library. This is already a dependency of Swift, so there's no need to install it.
Usage#
The package provides several implementations of the UUID behavior. The most basic form in the one below.
RFC 4122 UUIDs#
Version 1: Time-based#
A version 1 UUID uses the current time, along with the MAC address (or node) for a network interface on the local machine.
Version 2: DCE Security#
UUID v2 uses the current time, along with the MAC address (or node) for a network interface on the local machine. Additionally, a version 2 UUID replaces the low part of the time field with a local identifier such as the user ID or group ID of the local account that created the UUID.
Version 3: Name-based (MD5)#
Uses a version 3 (name-based) UUID based on the MD5 hash of a namespace ID and a name.
Version 4: Random#
They are randomly-generated and do not contain any information about the time they are created or the machine that generated them.
Version 5: Name-based (SHA-1)#
Uses a version 5 (name-based) UUID based on the SHA-1 hash of a namespace ID and a name.
Version 6: Nonstandard UUIDs#
If you want to use a custom
uuiddeclaration, you have to extend theSwift\Orm\Attributes\Behavior\Uuid\Uuidclass.
Auto timestamps#
The behaviour that is likely most used in the Auto Timestamps. This pattern adds created_at and updated_at fields to your entity. This behavior automatically saves datetime values to createdAt and updateAt fields.
Soft delete (DeletedAT)#
Adds a deleted_at column which defines if a record has been marked as deleted (and if so, when). Useful when designing a highly complicated system where data consistency is important and even if some data should be invisible in the backend, it should still remain in the database.
Optimistic lock#
The package adds support for automatic optimistic locking via a version field. In this approach any entity that should be protected against concurrent modifications during long-running business transactions gets a version field. When changes to such an entity are persisted at the end of a long-running conversation the version of the entity is compared to the version in the database and if they don't match, an Swift\Orm\Behavior\Exception\OptimisticLock\RecordIsLockedException is thrown,
indicating that the entity has been modified by someone else already.
Available strategies#
- MICROTIME- Current timestamp with microseconds as string.
- RAND_STR- Random generated string (random_bytes).
- INCREMENT- Auto incremented integer version.
- DATETIME- Current datetime.
- MANUAL- Allows using custom realisation for setting and controlling version. You have to manage entity and DB column by yourself.
Usage#
Note: Your field declaration should be compatible with the strategy you use.
Hooks#
Events serve as a great way to decouple various aspects of your application, since a single event can have multiple listeners that do not depend on each other.
This package provides a simple observer pattern implementation, allowing you to subscribe and listen for various events via closure based event listeners.
This is basically a simple version on LifeCycles
Usage#
To start listening to entity events, add a Swift\Orm\Attributes\Behavior\Hook attribute to your Entity.
Event listener#
Events serve as a great way to decouple various aspects of your application, since a single event can have multiple listeners that do not depend on each other.
This package provides a simple observer pattern implementation, allowing you to subscribe and listen for various events.
Usage#
If you are listening for many events on a given entity, you may use Swift\Orm\Attributes\Behavior\EventListener attribute to group all of your listeners into a single class. Event listener classes should have attributes which subscribe to the Entity events you wish to listen for. Each of these methods receives the event object as their only argument.
Entity event listener#
Listener class#
Next, let's take a look at the listener for our example Comment entity. Event listeners receive event instances in their methods that subscribed to the events.
Extensions#
Todo
Events#
The package dispatch several events, allowing you to hook into the following moments in an entity's lifecycle:
An event object contains the following public properties:
OnCreate#
The event will dispatch before an entity is stored to the database.
OnUpdate#
The event will dispatch before an entity is updated in the database.
OnDelete#
The event will dispatch before an entity is deleted from the database.