Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 12 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,8 @@ Or run the following command directly without changing your `composer.json`:

`composer require josegonzalez/cakephp-version:dev-master`

## Usage

In your app's `config/bootstrap.php` add:
In your app's `config/bootstrap.php` add to load the Plugin:

```php
Plugin::load('Josegonzalez/Version', ['bootstrap' => true]);
Expand Down Expand Up @@ -93,7 +92,7 @@ class PostEntity extends Entity {
}
```

Attach the behavior in the models you want with:
Attach the behavior in the table initialize function you want with:

```php
public function initialize(array $config) {
Expand Down Expand Up @@ -136,8 +135,10 @@ CREATE TABLE `version` (
```

Then define an event listener to handle the event and pass in additional metadata, for example:

App/src/Event/VersionListener.php
```php
namespace App\Event;

use Cake\Event\Event;
use Cake\Event\EventListenerInterface;

Expand All @@ -159,7 +160,7 @@ class VersionListener implements EventListenerInterface {
```

Your event listener can then be attached in your project, for example:

in bootstrap.php
```php
use App\Event\VersionListener;
use Cake\Event\EventManager;
Expand All @@ -173,7 +174,7 @@ This can provide useful functionality, but ensure that if your event listener re
`version_id`, `model`, `foreign_key`, `field`, `content` or `created` that this is the intended behavior.

#### Storing user_id as Meta Data
To store the `user_id` as additional meta data is easiest in combination with [Muffin/Footprint](https://github.com/UseMuffin/Footprint).
To store the `modified by user id` as additional meta data is easiest in combination with [Muffin/Footprint](https://github.com/UseMuffin/Footprint).
The above `insertAdditionalData()` method could then look like this:

```php
Expand All @@ -185,13 +186,14 @@ The above `insertAdditionalData()` method could then look like this:
public function insertAdditionalData(Event $event)
{
$data = [
...
//your additional custom data
];

//get modified_by_user_id from footprint plugin which holds the current logged in user entity
if ($event->data('_footprint')) {
$user = $event->data('_footprint');
$data += [
'user_id' => $user['id'],
$data += [
'modified_by_user_id' => $user->id,
Copy link
Author

@reloxx13 reloxx13 Sep 7, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

im not sure about this, i use a custom auth and in $this->Auth->user() is an array with my oauth tokendata and the user entity.

so its not the default footprint data

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why did you change the example?

];
}

Expand Down Expand Up @@ -259,3 +261,4 @@ There are five behavior configurations that may be used:
- `additionalVersionFields`: (Default `['created']`) The additional or custom fields of the versioned table to be exposed as well. By default prefixed with `version_`, e.g. `'version_user_id'` for `'user_id'`.
- `referenceName`: (Default: db table name) Discriminator used to identify records in the version table.
- `onlyDirty`: (Default: false) Set to true to version only dirty properties.
- `fields`: List of fields which should get saved in the versions table, if not defined all fields will be saved. Can be string or array