diff --git a/composer.phar b/composer.phar new file mode 100755 index 0000000..5496f4b Binary files /dev/null and b/composer.phar differ diff --git a/src/Model/Table/SoftDeleteTrait.php b/src/Model/Table/SoftDeleteTrait.php index 88a4675..122ff77 100644 --- a/src/Model/Table/SoftDeleteTrait.php +++ b/src/Model/Table/SoftDeleteTrait.php @@ -6,7 +6,8 @@ use SoftDelete\Error\MissingColumnException; use SoftDelete\ORM\Query; -trait SoftDeleteTrait { +trait SoftDeleteTrait +{ /** * Get the configured deletion field @@ -34,6 +35,46 @@ public function getSoftDeleteField() return $field; } + /** + * Get the configured deletion field + * + * @return string + * @throws \SoftDelete\Error\MissingFieldException + */ + public function getSoftDeleteFieldMessage() + { + if (isset($this->softDeleteFieldMessage)) { + $field = $this->softDeleteFieldMessage; + } else { + $field = 'message_delete'; + } + + if ($this->schema()->column($field) === null) { + throw new MissingColumnException( + __('Configured field `{0}` is missing from the table `{1}`.', + $field, + $this->alias() + ) + ); + } + + return $field; + } + + /** + * Get the configured deletion field + * + * @return string + * @throws \SoftDelete\Error\MissingFieldException + */ + public function getSoftDeleteMessage() + { + if (isset($this->softDeleteMessage)) { + return $this->softDeleteMessage; + } + return __('Configured message is missing from the table.'); + } + public function query() { return new Query($this->connection(), $this); @@ -84,7 +125,10 @@ protected function _processDelete($entity, $options) $query = $this->query(); $conditions = (array)$entity->extract($primaryKey); $statement = $query->update() - ->set([$this->getSoftDeleteField() => date('Y-m-d H:i:s')]) + ->set([ + $this->getSoftDeleteField() => date('Y-m-d H:i:s'), + $this->getSoftDeleteFieldMessage() => $this->getSoftDeleteMessage(), + ]) ->where($conditions) ->execute(); @@ -122,7 +166,7 @@ public function deleteAll($conditions) */ public function hardDelete(EntityInterface $entity) { - if(!$this->delete($entity)) { + if (!$this->delete($entity)) { return false; } $primaryKey = (array)$this->primaryKey(); @@ -142,7 +186,7 @@ public function hardDelete(EntityInterface $entity) /** * Hard deletes all records that were soft deleted before a given date. - * @param \DateTime $until Date until which soft deleted records must be hard deleted. + * @param \DateTime $until Date until witch soft deleted records must be hard deleted. * @return int number of affected rows. */ public function hardDeleteAll(\Datetime $until) diff --git a/tests/Fixture/PostsFixture.php b/tests/Fixture/PostsFixture.php index 19c60a3..cda7c02 100644 --- a/tests/Fixture/PostsFixture.php +++ b/tests/Fixture/PostsFixture.php @@ -26,6 +26,7 @@ class PostsFixture extends TestFixture { 'id' => ['type' => 'integer'], 'user_id' => ['type' => 'integer', 'default' => '0', 'null' => false], 'deleted' => ['type' => 'datetime', 'default' => null, 'null' => true], + 'message_delete' => ['type' => 'string'], '_constraints' => [ 'primary' => ['type' => 'primary', 'columns' => ['id']] ] diff --git a/tests/Fixture/PostsTagsFixture.php b/tests/Fixture/PostsTagsFixture.php index b871cb3..e05d6f1 100644 --- a/tests/Fixture/PostsTagsFixture.php +++ b/tests/Fixture/PostsTagsFixture.php @@ -19,13 +19,15 @@ public function initialize(array $config) } -class PostsTagsFixture extends TestFixture { +class PostsTagsFixture extends TestFixture +{ public $fields = [ 'id' => ['type' => 'integer'], 'post_id' => ['type' => 'integer'], 'tag_id' => ['type' => 'integer'], - 'deleted' => ['type' => 'datetime', 'default' => null, 'null' => true], + 'deleted' => ['type' => 'datetime', 'default' => null, 'null' => true], + 'message_delete' => ['type' => 'string'], '_constraints' => [ 'primary' => ['type' => 'primary', 'columns' => ['id']] ] diff --git a/tests/Fixture/TagsFixture.php b/tests/Fixture/TagsFixture.php index 0e09a54..6a7dba2 100644 --- a/tests/Fixture/TagsFixture.php +++ b/tests/Fixture/TagsFixture.php @@ -12,6 +12,8 @@ class TagsTable extends Table use SoftDeleteTrait; protected $softDeleteField = 'deleted_date'; + protected $softDeleteFieldMessage = 'message_delete'; + protected $softDeleteMessage = 'registration errors'; public function initialize(array $config) { @@ -26,12 +28,14 @@ public function initialize(array $config) } -class TagsFixture extends TestFixture { +class TagsFixture extends TestFixture +{ public $fields = [ - 'id' => ['type' => 'integer'], - 'name' => ['type' => 'string'], - 'deleted_date' => ['type' => 'datetime', 'default' => null, 'null' => true], + 'id' => ['type' => 'integer'], + 'name' => ['type' => 'string'], + 'deleted_date' => ['type' => 'datetime', 'default' => null, 'null' => true], + 'message_delete' => ['type' => 'string'], '_constraints' => [ 'primary' => ['type' => 'primary', 'columns' => ['id']] ] diff --git a/tests/Fixture/UsersFixture.php b/tests/Fixture/UsersFixture.php index ef50e0e..daa9059 100644 --- a/tests/Fixture/UsersFixture.php +++ b/tests/Fixture/UsersFixture.php @@ -26,6 +26,7 @@ class UsersFixture extends TestFixture { 'id' => ['type' => 'integer'], 'posts_count' => ['type' => 'integer', 'default' => '0', 'null' => false], 'deleted' => ['type' => 'datetime', 'default' => null, 'null' => true], + 'message_delete' => ['type' => 'string'], '_constraints' => [ 'primary' => ['type' => 'primary', 'columns' => ['id']] ] diff --git a/tests/TestCase/Model/Table/SoftDeleteTraitTest.php b/tests/TestCase/Model/Table/SoftDeleteTraitTest.php index 5370c84..2d4fe38 100644 --- a/tests/TestCase/Model/Table/SoftDeleteTraitTest.php +++ b/tests/TestCase/Model/Table/SoftDeleteTraitTest.php @@ -106,7 +106,7 @@ public function testFindBelongsToMany() public function testFindMatching() { $users = $this->usersTable->find() - ->matching('Posts', function($q) { + ->matching('Posts', function ($q) { return $q->where(['Posts.id' => 1]); }); $this->assertEquals(1, $users->count()); @@ -118,7 +118,7 @@ public function testFindMatching() $this->assertEquals(1, $posts->count()); $users = $this->usersTable->find() - ->matching('Posts', function($q) { + ->matching('Posts', function ($q) { return $q->where(['Posts.id' => 1]); }); $this->assertEquals(0, $users->count());