From 5542910ca1d25eca8d5eda779fedabb0f8f7c759 Mon Sep 17 00:00:00 2001 From: Simon Bouland Date: Fri, 5 May 2017 15:04:49 +0200 Subject: [PATCH] Expose preexistent logic in public function getVersionId --- src/Model/Behavior/VersionBehavior.php | 43 ++++++++++++++++---------- 1 file changed, 26 insertions(+), 17 deletions(-) diff --git a/src/Model/Behavior/VersionBehavior.php b/src/Model/Behavior/VersionBehavior.php index bcb8554..27397af 100644 --- a/src/Model/Behavior/VersionBehavior.php +++ b/src/Model/Behavior/VersionBehavior.php @@ -167,29 +167,17 @@ 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; @@ -197,11 +185,11 @@ public function beforeSave(Event $event, EntityInterface $entity, ArrayObject $o $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); @@ -209,7 +197,6 @@ public function beforeSave(Event $event, EntityInterface $entity, ArrayObject $o $data = array_merge($data, $userData->result); } - $entityClass = $table->entityClass(); $new[$field] = new $entityClass($data, [ 'useSetters' => false, 'markNew' => true @@ -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. *