-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathDBJson.php
81 lines (66 loc) · 2.05 KB
/
DBJson.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
<?php
namespace SilverStripe\LinkField\ORM;
use SilverStripe\Core\Config\Config;
use SilverStripe\LinkField\JsonData;
use SilverStripe\ORM\DB;
use SilverStripe\ORM\FieldType\DBField;
use SilverStripe\Dev\Deprecation;
/**
* Represents a DBField storing a JSON string
*
* @deprecated 3.0.0 Will be removed without equivalent functionality to replace it
*/
class DBJson extends DBField
{
public function __construct($name = null, $defaultVal = [])
{
Deprecation::withNoReplacement(function () {
Deprecation::notice('3.0.0', 'Will be removed without equivalent functionality to replace it', Deprecation::SCOPE_CLASS);
});
$this->defaultVal = is_array($defaultVal) ? $defaultVal : [];
parent::__construct($name);
}
public function requireField()
{
$charset = Config::inst()->get('SilverStripe\ORM\Connect\MySQLDatabase', 'charset');
$collation = Config::inst()->get('SilverStripe\ORM\Connect\MySQLDatabase', 'collation');
$parts = [
'datatype' => 'mediumtext',
'character set' => $charset,
'collate' => $collation,
'arrayValue' => $this->arrayValue
];
$values = [
'type' => 'text',
'parts' => $parts
];
DB::require_field($this->tableName, $this->name, $values);
}
public function nullValue()
{
return null;
}
public function setValue($value, $record = null, $markChanged = true)
{
if (!$value) {
$value = null;
}
if (is_string($value)) {
$value = json_decode($value, true);
} elseif ($value instanceof JsonData) {
$value = $value->jsonSerialize();
}
return parent::setValue($value, $record, $markChanged);
}
public function prepValueForDB($value)
{
if (is_array($value) || $value instanceof JsonData) {
$value = json_encode($value);
}
return $value;
}
public function scalarValueOnly()
{
return false;
}
}