Skip to content

Commit 993f8e3

Browse files
authored
Merge pull request #2 from Bit-Apps-Pro/database-null-issue
fix: prepare null convert to empty string issue
2 parents be9e80c + 444dd04 commit 993f8e3

File tree

1 file changed

+28
-4
lines changed

1 file changed

+28
-4
lines changed

src/QueryBuilder.php

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -998,6 +998,7 @@ public function prepare($sql = null)
998998
return empty($this->bindings)
999999
|| strpos($sql, '%') === false
10001000
? $sql : Connection::prepare($sql, $this->bindings);
1001+
10011002
}
10021003

10031004
/**
@@ -1309,6 +1310,12 @@ private function bulkInsert($attributes)
13091310
', ',
13101311
array_map(
13111312
function ($value) {
1313+
1314+
if (\is_null($value)) {
1315+
return 'NULL';
1316+
}
1317+
1318+
13121319
$this->bindings[] = $value;
13131320

13141321
return $this->getValueType($value);
@@ -1451,7 +1458,9 @@ private function prepareAttributeForSaveOrUpdate($isUpdate = false)
14511458
$this->bindings[] = \is_array($this->_model->{$column})
14521459
|| \is_object($this->_model->{$column})
14531460
? wp_json_encode($this->_model->{$column}) : $this->_model->{$column};
1454-
} else {
1461+
} elseif (\is_null($this->_model->{$column})) {
1462+
$this->bindings[] = null;
1463+
}else {
14551464
$this->bindings[] = '';
14561465
}
14571466
}
@@ -1495,10 +1504,17 @@ private function prepareInsert()
14951504
. implode(
14961505
', ',
14971506
array_map(
1498-
function ($value) {
1507+
function ($value, $key) {
1508+
if (\is_null($value)) {
1509+
unset($this->bindings[$key]);
1510+
1511+
return 'NULL';
1512+
}
1513+
14991514
return $this->getValueType($value);
15001515
},
1501-
$this->bindings
1516+
$this->bindings,
1517+
array_keys($this->bindings)
15021518
)
15031519
) . ')';
15041520

@@ -1517,7 +1533,15 @@ private function prepareUpdate()
15171533
$sql .= ' SET ';
15181534
$columnCount = \count($this->update);
15191535
foreach ($this->update as $key => $column) {
1520-
$sql .= $column . ' = ' . $this->getValueType($this->bindings[$key]);
1536+
// $sql .= $column . ' = ' . $this->getValueType($this->bindings[$key]);
1537+
1538+
if (\is_null($this->bindings[$key])) {
1539+
$sql .= $column . ' = NULL';
1540+
unset($this->bindings[$key]);
1541+
} else {
1542+
$sql .= $column . ' = ' . $this->getValueType($this->bindings[$key]);
1543+
}
1544+
15211545
if ($key < $columnCount - 1) {
15221546
$sql .= ', ';
15231547
}

0 commit comments

Comments
 (0)