Skip to content

Commit 55000c3

Browse files
committed
Fix issues of v1.1.2
1 parent 8d1a618 commit 55000c3

File tree

1 file changed

+10
-9
lines changed

1 file changed

+10
-9
lines changed

src/JSONDB/JSONDB.php

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -436,6 +436,8 @@ public function createTable($name, array $prototype)
436436
$ai_exist = TRUE;
437437
$prototype[$field]['unique_key'] = TRUE;
438438
$prototype[$field]['not_null'] = TRUE;
439+
$prototype[$field]['type'] = 'int';
440+
$has_tp = TRUE;
439441
$properties['unique_keys'][] = $field;
440442
}
441443
if ($has_pk) {
@@ -447,7 +449,7 @@ public function createTable($name, array $prototype)
447449
$properties['unique_keys'][] = $field;
448450
}
449451
if ($has_tp) {
450-
if (preg_match('#link\((.+)\)#', $prop['type'], $link)) {
452+
if (array_key_exists('type', $prop) && preg_match('#link\((.+)\)#', $prop['type'], $link)) {
451453
$link_info = explode('.', $link[1]);
452454
$link_table_path = $this->_getTablePath($link_info[0]);
453455
if (!file_exists($link_table_path)) {
@@ -908,7 +910,7 @@ protected function _insert($data)
908910
$array_data = array_diff_key($array_data, $non_pk);
909911
foreach (array_slice($insert, $i + 1) as $value) {
910912
$value = array_diff_key($value, $non_pk);
911-
$pk_error = $pk_error || ($value === $array_data);
913+
$pk_error = $pk_error || (($value === $array_data) && (count($array_data) > 0));
912914
if ($pk_error) {
913915
$values = implode(', ', $value);
914916
$keys = implode(', ', $data['properties']['primary_keys']);
@@ -925,7 +927,7 @@ protected function _insert($data)
925927
$array_data = array_intersect_key($array_data, array($uk => $uk));
926928
foreach (array_slice($insert, $i + 1) as $value) {
927929
$value = array_intersect_key($value, array($uk => $uk));
928-
$uk_error = $uk_error || (!empty($item[$uk]) && ($value === $array_data));
930+
$uk_error = $uk_error || (!empty($value[$uk]) && ($value === $array_data));
929931
if ($uk_error) {
930932
throw new Exception("JSONDB Error: Can't insert value. Duplicate values \"{$value[$uk]}\" for unique key \"{$uk}\".");
931933
}
@@ -1003,11 +1005,10 @@ protected function _replace($data)
10031005
}
10041006

10051007
$i = 0;
1006-
foreach ((array)$current_data as &$array_data) {
1007-
$array_data = array_key_exists($i, $insert) ? array_replace($array_data, $insert[$i]) : $array_data;
1008+
foreach ((array)$current_data as $field => $array_data) {
1009+
$current_data[$field] = array_key_exists($i, $insert) ? array_replace_recursive($array_data, $insert[$i]) : $array_data;
10081010
$i++;
10091011
}
1010-
unset($array_data);
10111012
$insert = $current_data;
10121013

10131014
$pk_error = FALSE;
@@ -1017,7 +1018,7 @@ protected function _replace($data)
10171018
$array = array_diff_key($array, $non_pk);
10181019
foreach (array_slice($insert, $i + 1) as $value) {
10191020
$value = array_diff_key($value, $non_pk);
1020-
$pk_error = $pk_error || ($value === $array);
1021+
$pk_error = $pk_error || (($value === $array) && (count($array) > 0));
10211022
if ($pk_error) {
10221023
$values = implode(', ', $value);
10231024
$keys = implode(', ', $data['properties']['primary_keys']);
@@ -1034,7 +1035,7 @@ protected function _replace($data)
10341035
$array = array_intersect_key($array, array($uk => $uk));
10351036
foreach (array_slice($insert, $i + 1) as $value) {
10361037
$value = array_intersect_key($value, array($uk => $uk));
1037-
$uk_error = $uk_error || (!empty($item[$uk]) && ($value === $array));
1038+
$uk_error = $uk_error || (!empty($value[$uk]) && ($value === $array));
10381039
if ($uk_error) {
10391040
throw new Exception("JSONDB Error: Can't replace value. Duplicate values \"{$value[$uk]}\" for unique key \"{$uk}\".");
10401041
}
@@ -1139,7 +1140,7 @@ protected function _update($data)
11391140
$non_pk = array_flip(array_diff($data['prototype'], $data['properties']['primary_keys']));
11401141
foreach ((array)$data['data'] as $array_data) {
11411142
$array_data = array_diff_key($array_data, $non_pk);
1142-
$pk_error = $pk_error || (array_diff_key($values, $non_pk) === $array_data);
1143+
$pk_error = $pk_error || ((array_diff_key($values, $non_pk) === $array_data) && (count($array_data) > 0));
11431144
if ($pk_error) {
11441145
$v = implode(', ', $array_data);
11451146
$k = implode(', ', $data['properties']['primary_keys']);

0 commit comments

Comments
 (0)