From f3dd7ca05872660279307328665d3338f93d5d70 Mon Sep 17 00:00:00 2001 From: Denitsa Mlechkova Date: Mon, 3 Dec 2018 11:47:53 +0100 Subject: [PATCH] Add model's `withCount` columns ot exempt attributes --- src/Cloneable.php | 9 ++++++++- stubs/Article.php | 2 ++ tests/CloneableTest.php | 4 ++++ 3 files changed, 14 insertions(+), 1 deletion(-) diff --git a/src/Cloneable.php b/src/Cloneable.php index 3e6e618..4ceaf02 100644 --- a/src/Cloneable.php +++ b/src/Cloneable.php @@ -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); diff --git a/stubs/Article.php b/stubs/Article.php index 5ee409f..3260d6d 100644 --- a/stubs/Article.php +++ b/stubs/Article.php @@ -6,6 +6,8 @@ class Article extends Eloquent { use Cloneable; + protected $withCount = ['photos']; + public $cloneable_relations = ['photos', 'authors', 'ratings']; public function photos() { diff --git a/tests/CloneableTest.php b/tests/CloneableTest.php index 0c02f9e..35bbb3b 100644 --- a/tests/CloneableTest.php +++ b/tests/CloneableTest.php @@ -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()); + } }