Skip to content

Commit fdd90d1

Browse files
LainowstonebuzzRom1-B
authored
Fix sql data cannot be null in query (#492)
* Fix sql data cannot be null in query * Update changelog * Update inc/commoninjectionlib.class.php Co-authored-by: Stanislas <[email protected]> * Update inc/commoninjectionlib.class.php Co-authored-by: Romain B. <[email protected]> * Add notnull_value array * Use reformat function * Update inc/commoninjectionlib.class.php Co-authored-by: Romain B. <[email protected]> --------- Co-authored-by: Stanislas <[email protected]> Co-authored-by: Romain B. <[email protected]>
1 parent b51ffb2 commit fdd90d1

File tree

3 files changed

+65
-16
lines changed

3 files changed

+65
-16
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
1414
- Fix injection of `values in entity tabs` when injecting an `entity`
1515
- Fix `pdffont` field error for users
1616
- Move `Notepads` search options to the Black List option
17+
- Fix the SQL error: `Column ‘...’ cannot be null in the query`
1718

1819

1920
### Added

inc/commoninjectionlib.class.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -955,7 +955,13 @@ private function setValueForItemtype($itemtype, $field, $value, $fromdb = false)
955955
}
956956
} else { // First value
957957
if (empty($value)) {
958-
$this->values[$itemtype][$field] = "NULL";
958+
if (isForeignKeyField($field) || (strpos($field, 'is_') !== false)) {
959+
// If the field is an id, we set it to 0
960+
$this->values[$itemtype][$field] = self::DROPDOWN_EMPTY_VALUE;
961+
} else {
962+
// Else we set it to NULL
963+
$this->values[$itemtype][$field] = self::EMPTY_VALUE;
964+
}
959965
} else {
960966
$this->values[$itemtype][$field] = $value;
961967
}

inc/networkequipmentinjection.class.php

Lines changed: 57 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -57,17 +57,17 @@ public function connectedTo()
5757

5858

5959
/**
60-
* @see plugins/datainjection/inc/PluginDatainjectionInjectionInterface::getOptions()
61-
**/
60+
* @see plugins/datainjection/inc/PluginDatainjectionInjectionInterface::getOptions()
61+
**/
6262
public function getOptions($primary_type = '')
6363
{
6464

6565
$tab = Search::getOptions(get_parent_class($this));
6666

67-
//Specific to location
67+
//Specific to location
6868
$tab[3]['linkfield'] = 'locations_id';
6969

70-
//Virtual type : need to be processed at the end !
70+
//Virtual type : need to be processed at the end !
7171
$tab[200]['table'] = 'glpi_networkequipments';
7272
$tab[200]['field'] = 'nb_ports';
7373
$tab[200]['name'] = __('Number of ports', 'datainjection');
@@ -76,17 +76,38 @@ public function getOptions($primary_type = '')
7676
$tab[200]['linkfield'] = 'nb_ports';
7777
$tab[200]['injectable'] = PluginDatainjectionCommonInjectionLib::FIELD_VIRTUAL;
7878

79-
//Remove some options because some fields cannot be imported
79+
//Remove some options because some fields cannot be imported
8080
$blacklist = PluginDatainjectionCommonInjectionLib::getBlacklistedOptions(get_parent_class($this));
8181
$notimportable = [
82-
41, 43, 44, 45, 46, 48, 61, 63, 64, 91, 92, 93
82+
41,
83+
43,
84+
44,
85+
45,
86+
46,
87+
48,
88+
61,
89+
63,
90+
64,
91+
91,
92+
92,
93+
93
8394
];
8495

8596
$options['ignore_fields'] = array_merge($blacklist, $notimportable);
8697

87-
$options['displaytype'] = ["dropdown" => [3, 4, 11, 23, 31, 32, 33,
88-
40, 49, 71
89-
],
98+
$options['displaytype'] = [
99+
"dropdown" => [
100+
3,
101+
4,
102+
11,
103+
23,
104+
31,
105+
32,
106+
33,
107+
40,
108+
49,
109+
71
110+
],
90111
"bool" => [86],
91112
"user" => [24, 70],
92113
"multiline_text" => [16, 90]
@@ -97,8 +118,8 @@ public function getOptions($primary_type = '')
97118

98119

99120
/**
100-
* @see plugins/datainjection/inc/PluginDatainjectionInjectionInterface::addOrUpdateObject()
101-
**/
121+
* @see plugins/datainjection/inc/PluginDatainjectionInjectionInterface::addOrUpdateObject()
122+
**/
102123
public function addOrUpdateObject($values = [], $options = [])
103124
{
104125

@@ -109,10 +130,10 @@ public function addOrUpdateObject($values = [], $options = [])
109130

110131

111132
/**
112-
* @param array $values
113-
* @param boolean $add (true by default)
114-
* @param array|null $rights array
115-
*/
133+
* @param array $values
134+
* @param boolean $add (true by default)
135+
* @param array|null $rights array
136+
*/
116137
public function processAfterInsertOrUpdate($values, $add = true, $rights = [])
117138
{
118139

@@ -135,4 +156,25 @@ public function processAfterInsertOrUpdate($values, $add = true, $rights = [])
135156
}
136157
}
137158
}
159+
160+
/**
161+
* @param array $values array
162+
**/
163+
public function reformat(&$values = [])
164+
{
165+
166+
foreach (
167+
['cpu'] as $int
168+
) {
169+
if (
170+
isset($values['NetworkEquipment'][$int])
171+
&& (
172+
$values['NetworkEquipment'][$int] == PluginDatainjectionCommonInjectionLib::EMPTY_VALUE
173+
|| $values['NetworkEquipment'][$int] === 'NULL'
174+
)
175+
) {
176+
$values['NetworkEquipment'][$int] = 0;
177+
}
178+
}
179+
}
138180
}

0 commit comments

Comments
 (0)