Skip to content

Commit

Permalink
add ability to ignore ID when fetching records
Browse files Browse the repository at this point in the history
  • Loading branch information
lekoala committed Jul 3, 2024
1 parent 13fa2fd commit 7b8c956
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 1 deletion.
12 changes: 11 additions & 1 deletion src/EncryptedDBField.php
Original file line number Diff line number Diff line change
Expand Up @@ -267,9 +267,10 @@ public function fetchDataList($val, $indexSuffix = null)
/**
* @param string $val The unencrypted value
* @param string $indexSuffix The blind index. Defaults to full index
* @param string|array|null $ignoreID Allows to ignore one id or a list of ids
* @return DataObject|false
*/
public function fetchRecord($val, $indexSuffix = null)
public function fetchRecord($val, $indexSuffix = null, $ignoreID = null)
{
if (!$indexSuffix) {
$indexSuffix = self::INDEX_SUFFIX;
Expand All @@ -282,6 +283,15 @@ public function fetchRecord($val, $indexSuffix = null)
$name = $this->name;
/** @var DataObject $record */
foreach ($list as $record) {
if ($ignoreID) {
if (is_array($ignoreID) && in_array($record->ID, $ignoreID)) {
continue;
}
if ($record->ID == $ignoreID) {
continue;
}
}

/** @var EncryptedDBField $obj */
$obj = $record->dbObject($name);
$objValue = $obj->getValue() ?? '';
Expand Down
21 changes: 21 additions & 0 deletions tests/EncryptTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1080,4 +1080,25 @@ public function testNullValue()
}
$this->assertNotEmpty($e);
}

public function testIgnoreID()
{
$model = $this->getTestModel();

/** @var EncryptedDBField $field */
$field = $model->dbObject('MyNumber');

$record = $field->fetchRecord($model->MyNumber);
$this->assertNotEmpty($record);

$record2 = $field->fetchRecord($model->MyNumber, null, $model->ID);
$this->assertNotEquals($model->ID, $record2->ID);
$this->assertNotEquals($record->ID, $record2->ID);

$record3 = $field->fetchRecord($model->MyNumber, null, [$model->ID, $record2->ID]);
$this->assertNotEquals($model->ID, $record3->ID);
$this->assertNotEquals($record->ID, $record3->ID);
$this->assertNotEquals($record2->ID, $record3->ID);
$this->assertEquals($model->MyNumber, $record3->MyNumber);
}
}

0 comments on commit 7b8c956

Please sign in to comment.