-
Notifications
You must be signed in to change notification settings - Fork 29
Expand file tree
/
Copy pathPHPLaravelDBDataWrapper.php
More file actions
executable file
·106 lines (85 loc) · 2.6 KB
/
PHPLaravelDBDataWrapper.php
File metadata and controls
executable file
·106 lines (85 loc) · 2.6 KB
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
<?php
namespace Dhtmlx\Connector\DataStorage;
use Dhtmlx\Connector\DataProcessor\DataProcessor;
use \Exception;
class PHPLaravelDBDataWrapper extends ArrayDBDataWrapper {
public function select($source) {
$sourceData = $source->get_source();
if(is_array($sourceData)) //result of find
$res = $sourceData;
else if ($sourceData){
if (is_string($sourceData))
$sourceData = new $sourceData();
if (is_a($sourceData, "Illuminate\\Database\\Eloquent\\Collection")){
$res = $sourceData->toArray();
} else if (is_a($sourceData, "Illuminate\\Database\\Eloquent\\Builder")){
$res = $sourceData->get();
} else {
$res = $sourceData->all();
}
}
return new ArrayQueryWrapper($res);
}
protected function getErrorMessage() {
$errors = $this->connection->getErrors();
$text = array();
foreach($errors as $key => $value)
$text[] = $key." - ".$value[0];
return implode("\n", $text);
}
public function insert($data, $source) {
$obj = null;
if(method_exists($source->get_source(), 'getModel')){
$obj = $source->get_source()->getModel()->newInstance();
} else {
$className = get_class($source->get_source());
$obj = new $className();
}
$this->fill_model($obj, $data)->save();
$fieldPrimaryKey = $this->config->id["db_name"];
$data->success($obj->$fieldPrimaryKey);
}
public function delete($data, $source) {
if(method_exists($source->get_source(), 'getModel')){
$source->get_source()->getModel()->find($data->get_id())->delete();
} else {
$className = get_class($source->get_source());
$className::destroy($data->get_id());
}
$data->success();
}
public function update($data, $source) {
$obj = null;
if(method_exists($source->get_source(), 'getModel')){
$obj = $source->get_source()->getModel()->find($data->get_id());
} else {
$className = get_class($source->get_source());
$obj = $className::find($data->get_id());
}
$this->fill_model($obj, $data)->save();
$data->success();
}
private function fill_model($obj, $data) {
$dataArray = $data->get_data();
unset($dataArray[DataProcessor::$action_param]);
unset($dataArray[$this->config->id["db_name"]]);
foreach($dataArray as $key => $value)
$obj->$key = $value;
return $obj;
}
protected function errors_to_string($errors) {
$text = array();
foreach($errors as $value)
$text[] = implode("\n", $value);
return implode("\n",$text);
}
public function escape($str) {
throw new Exception("Not implemented");
}
public function query($str) {
throw new Exception("Not implemented");
}
public function get_new_id() {
throw new Exception("Not implemented");
}
}