Skip to content

Messages #19

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added composer.phar
Binary file not shown.
52 changes: 48 additions & 4 deletions src/Model/Table/SoftDeleteTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
use SoftDelete\Error\MissingColumnException;
use SoftDelete\ORM\Query;

trait SoftDeleteTrait {
trait SoftDeleteTrait
{

/**
* Get the configured deletion field
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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();

Expand Down Expand Up @@ -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();
Expand All @@ -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)
Expand Down
1 change: 1 addition & 0 deletions tests/Fixture/PostsFixture.php
Original file line number Diff line number Diff line change
Expand Up @@ -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']]
]
Expand Down
6 changes: 4 additions & 2 deletions tests/Fixture/PostsTagsFixture.php
Original file line number Diff line number Diff line change
Expand Up @@ -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']]
]
Expand Down
12 changes: 8 additions & 4 deletions tests/Fixture/TagsFixture.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand All @@ -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']]
]
Expand Down
1 change: 1 addition & 0 deletions tests/Fixture/UsersFixture.php
Original file line number Diff line number Diff line change
Expand Up @@ -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']]
]
Expand Down
4 changes: 2 additions & 2 deletions tests/TestCase/Model/Table/SoftDeleteTraitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand All @@ -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());
Expand Down