Skip to content

Commit 98f1f09

Browse files
committed
Enable filtered column persist via AbstractMapper::filterColumns()
Apply filterColumns() in extractColumns() so Filtered collections generate partial INSERT/UPDATE statements with only the listed columns plus the primary key.
1 parent 094f84d commit 98f1f09

2 files changed

Lines changed: 31 additions & 1 deletion

File tree

src/Mapper.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -261,7 +261,7 @@ private function extractColumns(object $entity, Collection $collection): array
261261
$c = $this->entityFactory->get($c, $primaryName);
262262
}
263263

264-
return $cols;
264+
return $this->filterColumns($cols, $collection);
265265
}
266266

267267
/** @param array<string, Collection> $collections */

tests/MapperTest.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1205,6 +1205,36 @@ public function testFetchReturnsDbInstance(): void
12051205
$this->assertInstanceOf(Db::class, $mapper->db);
12061206
}
12071207

1208+
public function testFilteredPersistUpdatesOnlyFilteredColumns(): void
1209+
{
1210+
$mapper = $this->mapper;
1211+
$mapper->postTitles = Filtered::post('title');
1212+
$post = $mapper->postTitles()->fetch();
1213+
$this->assertEquals('Post Title', $post->title);
1214+
1215+
$post->title = 'Changed Title';
1216+
$mapper->postTitles()->persist($post);
1217+
$mapper->flush();
1218+
1219+
$row = $this->query('select * from post where id=5')->fetch(PDO::FETCH_ASSOC);
1220+
$this->assertEquals('Changed Title', $row['title']);
1221+
$this->assertEquals('Post Text', $row['text'], 'Non-filtered columns should remain unchanged');
1222+
$this->assertEquals(1, $row['author_id'], 'Non-filtered columns should remain unchanged');
1223+
}
1224+
1225+
public function testFilteredPersistInsertsOnlyFilteredColumns(): void
1226+
{
1227+
$mapper = $this->mapper;
1228+
$mapper->postTitles = Filtered::post('title');
1229+
$post = (object) ['id' => 99, 'title' => 'Partial Post', 'text' => 'Should not appear'];
1230+
$mapper->postTitles()->persist($post);
1231+
$mapper->flush();
1232+
1233+
$row = $this->query('select * from post where id=99')->fetch(PDO::FETCH_ASSOC);
1234+
$this->assertEquals('Partial Post', $row['title']);
1235+
$this->assertNull($row['text'], 'Non-filtered columns should not be inserted');
1236+
}
1237+
12081238
private function query(string $sql): PDOStatement
12091239
{
12101240
$stmt = $this->conn->query($sql);

0 commit comments

Comments
 (0)