Skip to content

Commit

Permalink
add find methods as draft
Browse files Browse the repository at this point in the history
  • Loading branch information
klimov-paul committed Jul 6, 2023
1 parent e9b6e37 commit d7514d1
Show file tree
Hide file tree
Showing 2 changed files with 108 additions and 0 deletions.
79 changes: 79 additions & 0 deletions src/SoftDeleteBehavior.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace yii1tech\ar\softdelete;

use CBehavior;
use CDbCriteria;
use CDbException;
use CDbExpression;
use CModelEvent;
Expand Down Expand Up @@ -423,6 +424,84 @@ public function safeDelete()
}
}

// Query:

public function createDeletedFindCondition()
{
foreach ($this->softDeleteAttributeValues as $attribute => $value) {
if (!is_scalar($value) && is_callable($value)) {
$value = call_user_func($value, $this->owner);
}
$attributes[$attribute] = $value;
}

$criteria = new CDbCriteria();
$criteria->addColumnCondition($attributes);

return $criteria;
}

public function createNotDeletedFindCondition()
{
foreach ($this->detectRestoreAttributeValues() as $attribute => $value) {
if (!is_scalar($value) && is_callable($value)) {
$value = call_user_func($value, $this->owner);
}
$attributes[$attribute] = $value;
}

$criteria = new CDbCriteria();
$criteria->addColumnCondition($attributes);

return $criteria;
}

/**
* Filters query to return only 'soft-deleted' records.
*
* @return \CActiveRecord|static query instance.
*/
public function deleted()
{
$this->owner->getDbCriteria()->mergeWith($this->createDeletedFindCondition());

return $this->owner;
}

/**
* Filters query to return only not 'soft-deleted' records.
*
* @return \CActiveRecord|static query instance.
*/
public function notDeleted()
{
$this->owner->getDbCriteria()->mergeWith($this->createNotDeletedFindCondition());

return $this->owner;
}

/**
* Applies `deleted()` or `notDeleted()` to the query regarding passed filter value.
* If an empty value is passed - only not deleted records will be queried.
* If value matching not empty int passed - only deleted records will be queried.
* If not empty value matching int zero passed (e.g. `0`, `'0'`, `'all'`, `false`) - all records will be queried.
*
* @param mixed $deleted filter value.
* @return \CActiveRecord|static
*/
public function filterDeleted($deleted)
{
if ($deleted === '' || $deleted === null || $deleted === []) {
return $this->notDeleted();
}

if ((int) $deleted) {
return $this->deleted();
}

return $this->owner;
}

// Event Handlers:

/**
Expand Down
29 changes: 29 additions & 0 deletions tests/SoftDeleteBehaviorFindTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

namespace yii1tech\ar\softdelete\test;

use yii1tech\ar\softdelete\SoftDeleteBehavior;
use yii1tech\ar\softdelete\test\data\Item;

class SoftDeleteBehaviorFindTest extends TestCase
{
public function testFindCriteria()
{
/** @var Item|SoftDeleteBehavior $query */
$query = Item::model();

$allCount = $query->count();

/** @var Item|SoftDeleteBehavior $item */
$item = Item::model()->findByPk(4);
$item->softDelete();

/** @var Item|SoftDeleteBehavior $query */
$query = Item::model();
$deletedCount = $query->deleted()->count();
$notDeletedCount = $query->notDeleted()->count();

$this->assertNotEquals($allCount, $deletedCount);
$this->assertNotEquals($allCount, $notDeletedCount);
}
}

0 comments on commit d7514d1

Please sign in to comment.