Skip to content

Commit

Permalink
Add clone handling
Browse files Browse the repository at this point in the history
  • Loading branch information
ronanchilvers committed Oct 10, 2019
1 parent 65d43cf commit 551e4a7
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 1 deletion.
11 changes: 11 additions & 0 deletions src/Features/HasTimestamps.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,4 +64,15 @@ protected function updateTimestamps()
$this->setAttribute(static::$updated, $now);
}
}

/**
* Clear the timestamps on this model
*
* @author Ronan Chilvers <[email protected]>
*/
protected function clearTimestamps()
{
$this->setAttribute(static::$created, null);
$this->setAttribute(static::$updated, null);
}
}
30 changes: 29 additions & 1 deletion src/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -150,10 +150,38 @@ public static function qualify($string)
*/
public function __construct()
{
$this->bootHasTimestamps();
if ($this->useTimestamps()) {
$this->bootHasTimestamps();
}
$this->boot();
}

/**
* Magic clone method to ensure that cloned models are new
*
* @author Ronan Chilvers <[email protected]>
*/
public function __clone()
{
$primaryKey = static::primaryKey();
if (isset($this->data[$primaryKey])) {
unset($this->data[$primaryKey]);
}
if ($this->useTimestamps()) {
$this->clearTimestamps();
}
$this->clone();
}

/**
* Clone function designed to be overriden by subclasses
*
*
* @author Ronan Chilvers <[email protected]>
*/
protected function clone()
{}

/**
* Boot the model
*
Expand Down

0 comments on commit 551e4a7

Please sign in to comment.