Download the PHP package mima/doctrine-entity-changes without Composer
On this page you can find all versions of the php package mima/doctrine-entity-changes. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package doctrine-entity-changes
Doctrine Entity Changes
Library makes it easy to get changes of Doctrine entities.
- Installation
- How to use
- Simple way to get changes
- Collect changes
- Field iteration
- Abstract visitors
- Visitor with callback
- Full implementation of visitor
- Getting field name
- Getting relation changes
- Apply visitor to concrete field
- How to help
Installation
Run command:
Add dependency to your composer.json:
How to use
Note that getting changes must be earlier then flush. For example, declare entity class:
Simple way to get changes
If field is part of association, then $fullName
equals to $fullName
equals to $name
. Old and New value converted from DateTime, Boolean, Integer and another fields by rules described above.
Convert dates to string
By default used this formats:
- date -> Y-m-d
- time -> H:i:s
- datetime -> Y-m-d H:i:s
You cat declare custom formats in first argument of ProcessorFieldVisitorFactory:
Convert booleans to string
By default boolean converts to this strings:
- true -> 'Checked'
- false|null -> 'Unchecked'
You can declare custom rules in second argument of ProcessorFieldVisitorFactory:
Convert floats to string
Float always converted to string accurate to 2 digits after the dot.
- 1 => '1.00'
- 10.3748 => '10.37'
- 2.3490 => '2.34'
Convert entities to string
Entity field values presents as serialized array:
You can use this fields for fetching entity.
Collect changes
Collect changes for example entity:
`
Field iteration
Field iteration is implemented using visitors. For example make ChangeLog entity
To simplify you can extend you visitor of:
- \Doctrine\ORM\ChangeSet\Visitor\AbstractEmptyFieldVisitor - full clear field visitor (with empty methods)
- \Doctrine\ORM\ChangeSet\Visitor\AbstractCommonFieldVisitor - visitor with logic to "pre-formatting" fields, like in example above
Otherwise you can implement \Doctrine\ORM\ChangeSetFieldVisitorInterface. For certain tasks you can use \Doctrine\ORM\ChangeSet\Visitor\CallbackFieldVisitor
Abstract visitors
Let's try to implement simple EntityFieldVisitor with AbstractCommonFieldVisitor:
You can add list of accepted fields in third argument of parent class constructor. By default accepted all fields. Example of saving received logs:
Visitor with callback
Callback visitor allows you to walk through field changes with a specific type.
Full implementation of visitor
Further you need it's implement FieldVisitorInterface:
Getting field name
For getting field name relative to the class where the field is declared all of you need:
This code return convrete field name: title, createdAt, etc. For getting full path of field, relative to root class in a chain to relations, need to do the following:
This code return field name like: relation.subRelation.title
Getting relation changes
The visitor enters only fields. You can getting relations changes through call:
As another way you can apply visitor to concrete related change set:
In this case relatedFieldName is name of field for which the association is declared. You can get access to any relation level: relatedFieldName.subRelatedField
Apply visitor to concrete field
You can apply visitor to conreate field:
In this case fieldName is name of target field. You can get access to field in related changes:
How to help
- Improve README
- Fix text in README (My English is poor)
- Improve library: any suggestions, corrections are accepted