1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Sludio\HelperBundle\Script\Repository; |
4
|
|
|
|
5
|
|
|
use Sludio\HelperBundle\Script\Utils\Helper; |
6
|
|
|
|
7
|
|
|
abstract class QuickInsertFunctions |
8
|
|
|
{ |
9
|
|
|
protected static $mock = []; |
10
|
|
|
protected static $metadata = []; |
11
|
|
|
protected static $tableName; |
12
|
|
|
protected static $identifier; |
13
|
|
|
|
14
|
|
|
public static $entityManager; |
15
|
|
|
public static $connection; |
16
|
|
|
|
17
|
|
|
public static function init($manager = null) |
18
|
|
|
{ |
19
|
|
|
if (self::$connection) { |
20
|
|
|
return; |
21
|
|
|
} |
22
|
|
|
global $kernel; |
23
|
|
|
|
24
|
|
|
if ('AppCache' === get_class($kernel)) { |
25
|
|
|
$kernel = $kernel->getKernel(); |
26
|
|
|
} |
27
|
|
|
$container = $kernel->getContainer(); |
28
|
|
|
|
29
|
|
|
$manager = $manager ?: $container->getParameter('sludio_helper.entity.manager'); |
30
|
|
|
self::$entityManager = $container->get('doctrine')->getManager($manager); |
31
|
|
|
self::$connection = self::$entityManager->getConnection(); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
protected static function extract($object) |
35
|
|
|
{ |
36
|
|
|
self::init(false); |
37
|
|
|
$data = self::extractExt($object, self::$entityManager); |
38
|
|
|
|
39
|
|
|
self::$mock = $data['mock']; |
40
|
|
|
self::$tableName = $data['table']; |
41
|
|
|
self::$metadata[$data['table']] = $data['meta']; |
42
|
|
|
self::$identifier = $data['identifier']; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
public static function extractExt($object, $entityManager) |
46
|
|
|
{ |
47
|
|
|
$metadata = $entityManager->getClassMetadata(get_class($object)); |
48
|
|
|
|
49
|
|
|
$fields = $metadata->getFieldNames(); |
50
|
|
|
$columns = $metadata->getColumnNames(); |
51
|
|
|
$table = $metadata->getTableName(); |
52
|
|
|
$identifier = null; |
53
|
|
|
|
54
|
|
|
$result = []; |
55
|
|
|
foreach ($fields as $key => $field) { |
56
|
|
|
foreach ($columns as $key2 => $column) { |
57
|
|
|
if ($key === $key2) { |
58
|
|
|
$result[$table][$field] = $column; |
59
|
|
|
if ($field === $metadata->getIdentifier()[0]) { |
60
|
|
|
$identifier = $column; |
61
|
|
|
} |
62
|
|
|
} |
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
$data = [ |
67
|
|
|
'mock' => $result, |
68
|
|
|
'table' => $table, |
69
|
|
|
'meta' => $metadata, |
70
|
|
|
'identifier' => $identifier, |
71
|
|
|
]; |
72
|
|
|
|
73
|
|
|
return $data; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
protected static function buildExtra($extra) |
77
|
|
|
{ |
78
|
|
|
$methods = [ |
79
|
|
|
'GROUP BY', |
80
|
|
|
'HAVING', |
81
|
|
|
'ORDER BY', |
82
|
|
|
]; |
83
|
|
|
$sql = ''; |
84
|
|
|
|
85
|
|
|
foreach ($methods as $method) { |
86
|
|
|
if (isset($extra[$method])) { |
87
|
|
|
$sql .= ' '.$method.' '; |
88
|
|
|
if (is_array($extra[$method])) { |
89
|
|
|
foreach ($extra[$method] as $group) { |
90
|
|
|
$sql .= $group.' '; |
91
|
|
|
} |
92
|
|
|
} else { |
93
|
|
|
$sql .= $extra[$method].' '; |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
if (isset($extra['LIMIT'])) { |
99
|
|
|
if (is_array($extra['LIMIT'])) { |
100
|
|
|
if (isset($extra['LIMIT'][1])) { |
101
|
|
|
$offset = $extra['LIMIT'][0]; |
102
|
|
|
$limit = $extra['LIMIT'][1]; |
103
|
|
|
} else { |
104
|
|
|
$offset = 0; |
105
|
|
|
$limit = $extra['LIMIT'][0]; |
106
|
|
|
} |
107
|
|
|
$sql .= 'LIMIT '.$offset.', '.$limit; |
108
|
|
|
} |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
$sql = str_replace(' ', ' ', $sql); |
112
|
|
|
|
113
|
|
|
return $sql; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
protected static function buildWhere($tableName, $where) |
117
|
|
|
{ |
118
|
|
|
$whereSql = ''; |
119
|
|
|
if (is_array($where) && !empty($where)) { |
120
|
|
|
reset($where); |
121
|
|
|
$first = key($where); |
122
|
|
|
$path = ' WHERE '; |
123
|
|
|
foreach ($where as $key => $value) { |
124
|
|
|
if (!is_array($value) && isset(self::$mock[$tableName][$key])) { |
125
|
|
|
$whereSql .= $path.self::$mock[$tableName][$key]." = ".(is_numeric($value) ? $value : "'".addslashes(trim($value))."'"); |
126
|
|
|
} else { |
127
|
|
|
if (is_array($value)) { |
128
|
|
|
$whereSql .= $path.$value[0]; |
129
|
|
|
} else { |
130
|
|
|
$whereSql .= $path.$key." = ".(is_numeric($value) ? $value : "'".addslashes(trim($value))."'"); |
131
|
|
|
} |
132
|
|
|
} |
133
|
|
|
if ($key === $first) { |
134
|
|
|
$path = ' AND '; |
135
|
|
|
} |
136
|
|
|
} |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
return $whereSql; |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
protected static function getTable(&$object, &$tableName, &$columns, &$type, $manager = null, $extraFields = []) |
143
|
|
|
{ |
144
|
|
|
self::init($manager); |
145
|
|
|
if (is_object($object)) { |
146
|
|
|
self::extract($object); |
147
|
|
|
$tableName = self::$tableName; |
148
|
|
|
$columns = self::$mock[$tableName] ?: []; |
149
|
|
|
$type = 'object'; |
150
|
|
|
} else { |
151
|
|
|
$tableName = $object['table_name']; |
152
|
|
|
unset($object['table_name']); |
153
|
|
|
$type = 'table'; |
154
|
|
|
$columns = array_keys($object) ?: []; |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
if (isset($extraFields[$tableName])) { |
158
|
|
|
$columns = array_merge($columns, $extraFields[$tableName]); |
159
|
|
|
} |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
protected static function value($object, $variable, $type, $check = true) |
163
|
|
|
{ |
164
|
|
|
$value = null; |
165
|
|
|
if ($type === 'object') { |
166
|
|
|
$value = $object->{'get'.ucfirst(Helper::toCamelCase($variable))}(); |
167
|
|
|
} else { |
168
|
|
|
if (isset($object[$variable])) { |
169
|
|
|
$value = $object[$variable]; |
170
|
|
|
} |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
if ($check) { |
174
|
|
|
Helper::variable($value); |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
return $value; |
178
|
|
|
} |
179
|
|
|
} |