This is add-on to Doctrine2 the implements EntityManager::flush()
logging and history. It can serve as a replacement to both EntityAudit and Atlantic18 Loggable / Blameable extensions.
Because relations. Most of the libraries log single entity changes, but a change in ProductTranslation
is actually a change in Product
from application perspective. Collection updates also matter (who attached that image to this product?).
Therefore, this library logs flushes, consisting of many changes (insertions, removals, deletions, AND collection changes). Also, it tries to be a low-level library, that does not create new entities on each flush, manipulating DBAL directly.
Basically, this library serializes UnitOfWork
into JSON and relies on DB native functions to browse and search the logs. This seems like as the only way to log relation changes (who added tag "Cancelled" to order #216?), and provides a bit of flexibility.
Stability: in development. Please join!
Database support: MySQL 5.6+
ORM: 2.5+
PHP: 7.1+
- filtering entities
- filtering entity fields (entity is tracked only if one of the fields is updated)
- collection change tracking
- "Affected entities" concept.
ProductTranslation
change affectsProduct
change, event if theProduct
entity wasn't changed at all.
JSON search is very slow. Can you add indexes?
No and this is a bad approach. If your application relies on entity changes and queries the flushlog frequently, a viable approach would be to use external search system that can work with JSON - like Elasticsearch, Sphinxsearch, or Lucene.
Can you support versioning of objects?
Yes, I could (creating a version for each entity, linking to particular flushlog), but it will take some time. You can submit a pull request or, alternatively, sponsor the development.
There is a bundle in development.
- Initialize the
FlushLogSubscriber
:
$subscriber = new Noop\FlushlLog\Doctrine\ORM\FlushLogSubscriber();
- Set the configuration (see example below):
$subscriber->setConfiguration(...);
- Add the subscriber to Doctrine's
EventManager
:
$eventManager->addEventSubscriber($subscriber);
- Extend
Noop\FlushLog\Doctrine\Entity\BaseLogEntry
to update your schema accordingly (it's aMappedSuperclass
, so your entity should be beautifully empty):
<?php
namespace App\Tests\Entity;
use Noop\FlushLog\Doctrine\Entity\BaseLogEntry;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity()
*/
class LogEntry extends BaseLogEntry
{
}
Example JSON:
{
"e": {
"App\\Entity\\EntityName": [6, 18, "123;31"]
},
"i": {
"App\\Entity\\EntityName": [6, 18, "123;31"]
},
"cs": {
"App\\Entity\\EntityName": {
"6": {
"field": [null, "asdf"],
"field1": [null, 6]
}
}
}
}
"e"
contains class -> id map of all affected entities (either directly or indirectly, by configuration or by collection change)"i"
contains class -> id maps of all inserted entities"cs"
contains class -> id -> field map of all changesets. You can set tracked fields per-entity in the configuration. By default, if entity is tracked, all fields are tracked
Example configuration (set in FlushLogSubscriber::setConfiguration
):
[
// lists tracked entities. All other entities are ignored
'entities' => [
'App\Entity\EntityName' => [
],
'App\Entity\OtherEntityName' => [
// if "fields" is set, only these fields will be tracked, otherwise, all fields are tracked
'fields' => ['id', 'name']
],
]
]