Skip to content

Commit

Permalink
Add model's withCount columns ot exempt attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
denitsa-md committed Dec 3, 2018
1 parent 14a36bd commit f3dd7ca
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 1 deletion.
9 changes: 8 additions & 1 deletion src/Cloneable.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,20 @@ trait Cloneable {
*/
public function getCloneExemptAttributes() {

// Alwyas make the id and timestamps exempt
// Always make the id and timestamps exempt
$defaults = [
$this->getKeyName(),
$this->getCreatedAtColumn(),
$this->getUpdatedAtColumn(),
];

// Include the model count columns in the exempt columns
$count_columns = array_map(function($count_column) {
return $count_column . '_count';
}, $this->withCount);

$defaults = array_merge($defaults, $count_columns);

// It none specified, just return the defaults, else, merge them
if (!isset($this->clone_exempt_attributes)) return $defaults;
return array_merge($defaults, $this->clone_exempt_attributes);
Expand Down
2 changes: 2 additions & 0 deletions stubs/Article.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
class Article extends Eloquent {
use Cloneable;

protected $withCount = ['photos'];

public $cloneable_relations = ['photos', 'authors', 'ratings'];

public function photos() {
Expand Down
4 changes: 4 additions & 0 deletions tests/CloneableTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,8 @@ public function testAddDuplicateRelation() {
$this->assertEquals(['photos', 'authors', 'ratings', 'test'], $article->getCloneableRelations());
}

public function testCountColumnsAreExempt() {
$article = new Article;
$this->assertContains('photos_count', $article->getCloneExemptAttributes());
}
}

0 comments on commit f3dd7ca

Please sign in to comment.