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
e5abaee8
Commit
e5abaee8
authored
May 30, 2015
by
cornernote
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
change namespace again
parent
02c356ab
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
115 additions
and
89 deletions
+115
-89
SoftDelete.php
SoftDelete.php
+0
-88
SoftDeleteBehavior.php
SoftDeleteBehavior.php
+114
-0
composer.json
composer.json
+1
-1
No files found.
SoftDelete.php
deleted
100644 → 0
View file @
02c356ab
<?php
namespace
cornernote\softdelete
;
use
yii\base\Behavior
;
use
yii\base\Event
;
use
yii\db\ActiveRecord
;
/**
* Class SoftDelete
*
* @package vendor\vyants\softdelete
* @author Vladimir Yants <vladimir.yants@gmail.com>
* @property ActiveRecord $owner
*/
class
SoftDelete
extends
Behavior
{
/**
* @var string delete time attribute
*/
public
$timeAttribute
=
false
;
/**
* @var string status attribute
*/
public
$statusAttribute
=
"status"
;
/**
* @var string deleted status attribute
*/
public
$deletedValue
=
-
1
;
/**
* @var string active status attribute
*/
public
$activeValue
=
1
;
/**
* Удалить софтделитом. Возращает true/false в зависимости от того, успешно ли
* @return bool
*/
public
function
softDelete
()
{
if
(
$this
->
timeAttribute
)
{
$attributes
[
0
]
=
$this
->
timeAttribute
;
$this
->
owner
->
$attributes
[
0
]
=
time
();
}
$attributes
[
1
]
=
$this
->
statusAttribute
;
$this
->
owner
->
$attributes
[
1
]
=
$this
->
deletedValue
;
// save record
return
$this
->
owner
->
save
(
false
,
$attributes
);
}
/**
* Restore soft-deleted record. Возращает true/false в зависимости от того, успешно ли
* @return bool
*/
public
function
restore
()
{
if
(
$this
->
timeAttribute
)
{
$attributes
[
0
]
=
$this
->
timeAttribute
;
$this
->
owner
->
$attributes
[
0
]
=
null
;
}
$attributes
[
1
]
=
$this
->
statusAttribute
;
$this
->
owner
->
$attributes
[
1
]
=
$this
->
activeValue
;
// save record
return
$this
->
owner
->
save
(
false
,
$attributes
);
}
/**
* Force delete from database. Возращает true/false в зависимости от того, успешно ли
* @return bool
*/
public
function
forceDelete
()
{
// store model so that we can detach the behavior and delete as normal
$model
=
$this
->
owner
;
$this
->
detach
();
$result
=
$model
->
delete
();
$this
->
attach
(
$model
);
return
$result
;
}
}
\ No newline at end of file
SoftDeleteBehavior.php
0 → 100644
View file @
e5abaee8
<?php
namespace
cornernote\behaviors
;
use
yii\base\Event
;
use
yii\db\BaseActiveRecord
;
use
yii\db\Expression
;
use
yii\behaviors\AttributeBehavior
;
/**
* SoftDeleteBehavior
*
* @usage:
* ```
* public function behaviors() {
* return [
* 'softDelete' => ['class' => 'cornernote\behaviors\SoftDeleteBehavior',
* 'attribute' => 'delete_time',
* 'value' => new Expression('NOW()'),
* ],
* ];
* }
* ```
*
* @property BaseActiveRecord $owner
*
* @author cornernote <cornernote@gmail.com>
* @author amnah <amnah.dev@gmail.com>
*/
class
SoftDeleteBehavior
extends
AttributeBehavior
{
/**
* @var string SoftDelete attribute
*/
public
$deletedAtAttribute
=
'deleted_at'
;
/**
* @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,
* or an [[Expression]] object representing a DB expression (e.g. `new Expression('NOW()')`).
* If not set, it will use the value of `time()` to set the attributes.
*/
public
$value
;
/**
* @inheritdoc
*/
public
function
events
()
{
return
[
BaseActiveRecord
::
EVENT_BEFORE_DELETE
=>
'doDeleteTimestamp'
];
}
/**
* Set the attribute with the current timestamp to mark as deleted
*
* @param Event $event
*/
public
function
doDeleteTimestamp
(
$event
)
{
// remove and mark as invalid to prevent real deletion
$this
->
remove
();
$event
->
isValid
=
false
;
}
/**
* Remove (aka soft-delete) record
*/
public
function
remove
()
{
// evaluate timestamp and set attribute
$timestamp
=
$this
->
evaluateAttributes
();
$attribute
=
$this
->
attribute
;
$this
->
owner
->
$attribute
=
$timestamp
;
// save record
$this
->
owner
->
save
(
false
,
[
$attribute
]);
}
/**
* Restore soft-deleted record
*/
public
function
restore
()
{
// mark attribute as null
$attribute
=
$this
->
attribute
;
$this
->
owner
->
$attribute
=
null
;
// save record
$this
->
owner
->
save
(
false
,
[
$attribute
]);
}
/**
* Delete record from database regardless of the $safeMode attribute
*/
public
function
forceDelete
()
{
// store model so that we can detach the behavior and delete as normal
$model
=
$this
->
owner
;
$this
->
detach
();
$model
->
delete
();
}
/**
* @inheritdoc
*/
protected
function
getValue
(
$event
)
{
if
(
$this
->
value
instanceof
Expression
)
{
return
$this
->
value
;
}
else
{
return
$this
->
value
!==
null
?
call_user_func
(
$this
->
value
,
$event
)
:
time
();
}
}
}
\ No newline at end of file
composer.json
View file @
e5abaee8
...
...
@@ -15,7 +15,7 @@
},
"autoload"
:
{
"psr-4"
:
{
"cornernote
\\
softdelete
\\
"
:
""
"cornernote
\\
"
:
""
}
}
}
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