Skip to content

Commit

Permalink
Updates Temporal to save revisions correctly when save() is called …
Browse files Browse the repository at this point in the history
…multiple times per request.

Temporal's save logic for when models are not new was fully rewitten. Now instead of saving the new information as a new model the old data is inserted as a new row and the current row is saved with the new information. This means that the model object that `save()` is called on will always point to the most recent revision in the DB.

Proper comments where also added with the correct copright and author information.
  • Loading branch information
Steve "uru" West committed Apr 24, 2013
1 parent 0ba2d8d commit e4fc063
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 38 deletions.
15 changes: 14 additions & 1 deletion classes/model/soft.php
@@ -1,4 +1,16 @@
<?php
/**
* Fuel
*
* Fuel is a fast, lightweight, community driven PHP5 framework.
*
* @package Fuel
* @version 1.6
* @author Fuel Development Team
* @license MIT License
* @copyright 2010 - 2013 Fuel Development Team
* @link http://fuelphp.com
*/

namespace Orm;

Expand All @@ -12,7 +24,8 @@ class RelationNotSoft extends \Exception
* that the data has been deleted but the data itself is not removed from the
* database.
*
* @author Steve "Uru" West <uruwolf@gmail.com>
* @package Orm
* @author Fuel Development Team
*/
class Model_Soft extends Model
{
Expand Down
72 changes: 37 additions & 35 deletions classes/model/temporal.php
@@ -1,11 +1,24 @@
<?php
/**
* Fuel
*
* Fuel is a fast, lightweight, community driven PHP5 framework.
*
* @package Fuel
* @version 1.6
* @author Fuel Development Team
* @license MIT License
* @copyright 2010 - 2013 Fuel Development Team
* @link http://fuelphp.com
*/

namespace Orm;

/**
* Allows revisions of database entries to be kept when updates are made.
*
* @author Steve "Uru" West <uruwolf@gmail.com>
* @package Orm
* @author Fuel Development Team
*/
class Model_Temporal extends Model
{
Expand Down Expand Up @@ -330,9 +343,10 @@ private static function getNonTimestampPks()

/**
* Overrides the save method to allow temporal models to be
* @param type $cascade
* @param type $use_transaction
* @return type
* @param boolean $cascade
* @param boolean $use_transaction
* @param boolean $skip_temporal Skips temporal filtering on initial inserts. Should not be used!
* @return boolean
*/
public function save($cascade = null, $use_transaction = false)
{
Expand Down Expand Up @@ -368,45 +382,33 @@ public function save($cascade = null, $use_transaction = false)

if (count($diff[0]) > 0)
{
self::disable_primary_key_check();
$this->{$timestamp_end_name} = $current_timestamp;
self::enable_primary_key_check();
//Take a copy of this model
$revision = clone $this;

//If we cannot save then don't bother updating anything else to prevent bad data in the database.
if ( ! parent::save($cascade, $use_transaction) )
{
return false;
}
//Give that new model an end time of the current time after resetting back to the old data
$revision->set($this->_original);

//Take a copy before resetting
$newModel = clone $this;
self::disable_primary_key_check();
$revision->{$timestamp_end_name} = $current_timestamp;
self::enable_primary_key_check();

//Reset the current model and update the timestamp
$this->reset();
//save that, now we have our archive
self::enable_id_only_primary_key();
$revision_result = $revision->overwrite(false, $use_transaction);
self::disable_id_only_primary_key();

//Construct a copy of this model and save that with a 0 timestamp
//Copy the PKs
foreach ($this->primary_key() as $pk)
if ( ! $revision_result)
{
if ($pk != $timestamp_start_name && $pk != $timestamp_end_name)
{
$newModel->{$pk} = $this->{$pk};
}
//If the revision did not save then stop the process so the user can do something.
return false;
}

//Then update the timestamps
static::disable_primary_key_check();
$newModel->{$timestamp_start_name} = $current_timestamp;
$newModel->{$timestamp_end_name} = $max_timestamp;
static::enable_primary_key_check();

$result = $newModel->save();
//Now that the old data is saved update the current object so its end timestamp is now
self::disable_primary_key_check();
$this->{$timestamp_start_name} = $current_timestamp;
self::enable_primary_key_check();

//Make sure $this is repopulated correctly (So Id's are present)
foreach($this->properties() as $proptery => $settings)
{
$this->_data[$proptery] = $newModel->{$proptery};
}
$result = parent::save($cascade, $use_transaction);

return $result;
}
Expand Down
1 change: 1 addition & 0 deletions classes/query/soft.php
Expand Up @@ -18,6 +18,7 @@
* Overrides the default Query object to allow for custom soft delete filtering on queries.
*
* @package Orm
* @author Fuel Development Team
*/
class Query_Soft extends Query
{
Expand Down
15 changes: 14 additions & 1 deletion classes/query/temporal.php
@@ -1,12 +1,25 @@
<?php
/**
* Fuel
*
* Fuel is a fast, lightweight, community driven PHP5 framework.
*
* @package Fuel
* @version 1.6
* @author Fuel Development Team
* @license MIT License
* @copyright 2010 - 2013 Fuel Development Team
* @link http://fuelphp.com
*/

namespace Orm;

/**
* Adds temporal properties to the query object to allow for correct relation
* filtering on joins.
*
* @author Steve "uru" West <uruwolf@gmail.com>
* @package Orm
* @author Fuel Development Team
*/
class Query_Temporal extends Query
{
Expand Down
2 changes: 1 addition & 1 deletion config/orm.php
@@ -1,5 +1,5 @@
<?php
return array(
'sql_max_timestamp_mysql' => '2038-01-18 22:14:08',
'sql_max_timestamp_unix' => '2147483647',
'sql_max_timestamp_unix' => 2147483647,
);

0 comments on commit e4fc063

Please sign in to comment.