1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace HnrAzevedo\Datamanager; |
4
|
|
|
|
5
|
|
|
use HnrAzevedo\Datamanager\DatamanagerException; |
6
|
|
|
|
7
|
|
|
trait EntityTrait{ |
8
|
|
|
use CheckTrait; |
9
|
|
|
|
10
|
|
|
protected string $lastQuery = ''; |
11
|
|
|
protected array $lastData = []; |
12
|
|
|
|
13
|
|
|
public function toEntity() |
14
|
|
|
{ |
15
|
|
|
if($this->getCount() === 0){ |
16
|
|
|
return null; |
17
|
|
|
} |
18
|
|
|
|
19
|
|
|
$entity = $this->setByDatabase($this->result[0]); |
20
|
|
|
|
21
|
|
|
if(count($this->result) > 1){ |
22
|
|
|
$entity = []; |
23
|
|
|
foreach ($this->result as $key => $value) { |
24
|
|
|
$entity[] = $this->setByDatabase($value); |
25
|
|
|
} |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
return $entity; |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
public function persist() |
32
|
|
|
{ |
33
|
|
|
$columns = ''; |
34
|
|
|
$values = ''; |
35
|
|
|
$data = []; |
36
|
|
|
|
37
|
|
|
foreach ($this->data as $key => $value) { |
38
|
|
|
if(strstr($this->data[$key]['extra'],'auto_increment')){ |
39
|
|
|
continue; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
$this->checkMaxlength($key, $value['value'], $value['maxlength']); |
43
|
|
|
|
44
|
|
|
$columns .= $key.','; |
45
|
|
|
$values .= ':'.$key.','; |
46
|
|
|
$data[$key] = $value['value']; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
$this->transaction('begin'); |
50
|
|
|
try{ |
51
|
|
|
|
52
|
|
|
$this->checkUniques($data); |
53
|
|
|
|
54
|
|
|
$id = $this->insert($data); |
55
|
|
|
|
56
|
|
|
$this->check_fail(); |
57
|
|
|
|
58
|
|
|
$primary = $this->primary; |
59
|
|
|
|
60
|
|
|
$this->$primary = $id; |
61
|
|
|
|
62
|
|
|
$this->transaction('commit'); |
63
|
|
|
|
64
|
|
|
}catch(DatamanagerException $er){ |
65
|
|
|
$this->transaction('rollback'); |
66
|
|
|
throw $er; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
return $this; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
public function remove(bool $exec = false) |
73
|
|
|
{ |
74
|
|
|
if(!$exec){ |
75
|
|
|
$this->clause = 'remove'; |
76
|
|
|
return $this; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
$this->clause = null; |
80
|
|
|
|
81
|
|
|
if(count($this->where) == 1){ |
82
|
|
|
$this->removeById(); |
83
|
|
|
return $this; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
$this->delete( |
87
|
|
|
$this->mountRemove()['where'], |
88
|
|
|
substr( $this->mountRemove()['data'] ,0,-1) |
89
|
|
|
); |
90
|
|
|
|
91
|
|
|
$this->check_fail(); |
92
|
|
|
|
93
|
|
|
return $this; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
public function save() |
97
|
|
|
{ |
98
|
|
|
$this->transaction('begin'); |
99
|
|
|
|
100
|
|
|
try{ |
101
|
|
|
$this->checkForChanges(); |
102
|
|
|
|
103
|
|
|
$this->update( |
104
|
|
|
$this->mountSave()['data'], |
105
|
|
|
"{$this->primary}=:{$this->primary}", |
106
|
|
|
$this->primary.'='.$this->getData()[$this->primary]['value'] |
107
|
|
|
); |
108
|
|
|
|
109
|
|
|
$this->check_fail(); |
110
|
|
|
|
111
|
|
|
$this->transaction('commit'); |
112
|
|
|
}catch(DatamanagerException $er){ |
113
|
|
|
$this->transaction('rollback'); |
114
|
|
|
throw $er; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
return $this; |
118
|
|
|
} |
119
|
|
|
} |