Skip to content

Commit 9941216

Browse files
committed
ADD support to Update Matching with values
1 parent 0896c70 commit 9941216

File tree

2 files changed

+32
-0
lines changed

2 files changed

+32
-0
lines changed

src/UpdateQuery.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,17 @@ public static function createUpdate($table, $data = null, ?array $matching = nul
5151
}, ModelHelper::getDataProperties($data, ModelHelper::PROPERTY_SKIP_KEYS));
5252
}
5353

54+
$usedKeys = [];
55+
foreach ($matching as $k => $v) {
56+
if (is_numeric($k)) {
57+
continue;
58+
}
59+
$key = new Column($k, null, $query->getTable()->getAliasOrName());
60+
$query->where()->equals($key, $v);
61+
$usedKeys[$k] = null;
62+
}
63+
$matching = array_diff_key($matching, $usedKeys);
64+
5465
$data = ModelHelper::dataAsArray($data);
5566
foreach ($data as $k => $v) {
5667
if (in_array($k, $matching)) {

test/UpdateQueryTest.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,4 +63,25 @@ public function testCreateUpdateWithClass()
6363
$this->assertTrue(true);
6464
$this->assertEquals($expected, $actual);
6565
}
66+
67+
public function testMatchingValues()
68+
{
69+
$group = json_decode(json_encode([
70+
'group_id' => 3,
71+
'year' => 2023,
72+
'topic' => 'Data Science'
73+
]));
74+
$query = Query::update('groups', $group, ['group_id' => 5, 'year'], ['topic']);
75+
76+
$compiled = $this->compiler->compileUpdate($query);
77+
78+
$this->assertEquals(
79+
'UPDATE groups SET groups.topic = :v1 WHERE groups.group_id = :v2 AND groups.year = :v3',
80+
$compiled->getQuery()
81+
);
82+
$this->assertEquals(
83+
['v1' => 'Data Science', 'v2' => 5, 'v3' => 2023],
84+
$compiled->getValues()
85+
);
86+
}
6687
}

0 commit comments

Comments
 (0)