Commit a8f371b9 authored by Nicola Tomassoni's avatar Nicola Tomassoni

Digisin release

parent efa5f6bf
...@@ -10,4 +10,5 @@ build/ ...@@ -10,4 +10,5 @@ build/
vendor/ vendor/
# ignore temporary files # ignore temporary files
*~ *~
\ No newline at end of file /nbproject/private/
\ No newline at end of file
{ {
"name": "cornernote/yii2-softdelete", "name": "digisin/yii2-softdelete",
"description": "Soft delete behavior for Yii2.", "description": "Soft delete behavior for Yii2.",
"keywords": ["yii2", "soft delete"], "keywords": ["yii2", "soft delete"],
"type": "yii2-behavior", "type": "yii2-behavior",
...@@ -8,6 +8,10 @@ ...@@ -8,6 +8,10 @@
{ {
"name": "Brett O'Donnell", "name": "Brett O'Donnell",
"email": "cornernote@gmail.com" "email": "cornernote@gmail.com"
},
{
"name": "Nicola Tomassoni",
"email": "nicola@digisin.it"
} }
], ],
"require": { "require": {
...@@ -20,7 +24,7 @@ ...@@ -20,7 +24,7 @@
}, },
"autoload": { "autoload": {
"psr-4": { "psr-4": {
"cornernote\\softdelete\\": "src" "digisin\\softdelete\\": "src"
} }
} }
} }
include.path=${php.global.include.path}
php.version=PHP_56
source.encoding=UTF-8
src.dir=.
tags.asp=false
tags.short=false
web.root=.
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://www.netbeans.org/ns/project/1">
<type>org.netbeans.modules.php.project</type>
<configuration>
<data xmlns="http://www.netbeans.org/ns/php-project/1">
<name>yii2-softdelete</name>
</data>
</configuration>
</project>
<?php <?php
/** /**
* @author Brett O'Donnell <cornernote@gmail.com> * @author Brett O'Donnell <cornernote@gmail.com>
* @copyright 2015 Mr PHP * @copyright 2015 Mr PHP
...@@ -6,7 +7,7 @@ ...@@ -6,7 +7,7 @@
* @license BSD-3-Clause https://raw.github.com/cornernote/yii2-softdelete/master/LICENSE.md * @license BSD-3-Clause https://raw.github.com/cornernote/yii2-softdelete/master/LICENSE.md
*/ */
namespace cornernote\softdelete; namespace digisin\softdelete;
use yii\base\Behavior; use yii\base\Behavior;
use yii\base\Event; use yii\base\Event;
...@@ -31,90 +32,84 @@ use yii\db\Expression; ...@@ -31,90 +32,84 @@ use yii\db\Expression;
* *
* @property BaseActiveRecord $owner * @property BaseActiveRecord $owner
*/ */
class SoftDeleteBehavior extends Behavior class SoftDeleteBehavior extends Behavior {
{
/**
* @var string SoftDelete attribute
*/
public $attribute = 'deleted_at';
/** /**
* @var callable|Expression The expression that will be used for generating the timestamp. * @var string SoftDelete attribute
* 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()')`). public $attribute = 'deleted_at';
* If not set, it will use the value of `time()` to set the attributes.
*/
public $value;
/** /**
* @inheritdoc * @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,
public function events() * 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.
return [BaseActiveRecord::EVENT_BEFORE_DELETE => 'softDeleteEvent']; */
} public $value;
/** /**
* Set the attribute with the current timestamp to mark as deleted * @inheritdoc
* */
* @param Event $event public function events() {
*/ return [BaseActiveRecord::EVENT_BEFORE_DELETE => 'softDeleteEvent'];
public function softDeleteEvent($event) }
{
// remove and mark as invalid to prevent real deletion
$this->softDelete();
$event->isValid = false;
}
/** /**
* Soft delete record * Set the attribute with the current timestamp to mark as deleted
*/ *
public function softDelete() * @param Event $event
{ */
// set attribute with evaluated timestamp public function softDeleteEvent($event) {
$attribute = $this->attribute; // remove and mark as invalid to prevent real deletion
$this->owner->$attribute = $this->getValue(null); $this->softDelete();
// save record $event->isValid = false;
$this->owner->save(false, [$attribute]); }
}
/** /**
* Restore record * Soft delete record
*/ */
public function unDelete() public function softDelete() {
{ // set attribute with evaluated timestamp
// set attribute as null $attribute = $this->attribute;
$attribute = $this->attribute; $this->owner->$attribute = $this->getValue(null);
$this->owner->$attribute = null; // save record
// save record $this->owner->save(false, [$attribute]);
$this->owner->save(false, [$attribute]); }
}
/** /**
* Delete record from database regardless of the $safeMode attribute * Restore record
*/ */
public function hardDelete() public function unDelete() {
{ // set attribute as null
// store model so that we can detach the behavior $attribute = $this->attribute;
$model = $this->owner; $this->owner->$attribute = null;
$this->detach(); // save record
// delete as normal $this->owner->save(false, [$attribute]);
$model->delete(); }
}
/**
* Delete record from database regardless of the $safeMode attribute
*/
public function hardDelete() {
// store model so that we can detach the behavior
$model = $this->owner;
$this->detach();
// delete as normal
$model->delete();
}
/** /**
* Evaluate the timestamp to be saved. * Evaluate the timestamp to be saved.
* *
* @param Event|null $event the event that triggers the current attribute updating. * @param Event|null $event the event that triggers the current attribute updating.
* @return mixed the attribute value * @return mixed the attribute value
*/ */
protected function getValue($event) protected function getValue($event) {
{ if ($this->value instanceof Expression) {
if ($this->value instanceof Expression) { return $this->value;
return $this->value; } else {
} else { return $this->value !== null ? call_user_func($this->value, $event) : time();
return $this->value !== null ? call_user_func($this->value, $event) : time();
}
} }
}
} }
\ No newline at end of file
<?php <?php
namespace cornernote\softdelete; namespace digisin\softdelete;
use yii\base\Behavior; use yii\base\Behavior;
use yii\db\ActiveQuery; use yii\db\ActiveQuery;
...@@ -25,37 +25,34 @@ use yii\db\ActiveRecord; ...@@ -25,37 +25,34 @@ use yii\db\ActiveRecord;
* *
* @author cornernote <cornernote@gmail.com> * @author cornernote <cornernote@gmail.com>
*/ */
class SoftDeleteQueryBehavior extends Behavior class SoftDeleteQueryBehavior extends Behavior {
{
/** /**
* @var string SoftDelete attribute * @var string SoftDelete attribute
*/ */
public $attribute = 'deleted_at'; public $attribute = 'deleted_at';
/** /**
* @return static * @return static
*/ */
public function deleted() public function deleted() {
{ return $this->owner->andWhere($this->tableName() . '.' . $this->attribute . ' IS NOT NULL');
return $this->owner->andWhere($this->tableName() . '.' . $this->attribute . ' IS NOT NULL'); }
}
/**
/** * @return static
* @return static */
*/ public function notDeleted() {
public function notDeleted() return $this->owner->andWhere($this->tableName() . '.' . $this->attribute . ' IS NULL');
{ }
return $this->owner->andWhere($this->tableName() . '.' . $this->attribute . ' IS NULL');
} /**
* @return string
/** */
* @return string protected function tableName() {
*/ /** @var ActiveRecord $modelClass */
protected function tableName() $modelClass = $this->owner->modelClass;
{ return $modelClass::tableName();
/** @var ActiveRecord $modelClass */ }
$modelClass = $this->owner->modelClass;
return $modelClass::tableName();
}
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment