Download the PHP package hostnet/entity-tracker-component without Composer
On this page you can find all versions of the php package hostnet/entity-tracker-component. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download hostnet/entity-tracker-component
More information about hostnet/entity-tracker-component
Files in hostnet/entity-tracker-component
Package entity-tracker-component
Short Description Provides an event when a Tracked entity changes
License MIT
Informations about the package entity-tracker-component
README
- What is the Entity Tracker?
- Requirements
- Installation
Documentation
- How does it work?
- Setup
- Registering the Events
- Creating the Listener
- Creating an Interface for the Entity
- Registering the Annotation on the Entity
- What's Next?
- Extending the Tracker Annotation
- Example Annotation
- Custom Annotation Resolvers
- Custom entityChanged Listener
What is the Entity Tracker?
The Entity Tracker Component is a library used to track changes within an Entity during a flush of the EntityManager. This makes it possible to do all sorts of things you want to automated during the preFlush
event.
Entities become tracked when you implement the @Tracked
annotation or a sub-class of @Tracked
. You have total control over what happens next and which events you will use to listen to the entityChanged
event.
Let's say that every time you flush your User, you want to set when it was updated. By default, you would have to call $user->setUpdatedAt()
manually or create a custom listener on preFlush that sets the updated at timestamp. Both are a lot of extra work and you have to write extra code to determine changes. Listening to preFlush will always trigger your listener and you don't want to make a huge if statement nor create a listener for each Entity.
Requirements
The tracker component requires at least php 5.4 and runs on Doctrine2. For specific requirements, please check composer.json
Installation
Installing is pretty easy, this package is available on packagist. You can register the package locked to a major as we follow Semantic Versioning 2.0.0.
Example
Note: You can use dev-master if you want the latest changes, but this is not recommended for production code!
Documentation
How does it work?
It works by putting an annotation on your Entity and registering your listener on our event, assuming you have already registered our event to doctrine. That's all you need to do to start tracking the Entity so it will be available in the entityChanged
event.
Setup
Registering The Events
Here's an example of a very basic setup. Setting this up will be a lot easier if you use a framework that has a Dependency Injection Container.
Note: If you use Symfony, you can take a look at the hostnet/entity-tracker-bundle. This bundle is designed to configure the services for you.
Creating the Listener
The listener needs to have 1 method that has the same name as the event name. This method will have 1 argument which is the EntityChangedEvent $event
. The event contains the used EntityManager, Current Entity, Original (old) Entity and an array of the fields which have been altered -or mutated.
Note: The Doctrine2 Event Manager uses the event name as method name, therefore you should implement the entityChanged method as listed below.
Creating an Interface for the Entity
Additionally to the @Tracked
annotation, we want to determine if we can set and updated_at field within our Entity. This can be done by creating the following interface for our Entity.
Registering the Annotation on the Entity
All we have to do now is put the @Tracked
annotation and Interface on our Entity and implement the required method
What's Next?
Change the value of a field and flush the Entity. This will trigger the preFlush, which in turn will trigger our listener, which then fires up the entityChanged event.
Extending the Tracker Annotation
You might want to extend the @Tracker
annotation. This allows you to add options and additional checks within your listener.
Example Annotation
In the following example, you will see how using a creating a custom annotation works.
- You have to add
@Annotation
- You have to add
@Target({"CLASS"})
- It has to extend
Hostnet\Component\EntityTracker\Annotation\Tracked
Using this annotation will give us specific access to options within our listener. We can now attempt to get this annotation in the listener and we get can call getIgnoredFields()
. This example will ignore certain fields for entities using the annotation.
Custom Annotation Resolvers
To obtain the Annotation, we have implemented resolvers. The example below shows how you could implement it yourself.
Custom entityChanged Listener
The listener can now use the resolver to obtain the annotation so possible do something extra when a specific set of fields is changed.
All versions of entity-tracker-component with dependencies
doctrine/annotations Version 1.*,>=1.2.0
doctrine/common Version ^2.13.3|^3.0.0
doctrine/orm Version ^2.7.5
psr/log Version ^1.0.0