Skip to content

Commit 72fc419

Browse files
authored
Merge pull request #20 from robertpustulka/only-dirty
Added support for versioning only dirty properties.
2 parents 44b632d + ba095d0 commit 72fc419

File tree

3 files changed

+27
-2
lines changed

3 files changed

+27
-2
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -228,3 +228,4 @@ There are three behavior configurations that may be used:
228228
- `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.
229229
- `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.
230230
- `referenceName`: (Default: db table name) Discriminator used to identify records in the version table.
231+
- `onlyDirty`: (Default: false) Set to true to version only dirty properties.

src/Model/Behavior/VersionBehavior.php

+3-2
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,8 @@ class VersionBehavior extends Behavior
5959
'versionField' => 'version_id',
6060
'fields' => null,
6161
'foreignKey' => 'foreign_key',
62-
'referenceName' => null
62+
'referenceName' => null,
63+
'onlyDirty' => false
6364
];
6465

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

163164
$fields = $this->_fields();
164-
$values = $entity->extract($fields);
165+
$values = $entity->extract($fields, $this->_config['onlyDirty']);
165166

166167
$model = $this->_config['referenceName'];
167168
$primaryKey = (array)$this->_table->primaryKey();

tests/TestCase/Model/Behavior/VersionBehaviorTest.php

+23
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,29 @@ public function testSaveLimitFields()
131131
$this->assertEquals('title', $results[0]['field']);
132132
}
133133

134+
public function testSaveDirtyFields()
135+
{
136+
$table = TableRegistry::get('Articles', [
137+
'entityClass' => 'Josegonzalez\Version\Test\TestCase\Model\Behavior\TestEntity'
138+
]);
139+
$table->addBehavior('Josegonzalez/Version.Version', ['onlyDirty' => true]);
140+
$article = $table->find('all')->first();
141+
142+
$article->title = 'Titulo';
143+
$article->body = 'Hello world!';
144+
$table->save($article);
145+
146+
$versionTable = TableRegistry::get('Version');
147+
$results = $versionTable->find('all')
148+
->where(['foreign_key' => $article->id, 'version_id' => 3])
149+
->hydrate(false)
150+
->toArray();
151+
152+
$this->assertCount(2, $results);
153+
$this->assertEquals('title', $results[0]['field']);
154+
$this->assertEquals('body', $results[1]['field']);
155+
}
156+
134157
public function testFindVersionLimitFields()
135158
{
136159
$table = TableRegistry::get('Articles', [

0 commit comments

Comments
 (0)