Overview
Comment:Fix Module data storing
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | dev
Files: files | file ages | folders
SHA3-256: 1bcf77a0caf4602794837eec3c31e551e1a80f65df4335bdaa778c2ff2918de7
User & Date: alinaar on 2023-01-20 09:42:49
Other Links: branch diff | manifest | tags
Context
2023-01-20
14:33
Use same return type for both config and custom table check-in: dde29f34a7 user: bohwaz tags: dev
10:24
Merge dev branch check-in: bfde055198 user: alinaar tags: invoice_module
09:42
Fix Module data storing check-in: 1bcf77a0ca user: alinaar tags: dev
2023-01-19
22:11
Make sure the category is set when creating a user entity check-in: a623fa51f5 user: bohwaz tags: dev
Changes

Modified src/include/lib/Garradin/UserTemplate/Functions.php from [1950c123ec] to [16bb6c161b].

115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
			$validate = $params['validate_schema'];
			unset($params['validate_schema']);
		}

		$db = DB::getInstance();

		if ($key == 'config') {
			$exists = $db->firstColumn(sprintf('SELECT config AS value FROM %s WHERE name = ?;', Module::TABLE), $name);
		}
		else {
			$db->exec(sprintf('
				CREATE TABLE IF NOT EXISTS %s (
					id INTEGER NOT NULL PRIMARY KEY,
					key TEXT NULL,
					value TEXT NOT NULL
				);
				CREATE UNIQUE INDEX IF NOT EXISTS %1$s_key ON %1$s (key);', $table));

			$exists = $db->first(sprintf('SELECT value FROM %s WHERE %s;', $table, ($field . ' = ?')), $where_value);
		}

		// Merge before update
		if ($exists) {
			$exists = json_decode((string) $exists->value, true);
			$params = array_merge($exists, $params);
		}

		// Remove NULL values
		$params = array_filter($params, fn($a) => !is_null($a));

		if ($validate) {
			$schema = self::read(['file' => $validate], $tpl, $line);







|










|



|
|
|







115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
			$validate = $params['validate_schema'];
			unset($params['validate_schema']);
		}

		$db = DB::getInstance();

		if ($key == 'config') {
			$result = $db->firstColumn(sprintf('SELECT config AS value FROM %s WHERE name = ?;', Module::TABLE), $name);
		}
		else {
			$db->exec(sprintf('
				CREATE TABLE IF NOT EXISTS %s (
					id INTEGER NOT NULL PRIMARY KEY,
					key TEXT NULL,
					value TEXT NOT NULL
				);
				CREATE UNIQUE INDEX IF NOT EXISTS %1$s_key ON %1$s (key);', $table));

			$result = $db->first(sprintf('SELECT value FROM %s WHERE %s;', $table, ($field . ' = ?')), $where_value);
		}

		// Merge before update
		if ($result) {
			$result = json_decode(is_string($result) ? $result : ((string) $result->value), true);
			$params = array_merge($result, $params);
		}

		// Remove NULL values
		$params = array_filter($params, fn($a) => !is_null($a));

		if ($validate) {
			$schema = self::read(['file' => $validate], $tpl, $line);
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
		$value = json_encode($params);

		if ($key == 'config') {
			$db->update(Module::TABLE, ['config' => $value], 'name = :name', compact('name'));
			return;
		}

		if (!$exists) {
			$db->insert($table, compact('value', 'key'));
		}
		else {
			$db->update($table, compact('value'), sprintf('%s = :match', $field), ['match' => $where_value]);
		}
	}








|







158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
		$value = json_encode($params);

		if ($key == 'config') {
			$db->update(Module::TABLE, ['config' => $value], 'name = :name', compact('name'));
			return;
		}

		if (!$result) {
			$db->insert($table, compact('value', 'key'));
		}
		else {
			$db->update($table, compact('value'), sprintf('%s = :match', $field), ['match' => $where_value]);
		}
	}