1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Sludio\HelperBundle\Script\Repository; |
4
|
|
|
|
5
|
|
|
use Sludio\HelperBundle\Script\Utils\Helper; |
6
|
|
|
|
7
|
|
|
class QuickInsertRepository extends QuickInsertFunctions |
8
|
|
|
{ |
9
|
|
|
public static function findNextIdExt($object, $entityManager = null) |
10
|
|
|
{ |
11
|
|
|
self::init(); |
12
|
|
|
$data = self::extractExt($object, $entityManager); |
13
|
|
|
|
14
|
|
|
return self::findNextId($data['table']); |
15
|
|
|
} |
16
|
|
|
|
17
|
|
|
public static function findNextId($tableName) |
18
|
|
|
{ |
19
|
|
|
return self::get(['table_name' => 'information_schema.tables'], true, [ |
20
|
|
|
'table_name' => $tableName, |
21
|
|
|
['table_schema = DATABASE()'], |
22
|
|
|
], true, ['AUTO_INCREMENT'], null, []) ?: 1; |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
public static function setFK($fkCheck = 0, $noFkCheck = false) |
26
|
|
|
{ |
27
|
|
|
if (!$noFkCheck) { |
28
|
|
|
self::runSQL("SET FOREIGN_KEY_CHECKS = $fkCheck", false, null, true); |
29
|
|
|
} |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
public static function runSQL($sql, $noFkCheck = true, $manager = null, $skip = false) |
33
|
|
|
{ |
34
|
|
|
$sql = trim(preg_replace('/\s+/', ' ', $sql)); |
35
|
|
|
self::init($manager); |
36
|
|
|
if (!$skip) { |
37
|
|
|
self::setFK(0, $noFkCheck); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
$sth = self::$connection->prepare($sql); |
41
|
|
|
$sth->execute(); |
42
|
|
|
|
43
|
|
|
if (!$skip) { |
44
|
|
|
self::setFK(1, $noFkCheck); |
45
|
|
|
} |
46
|
|
|
if (substr($sql, 0, 6) === "SELECT") { |
47
|
|
|
return $sth->fetchAll(); |
48
|
|
|
} |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
public static function get($object, $one = false, $where = [], $noFkCheck = true, $fields = [], $manager = null, $extra = []) |
52
|
|
|
{ |
53
|
|
|
self::getTable($object, $tableName, $columns, $type, $manager); |
54
|
|
|
|
55
|
|
|
$select = (isset($extra['MODE']) ? 'SELECT '.$extra['MODE'] : 'SELECT').' '; |
56
|
|
|
$fields = $fields ?: ['id']; |
57
|
|
|
$sql = $select.(implode(', ', $fields)).' FROM '.$tableName.self::buildWhere($tableName, $where).self::buildExtra($extra); |
58
|
|
|
|
59
|
|
|
$result = self::runSQL($sql) ?: null; |
60
|
|
|
|
61
|
|
|
if ($result) { |
62
|
|
|
$field = null; |
63
|
|
|
if (count($fields) === 1 && $fields[0] !== '*') { |
64
|
|
|
$field = $fields[0]; |
65
|
|
|
} |
66
|
|
|
if ($field) { |
67
|
|
|
if (!$one) { |
68
|
|
|
foreach ($result as &$res) { |
69
|
|
|
$res = $res[$field]; |
70
|
|
|
} |
71
|
|
|
} else { |
72
|
|
|
$result = $result[0][$field]; |
73
|
|
|
} |
74
|
|
|
} elseif ($one) { |
75
|
|
|
$result = $result[0]; |
76
|
|
|
} |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
return $result; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
public static function persist($object, $full = false, $extraFields = [], $noFkCheck = false, $manager = null) |
83
|
|
|
{ |
84
|
|
|
self::getTable($object, $tableName, $columns, $type, $manager, $extraFields); |
85
|
|
|
|
86
|
|
|
$id = self::findNextId($tableName); |
87
|
|
|
$data = []; |
88
|
|
|
|
89
|
|
|
$idd = null; |
90
|
|
|
foreach ($columns as $value => $key) { |
91
|
|
|
if (!is_array($key) && !is_array($value)) { |
92
|
|
|
$value = self::value($object, $value, $type); |
93
|
|
|
if ($value !== null) { |
94
|
|
|
$data[$key] = $value; |
95
|
|
|
if ($key === self::$identifier) { |
96
|
|
|
$idd = $value; |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
if (!$full) { |
103
|
|
|
$data[self::$identifier] = $id; |
104
|
|
|
} else { |
105
|
|
|
$id = $idd; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
if (Helper::isEmpty($data) && $id !== null) { |
109
|
|
|
return null; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
$sql = ' |
113
|
|
|
INSERT INTO |
114
|
|
|
'.$tableName.' |
115
|
|
|
('.implode(',', array_keys($data)).') |
116
|
|
|
VALUES |
117
|
|
|
('.implode(',', array_values($data)).') |
118
|
|
|
'; |
119
|
|
|
|
120
|
|
|
self::runSQL($sql, $noFkCheck); |
121
|
|
|
|
122
|
|
|
return $id; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
public static function update($id, $object, $extraFields = [], $noFkCheck = false, $manager = null) |
126
|
|
|
{ |
127
|
|
|
self::getTable($object, $tableName, $columns, $type, $manager, $extraFields); |
128
|
|
|
|
129
|
|
|
$result = self::get(['table_name' => $tableName], true, ['id' => $id], true, ['*']); |
130
|
|
|
unset($result['id']); |
131
|
|
|
$data = []; |
132
|
|
|
|
133
|
|
|
$flip = array_flip($columns); |
134
|
|
|
foreach ($result as $key => $value) { |
135
|
|
|
$content = self::value($object, $key, $type, false); |
136
|
|
|
if ($content !== $value) { |
137
|
|
|
$data[$key] = $content; |
138
|
|
|
} |
139
|
|
|
if (!$id && $content === null) { |
140
|
|
|
unset($data[$key]); |
141
|
|
|
} |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
if ($data) { |
145
|
|
|
$sql = ' |
146
|
|
|
UPDATE |
147
|
|
|
'.$tableName.' |
148
|
|
|
SET |
149
|
|
|
|
150
|
|
|
'; |
151
|
|
|
foreach ($data as $key => $value) { |
152
|
|
|
$meta = self::$metadata[$tableName]->getFieldMapping($flip[$key])['type']; |
153
|
|
|
if (in_array($meta, [ |
154
|
|
|
'boolean', |
155
|
|
|
'integer', |
156
|
|
|
'longint', |
157
|
|
|
])) { |
158
|
|
|
$value = intval($value); |
159
|
|
|
} else { |
160
|
|
|
$value = "'".addslashes(trim($value))."'"; |
161
|
|
|
} |
162
|
|
|
$sql .= " ".$key." = ".$value.","; |
163
|
|
|
} |
164
|
|
|
$sql = substr($sql, 0, -1).' WHERE id = '.$id; |
165
|
|
|
|
166
|
|
|
self::runSQL($sql, $noFkCheck); |
167
|
|
|
} |
168
|
|
|
} |
169
|
|
|
|
170
|
|
View Code Duplication |
public static function delete($object, $where = [], $noFkCheck = false, $manager = null) |
|
|
|
|
171
|
|
|
{ |
172
|
|
|
self::getTable($object, $tableName, $columns, $type, $manager); |
173
|
|
|
|
174
|
|
|
$sql = 'DELETE FROM '.$tableName.self::buildWhere($tableName, $where); |
175
|
|
|
self::runSQL($sql, $noFkCheck); |
176
|
|
|
} |
177
|
|
|
|
178
|
|
View Code Duplication |
public static function link($object, $data, $noFkCheck = false, $manager = null) |
|
|
|
|
179
|
|
|
{ |
180
|
|
|
self::getTable($object, $tableName, $columns, $type, $manager); |
181
|
|
|
|
182
|
|
|
$data['table_name'] = $tableName; |
183
|
|
|
self::persist($data, true, [], $noFkCheck, $manager); |
184
|
|
|
} |
185
|
|
|
} |
186
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.