Skip to content

Commit 712d529

Browse files
author
Ivan Vasilkov
committed
Improve AbstractEntity::resetChangedColumns for performance reasons
1 parent 033b678 commit 712d529

File tree

2 files changed

+24
-2
lines changed

2 files changed

+24
-2
lines changed

src/AbstractEntity.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -159,8 +159,16 @@ final public function getOldValue(string $columnName): mixed
159159
return $this->_initialColumns[$columnName] ?? null;
160160
}
161161

162-
final public function resetChangedColumns(): void
162+
/**
163+
* @param array<string, mixed>|null $values An associative array with column names as keys and
164+
* their respective values to reset, or null to reset all columns.
165+
*/
166+
final public function resetChangedColumns(?array $values = null): void
163167
{
164-
$this->_initialColumns = $this->toArray();
168+
if ($values) {
169+
$this->_initialColumns = $values + ($this->_initialColumns ?? []);
170+
} else {
171+
$this->_initialColumns = $this->toArray();
172+
}
165173
}
166174
}

tests/AbstractEntityTest.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -243,4 +243,18 @@ public function __construct(
243243
$expected = str_replace('Array', 'Composite\Entity\AbstractEntity@anonymous Object', $expected);
244244
$this->assertEquals($expected, print_r($entity, true));
245245
}
246+
247+
public function test_resetChangedColumns(): void
248+
{
249+
$entity = new TestSubEntity();
250+
$this->assertEquals($entity->toArray(), $entity->getChangedColumns());
251+
252+
$entity->resetChangedColumns();
253+
$this->assertEquals([], $entity->getChangedColumns());
254+
255+
$entity = new TestSubEntity();
256+
$entity->resetChangedColumns(['str' => $entity->str]);
257+
258+
$this->assertEquals(['number' => $entity->number], $entity->getChangedColumns());
259+
}
246260
}

0 commit comments

Comments
 (0)