Skip to content

Commit

Permalink
Merge pull request #31 from bouland/master
Browse files Browse the repository at this point in the history
Expose preexistent logic in public function getVersionId
  • Loading branch information
josegonzalez authored May 15, 2017
2 parents 4f0c098 + 18f6784 commit 2bfee28
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 17 deletions.
43 changes: 26 additions & 17 deletions src/Model/Behavior/VersionBehavior.php
Original file line number Diff line number Diff line change
Expand Up @@ -167,49 +167,36 @@ public function beforeSave(Event $event, EntityInterface $entity, ArrayObject $o
$fields = $this->_fields();
$values = $entity->extract($fields, $this->_config['onlyDirty']);

$model = $this->_config['referenceName'];
$primaryKey = (array)$this->_table->primaryKey();
$foreignKey = $this->_extractForeignKey($entity);
$versionField = $this->_config['versionField'];

if (isset($options['versionId'])) {
$versionId = $options['versionId'];
} else {
$table = TableRegistry::get($this->_config['versionTable']);
$preexistent = $table->find()
->select(['version_id'])
->where([
'model' => $model
] + $foreignKey)
->order(['id desc'])
->limit(1)
->hydrate(false)
->toArray();

$versionId = Hash::get($preexistent, '0.version_id', 0) + 1;
$versionId = $this->getVersionId($entity) + 1;
}
$created = new DateTime();
$new = [];
$entityClass = TableRegistry::get($this->_config['versionTable'])->entityClass();
foreach ($values as $field => $content) {
if (in_array($field, $primaryKey) || $field == $versionField) {
continue;
}

$data = [
'version_id' => $versionId,
'model' => $model,
'model' => $this->_config['referenceName'],
'field' => $field,
'content' => $content,
'created' => $created,
] + $foreignKey;
] + $this->_extractForeignKey($entity);

$event = new Event('Model.Version.beforeSave', $this, $options);
$userData = EventManager::instance()->dispatch($event);
if (isset($userData->result) && is_array($userData->result)) {
$data = array_merge($data, $userData->result);
}

$entityClass = $table->entityClass();
$new[$field] = new $entityClass($data, [
'useSetters' => false,
'markNew' => true
Expand All @@ -235,6 +222,28 @@ public function afterSave(Event $event, EntityInterface $entity)
$entity->unsetProperty($property);
}

/**
* return the last version id
*
* @param \Cake\Datasource\EntityInterface $entity Entity.
* @return int
*/
public function getVersionId(EntityInterface $entity)
{
$table = TableRegistry::get($this->_config['versionTable']);
$preexistent = $table->find()
->select(['version_id'])
->where([
'model' => $this->_config['referenceName']
] + $this->_extractForeignKey($entity))
->order(['id desc'])
->limit(1)
->hydrate(false)
->toArray();

return Hash::get($preexistent, '0.version_id', 0);
}

/**
* Custom finder method used to retrieve all versions for the found records.
*
Expand Down
21 changes: 21 additions & 0 deletions tests/TestCase/Model/Behavior/VersionBehaviorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -369,4 +369,25 @@ public function testAssociations()
$this->assertInstanceOf('Cake\Orm\Association\HasMany', $bodyVersions);
$this->assertEquals('body_version', $bodyVersions->property());
}

/**
* @return void
*/
public function testGetVersionId()
{
// init test data
$table = TableRegistry::get('Articles', [
'entityClass' => 'Josegonzalez\Version\Test\TestCase\Model\Behavior\TestEntity',
]);
$table->addBehavior('Josegonzalez/Version.Version');
$article = $table->find('all')->where(['version_id' => 2])->first();
$article->title = 'First Article Version 3';
$table->save($article);

// action in controller receiving outdated data
$table->patchEntity($article, ['version_id' => 2]);

$this->assertEquals(2, $article->version_id);
$this->assertEquals(3, $table->getVersionId($article));
}
}

0 comments on commit 2bfee28

Please sign in to comment.