Skip to content

Commit

Permalink
Merge pull request #20 from robertpustulka/only-dirty
Browse files Browse the repository at this point in the history
Added support for versioning only dirty properties.
  • Loading branch information
josegonzalez authored Nov 29, 2016
2 parents 44b632d + ba095d0 commit 72fc419
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 2 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -228,3 +228,4 @@ There are three behavior configurations that may be used:
- `versionTable`: (Default: `version`) The name of the table to be used to store versioned data. It may be useful to use a different table when versioning multiple types of entities.
- `versionField`: (Default: `version_id`) The name of the field in the versioned table that will store the current version. If missing, the plugin will continue to work as normal.
- `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.
5 changes: 3 additions & 2 deletions src/Model/Behavior/VersionBehavior.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,8 @@ class VersionBehavior extends Behavior
'versionField' => 'version_id',
'fields' => null,
'foreignKey' => 'foreign_key',
'referenceName' => null
'referenceName' => null,
'onlyDirty' => false
];

/**
Expand Down Expand Up @@ -161,7 +162,7 @@ public function beforeSave(Event $event, Entity $entity, ArrayObject $options)
$options['associated'] = $newOptions + $options['associated'];

$fields = $this->_fields();
$values = $entity->extract($fields);
$values = $entity->extract($fields, $this->_config['onlyDirty']);

$model = $this->_config['referenceName'];
$primaryKey = (array)$this->_table->primaryKey();
Expand Down
23 changes: 23 additions & 0 deletions tests/TestCase/Model/Behavior/VersionBehaviorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,29 @@ public function testSaveLimitFields()
$this->assertEquals('title', $results[0]['field']);
}

public function testSaveDirtyFields()
{
$table = TableRegistry::get('Articles', [
'entityClass' => 'Josegonzalez\Version\Test\TestCase\Model\Behavior\TestEntity'
]);
$table->addBehavior('Josegonzalez/Version.Version', ['onlyDirty' => true]);
$article = $table->find('all')->first();

$article->title = 'Titulo';
$article->body = 'Hello world!';
$table->save($article);

$versionTable = TableRegistry::get('Version');
$results = $versionTable->find('all')
->where(['foreign_key' => $article->id, 'version_id' => 3])
->hydrate(false)
->toArray();

$this->assertCount(2, $results);
$this->assertEquals('title', $results[0]['field']);
$this->assertEquals('body', $results[1]['field']);
}

public function testFindVersionLimitFields()
{
$table = TableRegistry::get('Articles', [
Expand Down

0 comments on commit 72fc419

Please sign in to comment.