Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in / Register
Toggle navigation
Y
yii2-softdelete
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
CI / CD
CI / CD
Pipelines
Schedules
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Commits
Open sidebar
digisin
yii2-softdelete
Commits
da8679dc
Commit
da8679dc
authored
May 30, 2015
by
cornernote
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
got it working
parent
1987e2f5
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
21 additions
and
18 deletions
+21
-18
SoftDeleteBehavior.php
behaviors/SoftDeleteBehavior.php
+21
-18
No files found.
SoftDeleteBehavior.php
→
behaviors/
SoftDeleteBehavior.php
View file @
da8679dc
...
@@ -2,10 +2,10 @@
...
@@ -2,10 +2,10 @@
namespace
cornernote\behaviors
;
namespace
cornernote\behaviors
;
use
yii\base\Behavior
;
use
yii\base\Event
;
use
yii\base\Event
;
use
yii\db\BaseActiveRecord
;
use
yii\db\BaseActiveRecord
;
use
yii\db\Expression
;
use
yii\db\Expression
;
use
yii\behaviors\AttributeBehavior
;
/**
/**
* SoftDeleteBehavior
* SoftDeleteBehavior
...
@@ -27,12 +27,13 @@ use yii\behaviors\AttributeBehavior;
...
@@ -27,12 +27,13 @@ use yii\behaviors\AttributeBehavior;
* @author cornernote <cornernote@gmail.com>
* @author cornernote <cornernote@gmail.com>
* @author amnah <amnah.dev@gmail.com>
* @author amnah <amnah.dev@gmail.com>
*/
*/
class
SoftDeleteBehavior
extends
Attribute
Behavior
class
SoftDeleteBehavior
extends
Behavior
{
{
/**
/**
* @var string SoftDelete attribute
* @var string SoftDelete attribute
*/
*/
public
$deletedAtAttribute
=
'deleted_at'
;
public
$attribute
=
'deleted_at'
;
/**
/**
* @var callable|Expression The expression that will be used for generating the timestamp.
* @var callable|Expression The expression that will be used for generating the timestamp.
* This can be either an anonymous function that returns the timestamp value,
* This can be either an anonymous function that returns the timestamp value,
...
@@ -46,7 +47,7 @@ class SoftDeleteBehavior extends AttributeBehavior
...
@@ -46,7 +47,7 @@ class SoftDeleteBehavior extends AttributeBehavior
*/
*/
public
function
events
()
public
function
events
()
{
{
return
[
BaseActiveRecord
::
EVENT_BEFORE_DELETE
=>
'
doDeleteTimestamp
'
];
return
[
BaseActiveRecord
::
EVENT_BEFORE_DELETE
=>
'
softDeleteEvent
'
];
}
}
/**
/**
...
@@ -54,32 +55,31 @@ class SoftDeleteBehavior extends AttributeBehavior
...
@@ -54,32 +55,31 @@ class SoftDeleteBehavior extends AttributeBehavior
*
*
* @param Event $event
* @param Event $event
*/
*/
public
function
doDeleteTimestamp
(
$event
)
public
function
softDeleteEvent
(
$event
)
{
{
// remove and mark as invalid to prevent real deletion
// remove and mark as invalid to prevent real deletion
$this
->
remov
e
();
$this
->
softDelet
e
();
$event
->
isValid
=
false
;
$event
->
isValid
=
false
;
}
}
/**
/**
*
Remove (aka soft-delete)
record
*
Soft delete
record
*/
*/
public
function
remov
e
()
public
function
softDelet
e
()
{
{
// evaluate timestamp and set attribute
// set attribute with evaluated timestamp
$timestamp
=
$this
->
evaluateAttributes
();
$attribute
=
$this
->
attribute
;
$attribute
=
$this
->
attribute
;
$this
->
owner
->
$attribute
=
$t
imestamp
;
$this
->
owner
->
$attribute
=
$t
his
->
getValue
(
null
)
;
// save record
// save record
$this
->
owner
->
save
(
false
,
[
$attribute
]);
$this
->
owner
->
save
(
false
,
[
$attribute
]);
}
}
/**
/**
* Restore
soft-deleted
record
* Restore record
*/
*/
public
function
restor
e
()
public
function
unDelet
e
()
{
{
//
mark
attribute as null
//
set
attribute as null
$attribute
=
$this
->
attribute
;
$attribute
=
$this
->
attribute
;
$this
->
owner
->
$attribute
=
null
;
$this
->
owner
->
$attribute
=
null
;
// save record
// save record
...
@@ -89,17 +89,20 @@ class SoftDeleteBehavior extends AttributeBehavior
...
@@ -89,17 +89,20 @@ class SoftDeleteBehavior extends AttributeBehavior
/**
/**
* Delete record from database regardless of the $safeMode attribute
* Delete record from database regardless of the $safeMode attribute
*/
*/
public
function
force
Delete
()
public
function
hard
Delete
()
{
{
// store model so that we can detach the behavior
and delete as normal
// store model so that we can detach the behavior
$model
=
$this
->
owner
;
$model
=
$this
->
owner
;
$this
->
detach
();
$this
->
detach
();
// delete as normal
$model
->
delete
();
$model
->
delete
();
}
}
/**
/**
* @inheritdoc
* Evaluate the timestamp to be saved.
*
* @param Event $event the event that triggers the current attribute updating.
* @return mixed the attribute value
*/
*/
protected
function
getValue
(
$event
)
protected
function
getValue
(
$event
)
{
{
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment